2015-05-03 73 views
1

如何在JavaFX場景中自動/開始時加載類似於ComboBox中的項的內容?在JavaFX場景中預加載東西

我使用Java 1.8.0_40

我認爲應該是這樣的,但它那倒工作

public class Main extends Application { 


public static void main(String[] args) throws SQLException { 
    launch(args); 

} 

@Override 
public void start(Stage primaryStage) throws Exception { 
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 
    primaryStage.setTitle("Print Shop"); 
    primaryStage.setScene(new Scene(root)); 
    primaryStage.show(); 

    Controller.addSelect(); 
} 


public class Controller implements Initializable { 

@FXML 
private ComboBox<String> firstSortSelect; 

public void addSelect() { 
    List<String> select = new ArrayList<String>(); 
    select.add("smts1"); 
    select.add("smts2"); 
    for (String cat : select) { 
     firstSortSelect.getItems().add(cat); 
    } 
} 

的錯誤是

Error:(26, 19) java: non-static method addSelect() cannot be referenced from a static context

如果我改變方法靜態

Error:(202, 13) java: non-static variable firstSortSelect cannot be referenced from a static context

我不想動態創建界面/場景,只有數據

回答

1

對不起,夥計們,我想出了我自己。

只需要使用控制器初始化方法

@Override 
public void initialize(URL location, ResourceBundle resources) { 
    addSelect(); 
}