FXML_JDBC_Combo1
  • Using PostGreSQL 9.2 DB/ Windows 7 and 10
Images:
DB:

SQL Query Test:

Code : (FXML_JDBC_Combo1.txt)

---mainclass--
package javafxtemplate1;
//
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::List , ListView::Label, Button");
primaryStage.show();
localcontroller.loadCombo();
/*
Group groot = new Group();
ExternalClass1 ext1 = new ExternalClass1();
ext1.loaddata();// to populate strings in exyternalcl;ass1
//
// working opn combox
ObservableList<String> oString1 =
FXCollections.<String>observableArrayList(ext1.list1);
ComboBox<String> CBox1 = new ComboBox(oString1);
ListView<String> lvwList = new ListView<>(oString1);
lvwList.setLayoutX(10); lvwList.setLayoutY(80);
lvwList.setMaxWidth(100);
//
CBox1.setLayoutX(110);CBox1.setLayoutY(80);
CBox1.setMaxWidth(100);

//

primaryStage.setWidth(300);
primaryStage.setHeight(250);
Label label1 = new Label("fname");
Label label2 = new Label("lname");
Label label3= new Label("Age");
Label label4 = new Label("caption1");
Label label5 = new Label("caption2");
Label label6 = new Label("caption3");

Button exitbtn = new Button("Exit Platform");
exitbtn.setOnAction(new DemoEventHandler1(){ });
Button btn1 = new Button("Display Data ");
btn1.setOnAction((ActionEvent e) -> {
label4.setText(ExternalClass1.str1);
label5.setText(ExternalClass1.str2);
label6.setText(ExternalClass1.str3);
});
//future linked data with labels
CBox1.setOnAction((ActionEvent e) -> {
label4.setText(ExternalClass1.str1);
label5.setText(ExternalClass1.str2);
label6.setText(ExternalClass1.str3);
});
//
exitbtn.setLayoutX(10);exitbtn.setLayoutY(10);
btn1.setLayoutX(150);btn1.setLayoutY(10);
label1.setLayoutX(10);label1.setLayoutY(40);
label2.setLayoutX(80);label2.setLayoutY(40);
label3.setLayoutX(160);label3.setLayoutY(40);
label4.setLayoutX(10);label4.setLayoutY(60);
label5.setLayoutX(80);label5.setLayoutY(60);
label6.setLayoutX(160);label6.setLayoutY(60);

//
groot.getChildren().addAll(label1,label2, label3,
label4,label5, label6,btn1, exitbtn, lvwList,CBox1 );
//
Scene scene = new Scene(groot, 350, 150);
primaryStage.setScene(scene);
primaryStage.setTitle("PostgreSQL:: UIControl");
primaryStage.show();
*/
}

public static void main(String[] args) {
launch(args);
}
}
class DemoEventHandler1 implements EventHandler<ActionEvent>{

@Override
public void handle(ActionEvent event) {
Platform.exit();

}
}
-----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 {
myCombobox.getItems().clear();
myCombobox.getItems().addAll(cstr1, cstr2,cstr3);
}

@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:

Overriding Combobox with SQL query strings: