In this tutorial, you will learn to use select all checkboxes to select all rows in a table view. This is a very cool feature that everyone needs at time of using JavaFX TableView. This is created with fxml based project in Netbeans IDE.
Here i am sharing implementation code used to create this sample application. Code has been given below as per file name.
FXMLDocument.fxml
<?xml version=”1.0″ encoding=”UTF-8″?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id=”AnchorPane” fx:id=”deleteRows” prefHeight=”492.0″ prefWidth=”557.0″ xmlns=”http://javafx.com/javafx/8″ xmlns:fx=”http://javafx.com/fxml/1″ fx:controller=”tableviewexample.FXMLDocumentController”>
<children>
<Label fx:id=”label” layoutX=”126″ layoutY=”120″ minHeight=”16″ minWidth=”69″ />
<Label layoutX=”16.0″ layoutY=”33.0″ text=”Table View Select All Example” textFill=”#da4d07″>
<font>
<Font name=”System Bold” size=”24.0″ />
</font>
</Label>
<TableView fx:id=”tableview” layoutX=”17.0″ layoutY=”81.0″ prefHeight=”345.0″ prefWidth=”521.0″>
<columns>
<TableColumn fx:id=”name” prefWidth=”107.0″ text=”Name” />
<TableColumn fx:id=”age” prefWidth=”78.0″ text=”Age” />
<TableColumn fx:id=”email” prefWidth=”264.0″ text=”Email ID” />
<TableColumn fx:id=”actionCol” prefWidth=”71.0″ text=”Select” />
</columns></TableView>
<Button layoutX=”119.0″ layoutY=”444.0″ mnemonicParsing=”false” prefHeight=”25.0″ prefWidth=”115.0″ text=”Print Values” />
<CheckBox fx:id=”selectAll” layoutX=”468.0″ layoutY=”62.0″ mnemonicParsing=”false” text=”Select All”>
<font>
<Font name=”System Bold” size=”11.0″ />
</font>
</CheckBox>
<Button layoutX=”253.0″ layoutY=”444.0″ mnemonicParsing=”false” prefHeight=”25.0″ prefWidth=”115.0″ text=”Delete” />
</children>
</AnchorPane>
FXMLDocumentController.java
package tableviewexample;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
/**
*
* @author Cool IT Help
*/
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
TableView tableview;
@FXML
private TableColumn name, age, email, actionCol;
@FXML
private CheckBox selectAll;
ObservableList<Person> data;
ObservableList<Person> items;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println(“You clicked me!”);
label.setText(“Hello World!”);
}
public void initialize(URL url, ResourceBundle rb) {
selectAll.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
System.out.println(“Select All Selected”);
items = tableview.getItems(); // getting table records
for(Person item : items)
{
if(selectAll.isSelected())
item.getRemark().setSelected(true);
else
item.getRemark().setSelected(false);
}
}
});
data = FXCollections.observableArrayList(
new Person(“Jacob”, “23”, “jacob.smith@example.com”, “”),
new Person(“Isabella”, “45”, “isabella.johnson@example.com”, “”),
new Person(“Ethan”, “38”, “ethan.williams@example.com”, “”),
new Person(“Emma”, “29”, “emma.jones@example.com”, “”),
new Person(“Michael”, “31”, “michael.brown@example.com”, “”),
new Person(“Michael”, “25”, “michael.brown@example.com”, “”),
new Person(“Michael”, “32”, “michael.brown@example.com”, “”),
new Person(“Michael”, “34”, “michael.brown@example.com”, “”)
);
name.setCellValueFactory(
new PropertyValueFactory<Person,String>(“firstName”)
);
age.setCellValueFactory(
new PropertyValueFactory<Person,String>(“age”)
);
email.setCellValueFactory(
new PropertyValueFactory<Person,String>(“email”)
);
actionCol.setCellValueFactory(
new PropertyValueFactory<Person,String>(“remark”)
);
tableview.setItems(data);
}
}
Person.java
package tableviewexample;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
public class Person {
private final SimpleStringProperty name;
private final SimpleStringProperty age;
private final SimpleStringProperty email;
private CheckBox remark;
Person(String fName, String age, String email, String value) {
this.name = new SimpleStringProperty(fName);
this.age = new SimpleStringProperty(age);
this.email = new SimpleStringProperty(email);
this.remark = new CheckBox();
}
public String getFirstName() {
return name.get();
}
public void setFirstName(String fName) {
name.set(fName);
}
public String getLastName() {
return age.get();
}
public void setLastName(String fName) {
age.set(fName);
}
public String getEmail() {
return email.get();
}
public void setEmail(String fName) {
email.set(fName);
}
public CheckBox getRemark() {
return remark;
}
public void setRemark(CheckBox remark) {
this.remark = remark;
}
}
TableViewExample.java
package tableviewexample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Cool IT Help
*/
public class TableViewExample extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource(“FXMLDocument.fxml”));
Scene scene = new Scene(root);
scene.getStylesheets().add(“/CSS/mycss.css”);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
For better clarification, I would suggest you please watch my YouTube video tutorial for a better understanding of the whole concept. I have explained the base concept with the help of a simple JavaFX project example. Click below to watch it.
If you still have any questions or doubts please ask in the comment box. I will try my best to reply as soon as possible.
Happy coding! 🙂