I'v this kinda of question, but none of the suggested solutions helped.
I am using JavaFX 2.2, Java 6.
I have ChoiceBox defined in FXML:
<ChoiceBox fx:id="companyList" prefHeight="25.0" prefWidth="300.0" />
then I have reference to it in my "Main" class:
@FXML
public ChoiceBox<String> companyList;
Inside "start" method I try to populate it with values like this
companyList = new ChoiceBox<String>();
companyList.setItems(FXCollections.observableArrayList("One","Two","Three"));
But no values are added to list; I'v tried in many different ways suggested in: Populate Choicebox defined in FXML How do I populate a JavaFX ChoiceBox with data from the Database? JavaFx 2 ChoiceBox with custom item
I don't get any compilation errors, but it just don't work, what I am doing wrong?
(PS I am new to Java)
EDIT (SOURCE)
<?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-->
Main class:
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
}