FXML_JDBC_Combo2
  • ObservableList<String> oString1 = FXCollections.<String>observableArrayList(cstr1,cstr2,cstr3);
images:

 
Code (FXML_JDBC_Combo2.txt )

---mainclass--
package javafxtemplate1;
//FXML_JDBC_Combo2
import java.sql.SQLException;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;

public class JavaFXTemplate1 extends Application {
@Override
public void start(Stage primaryStage) throws IOException, SQLException {

FXMLLoader loader =new FXMLLoader(getClass().getResource("JavaFXMLDoc1.fxml"));
//Parent root =FXMLLoader.load(getClass().getResource("JavaFXMLDoc1.fxml"));
Parent root = loader.load();
JavaFXMLDoc1Controller localcontroller = loader.getController();
//Group root = new Group();
Scene scene = new Scene(root, 450,300);
primaryStage.setScene(scene);
primaryStage.setTitle("FXML AnchorPane::JDBC Combo, Button");
primaryStage.show();
localcontroller.loadCombo();

}

public static void main(String[] args) {
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.collections.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" 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------
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
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;
@FXML
public ComboBox myCombobox;

ExternalClass1 ext1 = new ExternalClass1();
@FXML
private Button button;

String cstr1, cstr2, cstr3;
@FXML
private void startTask(ActionEvent event) throws SQLException {
ObservableList<String> oString1 = FXCollections.<String>observableArrayList(cstr1,cstr2,cstr3);
myCombobox.getItems().clear();
myCombobox.setItems(oString1);

}

@Override
public void initialize(URL url, ResourceBundle rb) {
try {
// TODO
ext1.loaddata();// to populate strings in exyternalcl;ass1
cstr1 = ExternalClass1.str1;
cstr2 = ExternalClass1.str2;
cstr3 = ExternalClass1.str3;


} catch (SQLException ex) {
Logger.getLogger(JavaFXMLDoc1Controller.class.getName()).log(Level.SEVERE, null, ex);
}
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() throws SQLException
{
//changing through button click

label1.setText(cstr1);
//changing through button click
myCombobox.getItems().clear();
myCombobox.getItems().addAll(
"example1.com",
"example2.com",
"example3.com",
"example4.com",
"example5.com");
}
}
---------ExternalClass1.java -----
/*
* author mana9 and manas14
* pgaAdmin III // postgresql db 9.2 localhost
* and open the template in the editor.
*/

package javafxtemplate1;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;

/**
*
* @author Manas14
*/
public class ExternalClass1 {
public static String str1, str2, str3; public int n1;
// call ExtrenalClass2 prior to Externalclass1
ExternalClass2 extclass = new ExternalClass2();
private static Connection conn;
public List list1 = new ArrayList();

public void loaddata() throws SQLException
{
// Connection
Connection connect = extclass.createConnection();
String query = " SELECT * FROM pgsvisitor1b where id=102";
System.out.println("query through");
//
try {
Statement st = connect.createStatement();
ResultSet rs = st.executeQuery(query);
System.out.println("query through");
while(rs.next())
{
str1 = rs.getString("fname");
str2 =rs.getString("lname");
str3 = Integer.toString(rs.getInt("age"));
//
list1.add( str1);
list1.add(str2);
list1.add( str3);
}
} catch (Exception e) {
System.out.println(e.toString());
}

}
}

-------ExternalClass2.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.sql.Connection;
import java.sql.DriverManager;

/**
*
* @author Manas14
*/
public class ExternalClass2 {
Connection cnn= null;
String dbURL = "jdbc:postgresql://localhost:5432/pgsdemo1";
String user = "postgres"; String pwd = "postgre_manas9";
// Creating a function to get a connection
public Connection createConnection() {
System.out.println("Connection Object Created");
// checking connection
if (cnn != null) {
System.out.println("Can't creaate a connection");
return cnn;
} else {
try {
// Getting connection
cnn = DriverManager.getConnection(dbURL,user,pwd);
} catch (Exception e) {
System.out.println(e.toString());
}
}
return cnn;
}

}

------Create Table SQL create script---
-- Table: pgsvisitor1b

-- DROP TABLE pgsvisitor1b;

CREATE TABLE pgsvisitor1b
(
id numeric(3,0) NOT NULL,
fname character varying(30),
lname character varying(30),
age numeric(2,0),
CONSTRAINT pk_v1b PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE pgsvisitor1b
OWNER TO postgres;

Displays: