如何在JavaFX中设置控件的数量?

时间:2018-06-19 09:09:02

标签: java javafx

我通过 Scene Builder 为JavaFX设计了这个视图。其中有4个from PyQt5 import QtCore, QtGui, QtWidgets import sys class CheckableComboBox(QtWidgets.QComboBox): def __init__(self, parent = None): super(CheckableComboBox, self).__init__(parent) self.setView(QtWidgets.QListView(self)) self.view().pressed.connect(self.handleItemPressed) self.setModel(QtGui.QStandardItemModel(self)) def handleItemPressed(self, index): item = self.model().itemFromIndex(index) if item.checkState() == QtCore.Qt.Checked: item.setCheckState(QtCore.Qt.Unchecked) else: item.setCheckState(QtCore.Qt.Checked) def checkedItems(self): checkedItems = [] for index in range(self.count()): item = self.model().item(index) if item.checkState() == QtCore.Qt.Checked: checkedItems.append(item) return checkedItems class Ui_dialogCreateBatch(object): def setupUi(self, dialogCreateBatch): dialogCreateBatch.resize(400, 338) dialogCreateBatch.setMouseTracking(True) self.gridLayoutWidget = QtWidgets.QWidget(dialogCreateBatch) self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 360, 115)) self.gridLayoutWidget.setObjectName("gridLayoutWidget") self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget) self.gridLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setObjectName("gridLayout") self.cboItemList = CheckableComboBox(self.gridLayoutWidget) self.cboItemList.setObjectName("cboItemList") self.gridLayout.addWidget(self.cboItemList, 0, 0, 1, 1) data = ('item1', 'item2', 'item3') for index, element in enumerate(data): self.cboItemList.addItem(element) item = self.cboItemList.model().item(index, 0) item.setCheckState(QtCore.Qt.Unchecked) self.buttonBox = QtWidgets.QDialogButtonBox(dialogCreateBatch) self.buttonBox.setGeometry(QtCore.QRect(100, 300, 156, 23)) self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok) self.buttonBox.setObjectName("buttonBox") self.retranslateUi(dialogCreateBatch) QtCore.QMetaObject.connectSlotsByName(dialogCreateBatch) def retranslateUi(self, dialogCreateBatch): _translate = QtCore.QCoreApplication.translate dialogCreateBatch.setWindowTitle(_translate("dialogCreateBatch", "Create Item Batch")) class DialogCreateBatch(QtWidgets.QDialog, Ui_dialogCreateBatch): def __init__(self, parent=None): QtWidgets.QDialog.__init__(self, parent) self.setupUi(self) self.buttonBox.accepted.connect(self.on_accepted) self.buttonBox.rejected.connect(self.reject) def on_accepted(self): selectedItems = self.cboItemList.checkedItems() print(selectedItems) self.accept() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) w = DialogCreateBatch() w.show() sys.exit(app.exec_()) 。我希望有可能让用户选择ComboBox使用的数量和数量。 例如,我的目标是有3种模式:

  1. 允许用户使用全部4 ComboBox;
  2. 允许用户只使用一个ComboBox让他选择它;
  3. 允许用户仅使用两个ComboBox并让他们选择四个ComboBox
  4. 的首选组合

    任何设计或想法(及其实施)都受到欢迎,因为此刻我还没有很好的解决方案。我正在考虑使用靠近每个Controls的{​​{1}}元素来启用或禁用它们,但无论如何它并不是很好。此外,我正考虑将3 CheckBox用于选择3种模式并动态填充我的ComboBox,但我不知道从哪里开始实现。

1 个答案:

答案 0 :(得分:1)

如果要让用户选择特定的ComboBox,则可以使用JavaFX函数setDisable()来启用它,该函数位于从Node类继承的所有类上。

(请参见http://api.mongodb.com/java/3.0/com/mongodb/WriteResult.html之间的区别)

在以下情况下,我将disabledProperty()绑定到每个selectedProperty()的反CheckBox上。这样,您可以选择特定的组合框以供选择。希望这可以帮助您开始了解JavaFX绑定的工作原理。

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Test extends Application
{
    public static void main(String[] args){
        launch(args);
    }

    @Override
    public void start (Stage primaryStage) throws Exception {
        VBox vBox = new VBox();

        HBox hBox1 = generateComboBoxHBox();
        HBox hBox2 = generateComboBoxHBox();
        HBox hBox3 = generateComboBoxHBox();
        HBox hBox4 = generateComboBoxHBox();
        vBox.getChildren().addAll(hBox1, hBox2, hBox3, hBox4);

        primaryStage.setScene(new Scene(vBox));
        primaryStage.show();
    }

    // Create 4 of the same HBoxes for an example. Each HBox has a checkbox and combobox
    private HBox generateComboBoxHBox(){
        HBox hBox = new HBox();
        CheckBox checkBox = new CheckBox();
        ComboBox<String> comboBox = new ComboBox<>(FXCollections.observableArrayList("Option1", "Option2", "Option3", "Option4"));

        comboBox.disableProperty().bind(checkBox.selectedProperty().not());

        hBox.getChildren().addAll(checkBox, comboBox);
        return hBox;
    }

}