This discussion is archived
2 Replies Latest reply: Jan 10, 2013 2:07 AM by tanvir ahmed RSS

fxml  ComboBox created in scene builder how to fetch data from database

tanvir ahmed Newbie
Currently Being Moderated
Hi Sir, How r u? Hope to be fine. Sir, I have a problem that i want to fetch data in fxml comboBox created in Scene Builder?

I have used this code in JavaFx 2.0 for comboBox and textField the code is

ComboBox studentRegId = new ComboBox();

try{
stm = db.con.createStatement();
rs = stm.executeQuery("select * from students order by s_reg_id asc");
while(rs.next()){               
for(int i=1; i<=1; i++)
studentRegId.getItems().add(rs.getString("s_reg_id"));

}
}catch(SQLException sqlException){         
}

final TextField sRegId = new TextField();

final String regId[] = new String[1000];
try{
stm = db.con.createStatement();
rs = stm.executeQuery("select * from students order by s_reg_id asc");
int a = 0;
while(rs.next()) {
regId[a] = rs.getString("s_reg_id");
a++; }
}catch(SQLException sqlException){         
}
studentRegId.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>(){
public void changed(ObservableValue ov,Number value, Number new_value){                           
sRegId.setText(regId[new_value.intValue()]);
}
});

Now, the problem is that I want to use this code in Scene Builder fxml ComboBox .

Please Help Me!

I shall be very grate full to you for this kindness.

Regards
  • 1. Re: fxml  ComboBox created in scene builder how to fetch data from database
    lgringo Newbie
    Currently Being Moderated
    Hi,

    Ok, so you create your FXML :

    Add a controller property to your root control. (See Code tab in SceneBuilder or direcly in FXML : fx:controller tag)
    Add a fx:id to your controls (your combobox) (First property in SceneBuilder or directly in FXML : fx:id)

    Example :
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import java.lang.*?>
    <?import java.util.*?>
    <?import javafx.collections.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.paint.*?>
    
    <HBox prefHeight="-1.0" prefWidth="-1.0" xmlns:fx="http://javafx.com/fxml" fx:controller="org.lgringo.comboexample.Controler">
      <children>
        <TextField fx:id="text" prefWidth="100.0" />
        <ComboBox fx:id="combo" prefWidth="200.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>
    </HBox>
    Then, you create your Controler class using @FXML annotation (name of control should be the name of fx:id properties)
    You can do some initialisation in the inherit method "initialize"
    Example :
    package org.lgringo.comboexample;
    
    import javafx.fxml.FXML;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.TextField;
    
    public class Controler {
    
         @FXML
         // fx:id="combo"
         private ComboBox<String> combo; // Value injected by FXMLLoader
    
         @FXML
         // fx:id="text"
         private TextField text; // Value injected by FXMLLoader
    
         @FXML // This method is called by the FXMLLoader when initialization is complete
        void initialize() {
            assert combo != null : "fx:id=\"combo\" was not injected: check your FXML file 'ComboboxExample.fxml'.";
            assert text != null : "fx:id=\"text\" was not injected: check your FXML file 'ComboboxExample.fxml'.";
    
            // Initialize your logic here: all @FXML variables will have been injected
            combo.getItems().clear();
            combo.getItems().addAll("John Lennon","Mick Jagger","David Bowie");
            combo.getItems().add("Others...");
            
            text.setText("List : ");
        }
    }
    Then you use the FXMLLoader class to load your FXML and Controler.
    Example :
    package org.lgringo.comboexample;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class App extends Application {
    
         /**
          * @param args
          */
         public static void main(String[] args) {
              App.launch(args);
    
         }
    
         @Override
         public void start(Stage primaryStage) throws Exception {
              Parent root = FXMLLoader.load(getClass().getResource("ComboboxExample.fxml"));
              primaryStage.setTitle("Combo Example");
              primaryStage.setScene(new Scene(root, 300, 275));
              primaryStage.show();
         }
    
    }
    More info : http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
  • 2. Re: fxml  ComboBox created in scene builder how to fetch data from database
    tanvir ahmed Newbie
    Currently Being Moderated
    Thanks a lot

    I am very grate full for this kindness

    Yahoooooooooooooooooooooooooooooooooooo!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points