FXML_ComboBox1
  • FXMLLoader loader =new FXMLLoader(getClass().getResource("JavaFXMLDoc1.fxml"));
  • Parent root = loader.load();
Code :


--main class--------
package javafxtemplate1;
//Grid_VBOx_Thread
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.layout.AnchorPane;

public class JavaFXTemplate1 extends Application {
//String path ="file:///C:/NetBean_Example/JavaCore/JavaFXTemplate1/JavaFXMLDoc1.fxml";

@Override
public void start(Stage primaryStage) throws IOException {
FXMLLoader loader =new FXMLLoader(getClass().getResource("JavaFXMLDoc1.fxml"));
//Parent root =FXMLLoader.load(getClass().getResource("JavaFXMLDoc1.fxml"));
Parent root = loader.load();
//Group root = new Group();
Scene scene = new Scene(root, 450,300);
primaryStage.setScene(scene);
primaryStage.setTitle("FXML AnchorPane::List , ListView::Label, Button");
primaryStage.show();
//localcontroller.loadCombo();

}
//

//
public static void main(String[] args) {
Application.launch(args);
}

}



----JavaFXMLDoc1.xml----
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.Label?>
<?import javafx.collections.*?>

<AnchorPane id="AnchorPane" prefHeight="304.0" prefWidth="408.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxtemplate1.JavaFXMLDoc1Controller">
<stylesheets>
<URL value="@javafxmldoc1.css" />
</stylesheets>
<children>
<Button fx:id="button" layoutX="24.0" layoutY="14.0" onAction="#startTask" text="Click Me!" />
<Label fx:id="label1" layoutX="139.0" layoutY="18.0" minHeight="16" minWidth="69" />
<Button layoutX="31.0" layoutY="227.0" mnemonicParsing="false" onAction="#endTask" text="Exit" />
<ComboBox fx:id="myCombobox" layoutX="89.0" layoutY="102.0" prefWidth="150.0" >
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Item 1" />
<String fx:value="Item 2" />
<String fx:value="Item 3" />
</FXCollections>
</items>
</ComboBox>
</children>
</AnchorPane>

---JavaFXMLDoc1Controller.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javafxtemplate1;


import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;


/**
* FXML Controller class
*
* @author Manas9
*/
public class JavaFXMLDoc1Controller implements Initializable {

/**
* Initializes the controller class.
*/
@FXML
private Label label1, label2;
@FXML
public ComboBox myCombobox;

@FXML
private void startTask(ActionEvent event) {
loadCombo();
}

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
label1.setText("Initialized ");
System.out.println("initialized!");
//ComboBox cmbBox = new ComboBox();

}


@FXML
private void endTask(ActionEvent event) {
//System.out.println("You clicked me!");
Platform.exit();
}
public void loadCombo()
{
//changing through button click
myCombobox.getItems().clear();
myCombobox.getItems().addAll(
"example1.com",
"example2.com",
"example3.com",
"example4.com",
"example5.com");


}
}

Displays:

flush combo box with new Items