我是这样一个问题,但没有一个建议的解决方案有帮助。
我正在使用JavaFX 2.2,Java 6。
我在FXML中定义了ChoiceBox:
<ChoiceBox fx:id="companyList" prefHeight="25.0" prefWidth="300.0" />
然后我在我的“主要”课程中引用它:
@FXML
public ChoiceBox<String> companyList;
在“开始”方法内部我尝试用这样的值填充它
companyList = new ChoiceBox<String>();
companyList.setItems(FXCollections.observableArrayList("One","Two","Three"));
但是没有值添加到列表中; 我尝试了许多不同的方式: Populate Choicebox defined in FXML How do I populate a JavaFX ChoiceBox with data from the Database? JavaFx 2 ChoiceBox with custom item
我没有得到任何编译错误,但它只是不起作用,我做错了什么?
(PS我是Java新手)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane focusTraversable="true" prefHeight="435.0" prefWidth="320.0" snapToPixel="false" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Main">
<Pane prefHeight="425.0" prefWidth="300.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
<VBox>
<VBox minHeight="50.0">
<Label prefHeight="25.0" text="Įmonė kuriai generuojamas išrašas" />
<ChoiceBox fx:id="companyList" prefHeight="25.0" prefWidth="300.0" />
<!-- other markup is irrelevant-->
主要课程:
package sample;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
public class Main extends Application {
@FXML public ChoiceBox<String> companyList;
@Override
public void start(final Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("IST Billing Generator");
Scene scence = new Scene(root);
primaryStage.setScene(scence);
companyList = new ChoiceBox<String>();
companyList.setItems(FXCollections.observableArrayList("One","Two","Three"));
this.primaryStage = primaryStage;
primaryStage.show();
}
//other code is irrelevant too
}