JAVAFX tablecolumn - javafx

I am trying to get my javafx(using scene builder) to read in a list of employee id and name. Most of it works, but I want the names that have been added to the list to show when I click the show button. Also, I created a tableview in scene builder and I want that window to pop up when the "show" button is clicked. Is the only way to do this with a obeservablelist? How can accomplish this. My code so far is below. I didn't attach the employee class or the primary fxml file, but i Can if it helps.
enter
package queue5230;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Queue;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class FXMLDocumentController {
Queue<Employee> empQu = new LinkedList<>();
String name;
String startTime;
String endTime;
int id;
#FXML
private TextField employeeId;
#FXML
private TextField employeeName;
#FXML
private Button insertButton;
#FXML
private Button removeButton;
#FXML
private Button showButton;
#FXML
private TableView<Employee> showTable;
#FXML
private TableColumn<Employee, String> idField;
#FXML
private TableColumn<Employee, String> nameField;
#FXML
private void handleButtonAction(ActionEvent event) {
}
#FXML
private void handleTextInsert(ActionEvent event) throws IOException{
if(insertButton == event.getSource())
{
//get values entered
id = Integer.parseInt(employeeId.getText());
name = employeeName.getText();
//get time entered
Date d = new Date();
//get the date format
startTime = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH).format(d);
//Create employee object
Employee emp = new Employee(id, name, startTime);
//ad object to queue
empQu.add(emp);
}else if(removeButton == event.getSource()){
Employee empr = empQu.remove();
Date end = new Date();
endTime = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH).format(end);
String elapse = getElapsedTime(empr.getTime(), endTime);
}else if(showButton == event.getSource()){
Stage stage;
Parent root;
String str = "ID NAME TIME START\n\n";
Iterator<Employee> it = empQu.iterator();
while(it.hasNext()){
//str += it.next() + "\n";
}
stage = new Stage();
root = FXMLLoader.load(getClass().getClassLoader().getResource("Show.fxml"));
stage.setScene(new Scene(root));
stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait();
}
}
public String getElapsedTime(String starting, String ending)
{
String startToken[] = starting.split(":");
String endToken[] = ending.split(":");
int hours = Math.abs(Integer.parseInt(startToken[0])-Integer.parseInt(endToken[0]));
int minutes = Math.abs(Integer.parseInt(startToken[1])-Integer.parseInt(endToken[1]));
int seconds = Math.abs(Integer.parseInt(startToken[2])-Integer.parseInt(endToken[2]));
return hours + ":" + minutes + ":" + seconds;
}
public void initialize() {
}
}
code here
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="queue5230.FXMLDocumentController">
<children>
<Pane layoutX="31.0" layoutY="22.0" prefHeight="361.0" prefWidth="540.0">
<children>
<TableView fx:id="showTable" layoutX="101.0" layoutY="11.0" prefHeight="340.0" prefWidth="401.0">
<columns>
<TableColumn fx:id="idField" prefWidth="75.0" text="ID" />
<TableColumn fx:id="nameField" prefWidth="75.0" text="Name" />
</columns>
</TableView>
</children>
</Pane>
</children>
</AnchorPane>

fine in this page https://docs.oracle.com/javafx/2/ui_controls/table-view.htm, there are example of how to show a object in the TableView, but you can define in the fxml:
<TableColumn fx:id="nameField" prefWidth="75.0" text="Name" >
<cellValueFactory>
<javafx.scene.control.cell.PropertyValueFactory property="the field name"/>
</cellValueFactory>
</TableColumn>
your fxml code, it will look like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="queue5230.FXMLDocumentController">
<children>
<Pane layoutX="31.0" layoutY="22.0" prefHeight="361.0" prefWidth="540.0">
<children>
<TableView fx:id="showTable" layoutX="101.0" layoutY="11.0" prefHeight="340.0" prefWidth="401.0">
<columns>
<TableColumn fx:id="idField" prefWidth="75.0" text="ID" >
<cellValueFactory>
<javafx.scene.control.cell.PropertyValueFactory property="id"/>
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="nameField" prefWidth="75.0" text="Name" >
<cellValueFactory>
<javafx.scene.control.cell.PropertyValueFactory property="name"/>
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</children>
</Pane>
</children>
</AnchorPane>

Related

How to refresh/update the tableview in a tab controller from another tab controller JavaFx

I developed a simple demo JavaFx app with Sqlite since command line(terminal) with two tabs (tab1-Info and tab2-Register), in tab1-Info there is a tableview "myTable" that shows only id, name and lastname and in tab2-Register shows the textfields to register name and lastname as seen in the following figures.
In the archives i use tab1.fxml and tab2.fxml which tab1 calls Controller.java and tab2.fxml calls Tab2Controller.java.
Actually the simple demo JavaFx app with Sqlite is working perfectly, but i do not know how to refresh tableview "myTable" when i pressed the button "save" which is in another controller tab2.fxml (Tab2Controller.java) different from where tab1-info TableView "myTable" is located.
I wish that when i pressed the button "save", automatically "myTable" updates/refresh the new content data, any advice in order to solve this issue?
Here are the java program archives.
principal.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class principal extends Application {
public static void main(String[] args) {
Application.launch(principal.class, args);
}
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("scheme.fxml"));
stage.setTitle("Demo tab version");
stage.setScene(new Scene(root, 300, 400));
stage.show();
}
}
scheme.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane xmlns:fx="http://javafx.com/fxml" hgap="20" vgap="20" styleClass="root" gridLinesVisible="false">
<padding><Insets top="25" right="2" bottom="20" left="2" /></padding>
<Text id="welcome-text" text="Demo tab" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="6" GridPane.rowSpan="1"/>
<TabPane fx:id="tabPane" xmlns:fx="http://javafx.com/fxml" prefHeight="500.0" prefWidth="300.0" GridPane.columnIndex="0" GridPane.rowIndex="1" GridPane.columnSpan="6" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab fx:id="tab1" text="Info" >
<fx:include source="Tab1.fxml"/>
</Tab>
<Tab fx:id="tab2" text="Register" >
<fx:include source="Tab2.fxml"/>
</Tab>
</tabs>
</TabPane>
<stylesheets>
<URL value="#Login.css" />
</stylesheets>
Tab1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.cell.*?>
<GridPane fx:controller="Controller" xmlns:fx="http://javafx.com/fxml" hgap="20" vgap="20" >
<!-- <ScrollPane visible="true" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="1" GridPane.rowSpan="5"> -->
<VBox layoutX="15.0" layoutY="76.0" prefHeight="528.0" prefWidth="1200.0">
<children>
<TableView fx:id="myTable" prefHeight="512.0" prefWidth="500.0" />
</children>
</VBox>
</GridPane>
Tab2.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.Label?>
<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="Tab2Controller" hgap="10" vgap="5">
<Text id="informacion" text="Personal Information" GridPane.columnIndex="1" GridPane.rowIndex="0" style="-fx-fill:white" />
<Text text="Name" GridPane.columnIndex="1" GridPane.rowIndex="2" style="-fx-fill:white"/>
<TextField fx:id="name" GridPane.columnIndex="1" GridPane.rowIndex="3" prefWidth="150.0"/>
<Text text="Lastname" GridPane.columnIndex="1" GridPane.rowIndex="4" style="-fx-fill:white" />
<TextField fx:id="lastname" GridPane.columnIndex="1" GridPane.rowIndex="5" prefWidth="300.0" />
<Button text="Save" GridPane.columnIndex="1" GridPane.rowIndex="7" prefHeight="20" prefWidth="90" GridPane.halignment="RIGHT" onAction="#handleSubmitButtonAction"/>
</GridPane>
Controller.java
import java.sql.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class Controller implements Initializable{
#FXML
public TableView myTable;
#Override
public void initialize(URL url, ResourceBundle rb) {
TableColumn<Integer, Person> column1 = new TableColumn<>("Id");
column1.setCellValueFactory(new PropertyValueFactory<>("id"));
TableColumn<String, Person> column2 = new TableColumn<>("Name");
column2.setCellValueFactory(new PropertyValueFactory<>("nomCom"));
TableColumn<String, Person> column3 = new TableColumn<>("LastName");
column3.setCellValueFactory(new PropertyValueFactory<>("ApellPatern"));
myTable.getColumns().add(column1);
myTable.getColumns().add(column2);
myTable.getColumns().add(column3);
String sql = "SELECT id, nomCom, ApellPatern FROM warehouses";
try (Connection conn = this.connect();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)){
System.out.println("Opened database successfully");
while (rs.next()) {
System.out.println(rs.getInt("id")+ "\t" + rs.getString("nomCom") + "\t" + rs.getString("ApellPatern"));
myTable.getItems().add(new Person(rs.getInt("id"),rs.getString("nomCom"),rs.getString("ApellPatern")));
}/*Aqui finaliza el while */
/*Aqui finaliza el executequery*/
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}/* End of initialize */
public class Person {
private Integer id=null;
private String nomCom=null;
private String ApellPatern=null;
public Person(Integer id, String nomCom, String ApellPatern){
this.id=id;
this.nomCom=nomCom;
this.ApellPatern=ApellPatern;
}
public Integer getId(){
return id;
}
public String getNomCom() {
return nomCom;
}
public String getApellPatern(){
return ApellPatern;
}
}
private Connection connect() {
String url ="jdbc:sqlite:test.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
}
Tab2Controller.java
import java.sql.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import java.sql.SQLException;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Alert;
public class Tab2Controller implements Initializable{
/*#FXML private fx:id="name" */
#FXML private TextField name;
/*#FXML private fx:id="lastname" ;*/
#FXML private TextField lastname;
#Override
public void initialize(URL url, ResourceBundle rb) {
/* No information*/
}
#FXML protected void handleSubmitButtonAction(ActionEvent event) {
String nomCom=name.getText();
String ApellPatern=lastname.getText();
String sql = "INSERT INTO warehouses(nomCom,ApellPatern) VALUES(?,?)";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1,nomCom);
pstmt.setString(2, ApellPatern);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("The information has been saved succesfully");
alert.showAndWait();
}
private Connection connect() {
// SQLite connection string
// String url = "jdbc:sqlite:C://sqlite/db/test.db";
String url ="jdbc:sqlite:test.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
}
Any help is appreciated. Thank you in advance

Updating element in JavaFX page upon closing another window

So I have a program that starts with a home screen that has button and a label. I want to make it so that when someone click the button, a new window pops out. And by entering some text in the text field in the new window and hit submit button, the new window closes and the text of label in the home screen is set to the user input.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="HomeController" prefHeight="400.0" prefWidth="600.0">
<VBox prefHeight="400.0" prefWidth="600.0">
<children>
<Label fx:id="label" prefWidth="100" prefHeight="50">
<VBox.margin>
<Insets top="75" left="250"/>
</VBox.margin>
</Label>
<Button fx:id="button" prefWidth="100" prefHeight="50" text="Button" onAction="#buttonClick">
<VBox.margin>
<Insets top="150" left="250"/>
</VBox.margin>
</Button>
</children>
</VBox>
</AnchorPane>
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
public class HomeController {
#FXML
private Button button;
#FXML
private static Label label;
public void buttonClick(ActionEvent e) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("alert.fxml"));
Stage window = new Stage();
window.initStyle(StageStyle.UNDECORATED);
window.initModality(Modality.APPLICATION_MODAL);
window.setScene(new Scene(root));
window.show();
}
public static Label getLabel() {
return label;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="AlertController" prefHeight="200.0" prefWidth="300.0">
<VBox prefHeight="200.0" prefWidth="300.0">
<children>
<TextField fx:id="text" prefWidth="200" prefHeight="25">
<VBox.margin>
<Insets top="37.5" left="50" right="50"/>
</VBox.margin>
</TextField>
<Button fx:id="submit" prefWidth="100" prefHeight="25" text="Submit" onAction="#submit">
<VBox.margin>
<Insets top="75" left="100"/>
</VBox.margin>
</Button>
</children>
</VBox>
</AnchorPane>
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class AlertController {
#FXML
private TextField text;
public void submit(ActionEvent actionEvent) {
String str = text.getText();
Stage window = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
HomeController.getLabel().setText(str);
window.close();
}
}
How do different fxml controllers communicate to each other? Since creating an instance of controller class doesn't seem to work, I try to make the label in home controller static, but when I try to update its text in another controller, a NullPointerException is thrown.
Is there any other way
I found the answer at JavaFX Stage close event handler
It's as simple as adding a onHidding event handler for the new window in the original calling function

JAVAFX ComboBox does not display value names correctly [duplicate]

This question already has an answer here:
How do I add a value to items in a ComboBox in JavaFX
(1 answer)
Closed 4 years ago.
Hi I am trying to populate data from a database into a Combo Box, but when I click on the dropdown menu, it does not list the names of the items correctly. I can't figure out why.
Here is what appears when I click on the combobox image
Any help is appreciated thanks!
Edit:I tried using a String Converter but it still does not display the output . Did I use the String Converter correctly?
OrderController class
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.event.ActionEvent;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ResourceBundle;
public class Ordercontroller implements Initializable {
#FXML ComboBox<Orderitems> itemdropdown;
#FXML ComboBox<String> droppax;
#FXML ComboBox<String> tablenum;
#FXML
ObservableList<Orderitems> choice = FXCollections.observableArrayList();
ObservableList<String> pax =
FXCollections.observableArrayList("1","2","3","4","5","6","7");
ObservableList<String> tableno =
FXCollections.observableArrayList("1","2","3","4","5","6","7");
#Override
public void initialize(URL location, ResourceBundle resources) {
itemdrop();
}
public void itemdrop(){
itemdropdown.setValue(null);
itemdropdown.setItems(choice);
droppax.setItems(pax);
tablenum.setItems(tableno);
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
String path = "C:\\Users\\franc\\Desktop\\Database\\CSIA1.accdb";
String url = "jdbc:ucanaccess://" + path;
Connection conn = DriverManager.getConnection(url);
Statement st = conn.createStatement();
String sql = "Select * from Menu";
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
choice.add(new Orderitems(rs.getString("Item")));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Orderitems class
package sample;
import java.awt.*;
import javafx.util.StringConverter;
public class Orderitems extends Ordercontroller{
String Item;
Ordercontroller order = new Ordercontroller();
public Orderitems(String item) {
this.Item = item;
}
public String getItem() {
return Item;
}
public void setItem(String item) {
Item = item;
}
public void start() throws Exception{
itemdropdown.setConverter(new StringConverter<Orderitems>() {
#Override
public String toString(Orderitems object) {
return null;
}
#Override
public Orderitems fromString(String string) {
return null;
}
});}}
Order FXML file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.172-ea"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Ordercontroller">
<children>
<TableView fx:id="Ordertable" layoutX="204.0" layoutY="51.0"
prefHeight="200.0" prefWidth="345.0">
<columns>
<TableColumn fx:id="Oitem" prefWidth="75.0" text="Item" />
<TableColumn fx:id="Oprice" prefWidth="75.0" text="Price" />
<TableColumn fx:id="Ospecial" prefWidth="155.0" text="Special
Requests" />
</columns>
</TableView>
<Button layoutX="475.0" layoutY="266.0" mnemonicParsing="false" text="Add
Item" />
<Label layoutX="38.0" layoutY="238.0" text="Table Number" />
<TextArea fx:id="specialreq" layoutX="278.0" layoutY="352.0"
prefHeight="65.0" prefWidth="200.0" text="Special Requests" />
<Button layoutX="82.0" layoutY="312.0" mnemonicParsing="false" text="Load" />
<Label layoutX="68.0" layoutY="68.0" text="No. of Pax" />
<Button layoutX="496.0" layoutY="369.0" mnemonicParsing="false" text="Add"
/>
<Button layoutX="473.0" layoutY="308.0" mnemonicParsing="false"
text="Delete Item" />
<ComboBox fx:id="itemdropdown" layoutX="308.0" layoutY="270.0"
onAction="#itemdrop" prefWidth="150.0" />
<ComboBox fx:id="tablenum" layoutX="34.0" layoutY="270.0"
prefWidth="150.0" />
<ComboBox fx:id="droppax" layoutX="34.0" layoutY="96.0" prefWidth="150.0"
/>
<Button layoutX="28.0" layoutY="14.0" mnemonicParsing="false"
onAction="#returnhome" text="Exit" />
</children>
</AnchorPane>
Try implementing toString on your OrderItem class.
#Override
public String toString() {
return item;
}

Add checkbox to tableview using FXML and controller class

So I was just trying to add a checkbox to my programm. Only problem is, I don't know how to do that using FXML and normal javaFX. Can you guys help me?
heres my Main class
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/sample/sample.fxml"));
AnchorPane rootLayout = loader.load();
Scene scene = new Scene(rootLayout);
//rootLayout.prefWidthProperty().bind(scene.widthProperty()); not needed
//rootLayout.prefHeightProperty().bind(scene.heightProperty());in these scenario
primaryStage.setScene(scene);
primaryStage.getIcons().add(new Image("/sample/img/Java.png"));
primaryStage.setTitle("Tool-Downloader");
primaryStage.show();
primaryStage.setResizable(false);
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
My Controller:
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import static java.util.ServiceLoader.load;
public class Controller {
#FXML
private void downloadButton(ActionEvent event){
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("grievous.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
stage.setResizable(false);
} catch(Exception e) {
e.printStackTrace();
}
}
#FXML
private void close(ActionEvent onClick){
((Stage)(((javafx.scene.control.Button)onClick.getSource()).getScene().getWindow())).close();
}
#FXML
public void selection(){
}
}
My FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.String?>
<?import javafx.scene.control.cell.*?>
<?import javafx.collections.*?>
<?import javafx.collections.FXCollections?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.CheckBox?>
<?import sample.Programs?>
<?import sample.Selection?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.image.Image?>
<AnchorPane maxHeight="-Infinity" maxWidth="1000" minHeight="650" minWidth="900" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" style="-fx-background-color: white" fx:controller="sample.Controller"
stylesheets="/sample/style1.css">
<HBox alignment="CENTER_LEFT" layoutX="6.0" layoutY="14.0" spacing="52.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" styleClass="background">
<padding>
<Insets left="10.0" right="3.0" top="5.0"/>
</padding>
<ComboBox fx:id="versionCombo" prefWidth="150.0" promptText="Choose OS" styleClass="combox2">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Windows"/>
<String fx:value="Mac OS X"/>
<String fx:value="Ubuntu"/>
</FXCollections>
</items>
</ComboBox>
<Button onAction="#downloadButton" minWidth="80.0" mnemonicParsing="false" text="Download" styleClass="button1"/>
<HBox>
<padding>
<Insets left="550.0" right="0.0" top="0.0"/>
</padding>
<Button onAction="#close" minWidth="80.0" mnemonicParsing="false" text="close" styleClass="close"/>
</HBox>
</HBox>
<TableView fx:id="tableView" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="2.0" AnchorPane.rightAnchor="2.0" AnchorPane.topAnchor="40.0" styleClass="table-view">
<columns>
<TableColumn fx:id="selection" prefWidth="100.0" maxWidth="100.0" minWidth="100" text="Selection" styleClass="table1" >
<cellValueFactory>
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="date" prefWidth="652" maxWidth="652" minWidth="652" text="Program" styleClass="table2">
<cellValueFactory>
<PropertyValueFactory property="programs"/>
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="release" prefWidth="150" maxWidth="150" minWidth="150" text="Date" styleClass="table3">
<cellValueFactory>
<PropertyValueFactory property="date"/>
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="opsystem" prefWidth="100" minWidth="100" maxWidth="100" text="OS" styleClass="table4">
<cellValueFactory>
<PropertyValueFactory property="operatingSystem"/>
<PropertyValueFactory property="java"/>
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<Programs programs="Google Chrome is one of the most popular webbrowsers to date. It has many addons and features." date="06.07.2017" operatingSystem="Windows"/>
</FXCollections>
</items>
</TableView>
</AnchorPane>
And Programs Class:ยจ
package sample;
import javafx.beans.property.SimpleStringProperty;
public class Programs {
private final SimpleStringProperty firstName = new SimpleStringProperty("");
private final SimpleStringProperty lastName = new SimpleStringProperty("");
private final SimpleStringProperty email = new SimpleStringProperty("");
public Programs() {
this("", "", "");
}
public Programs(String firstName, String lastName, String email) {
setPrograms(firstName);
setDate(lastName);
setOperatingSystem(email);
}
public String getPrograms() {
return firstName.get();
}
public void setPrograms(String fName) {
firstName.set(fName);
}
public String getDate() {
return lastName.get();
}
public void setDate(String fName) {
lastName.set(fName);
}
public String getOperatingSystem(){
return email.get();
}
public void setOperatingSystem(String fName){
email.set(fName);
}
}
I have already seaerched for quite some time for this and couldn't find anything.

HiddenSidesPane fxml example

Can Anybody tell me how to construct HiddenSidesPane in FXML not in the controller?
I am having the basic controller code for this but I am not able to understand how to create fxml structure from that.
Can I have something like this? Below code;
<HiddenSidesPane prefWidth="800.0" pinnedSide="TOP">
<content>
<HBox fillHeight="false" nodeOrientation="RIGHT_TO_LEFT"
prefHeight="27.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="3.0" StackPane.alignment="TOP_RIGHT">
<children>
<Label prefHeight="14.0" prefWidth="94.0" text="Value Date From">
<HBox.margin>
<Insets right="2.0" top="5.0" />
</HBox.margin>
</Label>
</children>
<StackPane.margin>
<Insets top="2.0" />
</StackPane.margin>
</HBox>
</content>
</HiddenSidesPane>
This how I made a fast example with the offical FXSampler of ControlsFX in mind:
Assumptions
You already set up your FXML Project and added the ControlsFX.jar as dependency on your build path.
FXMLDocument.fxml
Watch for the import statements.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.*?>
<?import org.controlsfx.control.*?>
<StackPane xmlns:fx="http://javafx.com/fxml/1" prefHeight="200" prefWidth="320" fx:controller="javafxapplication17.FXMLDocumentController">
<children>
<HiddenSidesPane fx:id="pane">
<content>
<Label alignment="CENTER" style="-fx-background-color: white; -fx-border-color: black;" maxHeight="1000.0" maxWidth="1000.0" text="Content Node" />
</content>
<top>
<Label fx:id="pinLabel" style="-fx-background-color: rgba(0,255,0,.25);" text="(Click to pin / unpin)" alignment="CENTER" prefHeight="50.0" prefWidth="50.0" onMouseClicked="#handleMouseClicked" />
</top>
</HiddenSidesPane>
</children>
</StackPane>
FXMLController.java
Inject your variables pane and pinLabel to set them.
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Side;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import org.controlsfx.control.HiddenSidesPane;
public class FXMLDocumentController implements Initializable {
#FXML
private HiddenSidesPane pane;
#FXML
private Label pinLabel;
#FXML
private void handleMouseClicked(MouseEvent event) {
if (pane.getPinnedSide() != null) {
pinLabel.setText("(unpinned)");
pane.setPinnedSide(null);
} else {
pinLabel.setText("(pinned)");
pane.setPinnedSide(Side.TOP);
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
JavaFXApplication17.java
Sorry for that name :-)
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXApplication17 extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
As far as you already know here is the JavaDoc of HiddenSidesPane: http://controlsfx.bitbucket.org/org/controlsfx/control/HiddenSidesPane.html
And if you need an example download the Zip http://fxexperience.com/downloads/controlsfx-8.40.9.zip and unzip it, there is a file controlsfx-samples-8.40.9.jar inside. Double click it and show the sources.

Resources