【问题标题】:Selecting an Item in ComboBox won't show up?在 ComboBox 中选择一个项目不会显示?
【发布时间】:2018-01-04 03:32:19
【问题描述】:

我有一个 GUI,它根据预先从程序传递的值创建多个组合框。 Here's the GUI。现在,当我尝试单击开始时间和结束时间小时/分钟的组合框时,它不会显示,但它会显示 AM/PM 组合框(如图所示),但是我知道数据正在收到并选择,因为我可以打印一份声明来确认这一点。唯一的问题是它不会显示在我的 GUI 上,是因为我的多个窗格的分层问题吗?这是我的代码:

// Create a new grid pane to get rid of nodes from previous page
            pane = new GridPane();

            // Obtain the number of time slots from the CB
            numTimeSlots = numOfTimeSlotsCB.getValue();

            // For loop to create the required amount of labels and textFields
            startingTimeHBox = new HBox[numTimeSlots];
            timeSlotLabel = new Label[numTimeSlots];
            startingTimeLabel = new Label[numTimeSlots];
            startingTimeHour = new ComboBox[numTimeSlots];
            colonLabel1 = new Label[numTimeSlots];
            startingTimeMinutes = new ComboBox[numTimeSlots];
            startingTimeAMPM = new ComboBox[numTimeSlots];

            endingTimeHBox = new HBox[numTimeSlots];
            endingAndStartingTimeVBox = new VBox[numTimeSlots];
            endingTimeLabel = new Label[numTimeSlots];
            endingTimeHour = new ComboBox[numTimeSlots];
            colonLabel2 = new Label[numTimeSlots];
            endingTimeMinutes = new ComboBox[numTimeSlots];
            endingTimeAMPM = new ComboBox[numTimeSlots];

            for(int i = 0; i < numTimeSlots; i++)
            {
                timeSlotLabel[i] = new Label("Time Slot " + (i+1));
                startingTimeLabel[i] = new Label("Starting Time:");
                startingTimeHour[i] = new ComboBox<>();
                startingTimeHour[i].getItems().addAll(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
                colonLabel1[i] = new Label(":");
                startingTimeMinutes[i] = new ComboBox<>();
                startingTimeMinutes[i].getItems().addAll("00", "10", "20", "30", "40", "50");
                startingTimeAMPM[i] = new ComboBox<>();

                startingTimeHour[i].setPrefWidth(50.0);
                startingTimeMinutes[i].setPrefWidth(50.0);
                startingTimeAMPM[i].getItems().addAll("AM", "PM");

                startingTimeHBox[i] = new HBox();
                startingTimeHBox[i].getChildren().addAll(startingTimeHour[i], colonLabel1[i], startingTimeMinutes[i], startingTimeAMPM[i]);
                startingTimeHBox[i].setSpacing(5.0);

                endingTimeLabel[i] = new Label("Ending Time:");
                endingTimeHour[i] = new ComboBox<>();
                endingTimeHour[i].getItems().addAll(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
                colonLabel2[i] = new Label(":");
                endingTimeMinutes[i] = new ComboBox<>();
                endingTimeMinutes[i].getItems().addAll("00", "10", "20", "30", "40", "50");
                endingTimeAMPM[i] = new ComboBox<>();

                endingTimeHour[i].setPrefWidth(50.0);
                endingTimeMinutes[i].setPrefWidth(50.0);
                endingTimeAMPM[i].getItems().addAll("AM", "PM");

                endingTimeHBox[i] = new HBox();
                endingAndStartingTimeVBox[i] = new VBox();
                endingTimeHBox[i].getChildren().addAll(endingTimeHour[i], colonLabel2[i], endingTimeMinutes[i], endingTimeAMPM[i]);
                endingAndStartingTimeVBox[i].getChildren().addAll(timeSlotLabel[i], startingTimeLabel[i], startingTimeHBox[i], endingTimeLabel[i], endingTimeHBox[i]);
                endingTimeHBox[i].setSpacing(5.0);
                endingAndStartingTimeVBox[i].setSpacing(5.0);
                pane.add(endingAndStartingTimeVBox[i], 0, i);
                pane.setMargin(endingAndStartingTimeVBox[i], new Insets(10,10,10,10));
            }

            // Add next and prev buttons
            buttonBox = new HBox();
            buttonBox.getChildren().addAll(prevButton, nextButton);
            buttonBox.setSpacing(5.0);
            buttonBox.setAlignment(Pos.BOTTOM_RIGHT);
            pane.add(buttonBox, 0, numTimeSlots + 1);
            pane.setHalignment(buttonBox, HPos.RIGHT);
            pane.getColumnConstraints().add(new ColumnConstraints(320));
            pane.setMargin(buttonBox, new Insets(10,10,10,10));

            // Add pane to the ScrollPane
            scrollPane.setContent(pane);
            scrollPane.setFitToHeight(true);

谢谢!

【问题讨论】:

  • 如果我删除ComboBox 上的setPrefWidth 语句,它似乎工作得很好,也许当组合框太小时组件根本无法呈现值......不知道

标签: java javafx combobox


【解决方案1】:

问题是组合框中的箭头有一个默认填充,它会切入看起来像可用空间的地方有两种方法可以解决这个问题,增加组合框的大小我发现你的代码开始在 70 左右工作,或者你可以使用css

.combobox .arrow-button-{
    -fx-padding:0;
    -fx-insets:0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    相关资源
    最近更新 更多