Reputation: 83
How to load something, like items in ComboBox at JavaFX scene automatically/at start?
I use Java 1.8.0_40
I thought it should be like this, but it won`t working
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);
}
}
The errors is
Error:(26, 19) java: non-static method addSelect() cannot be referenced from a static context
if I change method to static
Error:(202, 13) java: non-static variable firstSortSelect cannot be referenced from a static context
I don`t want to create interface/scene dynamically, only data
Upvotes: 1
Views: 162
Reputation: 83
Sorry guys, I figured out that myself.
Just use initialize method in Controller
@Override
public void initialize(URL location, ResourceBundle resources) {
addSelect();
}
Upvotes: 1