在ComboBox中选择一个项目不会出现?

时间:2018-01-04 03:32:19

标签: java javafx combobox

我有一个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);

谢谢!

1 个答案:

答案 0 :(得分:0)

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

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