Currently in TableView, the row selection is not happening when we click mouse middle button. The row is selected if we do right or left click. I am trying to have the feature of selecting the row on middle button click.
I am already aware that including an event handler in table row factory can fix this. But I have a custom table view with lot of custom features for my application. And this custom TableView is widely used across my application. My main problem is, I cannot ask each and every table row factory to include this fix.
I am looking for a way to do this on higher level (may be on TableView level) so that the row factory does not need to care of that.
Any ideas/help is highly appreciated.
Below is a quick example of what I am trying to achieve.
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableRowSelectionOnMiddleButtonDemo extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
ObservableList<Person> persons = FXCollections.observableArrayList();
for (int i = 0; i < 4; i++) {
persons.add(new Person("First name" + i, "Last Name" + i));
}
CustomTableView<Person> tableView = new CustomTableView<>();
TableColumn<Person, String> fnCol = new TableColumn<>("First Name");
fnCol.setCellValueFactory(param -> param.getValue().firstNameProperty());
TableColumn<Person, String> lnCol = new TableColumn<>("Last Name");
lnCol.setCellValueFactory(param -> param.getValue().lastNameProperty());
tableView.getColumns().addAll(fnCol, lnCol);
tableView.getItems().addAll(persons);
tableView.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>() {
#Override
public TableRow<Person> call(TableView<Person> param) {
return new TableRow<Person>(){
{
/* This will fix my issue, but I dont want each tableView rowfactory to set this behavior.*/
// addEventHandler(MouseEvent.MOUSE_PRESSED,e->{
// getTableView().getSelectionModel().select(getItem());
// });
}
#Override
protected void updateItem(Person item, boolean empty) {
super.updateItem(item, empty);
}
};
}
});
VBox sp = new VBox();
sp.setAlignment(Pos.TOP_LEFT);
sp.getChildren().addAll(tableView);
Scene sc = new Scene(sp);
primaryStage.setScene(sc);
primaryStage.show();
}
public static void main(String... a) {
Application.launch(a);
}
/**
* My custom tableView.
* #param <S>
*/
class CustomTableView<S> extends TableView<S> {
public CustomTableView() {
// A lot of custom behavior is included to this TableView.
}
}
class Person {
private StringProperty firstName = new SimpleStringProperty();
private StringProperty lastName = new SimpleStringProperty();
public Person(String fn, String ln) {
setFirstName(fn);
setLastName(ln);
}
public String getFirstName() {
return firstName.get();
}
public StringProperty firstNameProperty() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public String getLastName() {
return lastName.get();
}
public StringProperty lastNameProperty() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
}
}
The entry point for any global custom (per-control) functionality/behavior is the control's skin. More specifically: user interaction is controlled by the skin's behavior - which unfortunately didn't make it into public scope, such that its access/modification requires to go dirty (as in accessing internal classes/methods, partly via reflection).
Assuming that such access is allowed, the steps to tweak the mouse interaction into reacting the same way for the middle as for the primary button for a TableCell are
implement a custom TableCellSkin
reflectively access its behavior
find the mousePressed handler in the behavior's inputMap
replace the original handler with a custom handler that replaces the mouseEvent coming from the middle button by a mouseEvent coming from the primary button
make the custom TableCellSkin the default by css
Note: the TableRowSkin which is responsible for handling mouseEvents in the free space at the right of the table doesn't separate out the middle button, so currently nothing to do. If that changes in future, simple apply the same trick as for the table cells.
Example:
public class TableRowCustomMouse extends Application {
public static class CustomMouseTableCellSkin<T, S> extends TableCellSkin<T, S> {
EventHandler<MouseEvent> original;
public CustomMouseTableCellSkin(TableCell<T, S> control) {
super(control);
adjustMouseBehavior();
}
private void adjustMouseBehavior() {
// dirty: reflective access to behavior, use your custom reflective accessor
TableCellBehavior<T, S> behavior =
(TableCellBehavior<T, S>) FXUtils.invokeGetFieldValue(TableCellSkin.class, this, "behavior");
InputMap<TableCell<T, S>> inputMap = behavior.getInputMap();
ObservableList<Mapping<?>> mappings = inputMap.getMappings();
List<Mapping<?>> pressedMapping = mappings.stream()
.filter(mapping -> mapping.getEventType() == MouseEvent.MOUSE_PRESSED)
.collect(Collectors.toList());
if (pressedMapping.size() == 1) {
Mapping<?> originalMapping = pressedMapping.get(0);
original = (EventHandler<MouseEvent>) pressedMapping.get(0).getEventHandler();
if (original != null) {
EventHandler<MouseEvent> replaced = this::replaceMouseEvent;
mappings.remove(originalMapping);
mappings.add(new MouseMapping(MouseEvent.MOUSE_PRESSED, replaced));
}
}
}
private void replaceMouseEvent(MouseEvent e) {
MouseEvent replaced = e;
if (e.isMiddleButtonDown()) {
replaced = new MouseEvent(e.getSource(), e.getTarget(), e.getEventType(),
e.getX(), e.getY(),
e.getScreenX(), e.getScreenY(),
MouseButton.PRIMARY,
e.getClickCount(),
e.isShiftDown(), e.isControlDown(), e.isAltDown(), e.isMetaDown(),
true, false, false,
e.isSynthesized(), e.isPopupTrigger(), e.isStillSincePress(),
null
);
}
original.handle(replaced);
}
}
private Parent createContent() {
TableView<Person> table = new TableView<>(Person.persons());
TableColumn<Person, String> first = new TableColumn("First Name");
first.setCellValueFactory(cc -> cc.getValue().firstNameProperty());
TableColumn<Person, String> last = new TableColumn<>("Last Name");
last.setCellValueFactory(cc -> cc.getValue().lastNameProperty());
table.getColumns().addAll(first, last);
BorderPane content = new BorderPane(table);
return content;
}
#Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(createContent()));
// load the default css
stage.getScene().getStylesheets()
.add(getClass().getResource("customtablecellskin.css").toExternalForm());
stage.setTitle(FXUtils.version());
stage.show();
}
public static void main(String[] args) {
launch(args);
}
#SuppressWarnings("unused")
private static final Logger LOG = Logger
.getLogger(TableRowCustomMouse.class.getName());
}
The css with custom skin for TableCell:
.table-cell {
-fx-skin: "<yourpackage>.TableRowCustomMouse$CustomMouseTableCellSkin";
}
Accepted #kleopatra's approach as an answer. However the solution to my question is a bit different to #kleopatra's answer. But the core idea is still the same.
I used the approach to override the doSelect method of TableCellBehavior
#Override
protected void doSelect(double x, double y, MouseButton button, int clickCount, boolean shiftDown, boolean shortcutDown) {
MouseButton btn = button;
if (button == MouseButton.MIDDLE) {
btn = MouseButton.PRIMARY;
}
super.doSelect(x, y, btn, clickCount, shiftDown, shortcutDown);
}
Below is the working demo which solved my issue:
DemoClass:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableRowSelectionOnMiddleButtonDemo extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
ObservableList<Person> persons = FXCollections.observableArrayList();
for (int i = 0; i < 4; i++) {
persons.add(new Person("First name" + i, "Last Name" + i));
}
CustomTableView<Person> tableView = new CustomTableView<>();
TableColumn<Person, String> fnCol = new TableColumn<>("First Name");
fnCol.setCellValueFactory(param -> param.getValue().firstNameProperty());
TableColumn<Person, String> lnCol = new TableColumn<>("Last Name");
lnCol.setCellValueFactory(param -> param.getValue().lastNameProperty());
tableView.getColumns().addAll(fnCol, lnCol);
tableView.getItems().addAll(persons);
tableView.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>() {
#Override
public TableRow<Person> call(TableView<Person> param) {
return new TableRow<Person>() {
{
/* This will fix my issue, but I dont want each tableView rowfactory to set this behavior.*/
// addEventHandler(MouseEvent.MOUSE_PRESSED,e->{
// getTableView().getSelectionModel().select(getItem());
// });
}
#Override
protected void updateItem(Person item, boolean empty) {
super.updateItem(item, empty);
}
};
}
});
VBox sp = new VBox();
sp.setAlignment(Pos.TOP_LEFT);
sp.getChildren().addAll(tableView);
Scene sc = new Scene(sp);
sc.getStylesheets().add(this.getClass().getResource("tableRowSelectionOnMiddleButton.css").toExternalForm());
primaryStage.setScene(sc);
primaryStage.show();
}
public static void main(String... a) {
Application.launch(a);
}
/**
* My custom tableView.
*
* #param <S>
*/
class CustomTableView<S> extends TableView<S> {
public CustomTableView() {
// A lot of custom behavior is included to this TableView.
}
}
class Person {
private StringProperty firstName = new SimpleStringProperty();
private StringProperty lastName = new SimpleStringProperty();
public Person(String fn, String ln) {
setFirstName(fn);
setLastName(ln);
}
public String getFirstName() {
return firstName.get();
}
public StringProperty firstNameProperty() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public String getLastName() {
return lastName.get();
}
public StringProperty lastNameProperty() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
}
}
CustomTableCellSkin class:
import com.sun.javafx.scene.control.behavior.TableCellBehavior;
import com.sun.javafx.scene.control.skin.TableCellSkinBase;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.input.MouseButton;
public class CustomTableCellSkin<S, T> extends TableCellSkinBase<TableCell<S, T>, TableCellBehavior<S, T>> {
private final TableColumn<S, T> tableColumn;
public CustomTableCellSkin(TableCell<S, T> tableCell) {
super(tableCell, new CustomTableCellBehavior<S, T>(tableCell));
this.tableColumn = tableCell.getTableColumn();
super.init(tableCell);
}
#Override
protected BooleanProperty columnVisibleProperty() {
return tableColumn.visibleProperty();
}
#Override
protected ReadOnlyDoubleProperty columnWidthProperty() {
return tableColumn.widthProperty();
}
}
class CustomTableCellBehavior<S, T> extends TableCellBehavior<S, T> {
public CustomTableCellBehavior(TableCell<S, T> control) {
super(control);
}
#Override
protected void doSelect(double x, double y, MouseButton button, int clickCount, boolean shiftDown, boolean shortcutDown) {
MouseButton btn = button;
if (button == MouseButton.MIDDLE) {
btn = MouseButton.PRIMARY;
}
super.doSelect(x, y, btn, clickCount, shiftDown, shortcutDown);
}
}
tableRowSelectionOnMiddleButton.css
.table-cell {
-fx-skin: "<package>.CustomTableCellSkin";
}
My question is about JavaFX-9.
I am trying to handle two table view in one FXML Controller class. But it is giving me a Null pointer exception. How to solve this?
The first TableView (studentTable) is working and second table(rTable) is not working.
home.java:
package Home;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class home extends Application{
public void start(Stage stage)throws Exception{
Parent root=(Parent) FXMLLoader.load(getClass().getResource("Home.fxml"));
Scene scene=new Scene(root);
stage.setScene(scene);
stage.setTitle("Result Analysis System");
stage.show();
}
public static void main(String[] args){
launch(args);
}
}
homeController.java
package Home;
/*_____________________
Error in this file
_____________________*/
import dbUtils.dbConnection;
//import Home.rControl;
//import Home.resultData;
import javafx.collections.FXCollections;
import javafx.collections.ObservableArray;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
//import javafx.scene.control.DatePicker;
import javafx.scene.control.cell.PropertyValueFactory;
//import Home.resultData;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;
public class homeController /*extends resultController*/ implements Initializable{
//Student tab
#FXML
private TextField usn;
#FXML
private TextField name;
#FXML
protected TableView<studentData> studentTable;
#FXML
protected TableColumn<studentData,String> USNcolumn;
#FXML
protected TableColumn<studentData,String> Namecolumn;
//Result tab
#FXML
private TextField rusn;
#FXML
private ComboBox<option> rSelectSem;
#FXML
private TextField rSub1;
#FXML
private TextField rSub2;
#FXML
private TextField rSub3;
#FXML
private TextField rSub4;
#FXML
private TextField rSub5;
#FXML
private TextField rSub6;
#FXML
private TextField rSub7;
#FXML
private TextField rSub8;
#FXML
private Button rAdd;
#FXML
private Button rLoad;
#FXML
private Button rClear;
#FXML
private ComboBox<option> rSelectSem1;
#FXML
private Button rLoad1;
#FXML
private TableView<resultData> rTable;
#FXML
private TableColumn<resultData,String> rColusn;
#FXML
private TableColumn<resultData,String> rColname;
#FXML
private TableColumn<resultData,Integer> rColsub1;
#FXML
private TableColumn<resultData,Integer> rColsub2;
#FXML
private TableColumn<resultData,Integer> rColsub3;
#FXML
private TableColumn<resultData,Integer> rColsub4;
#FXML
private TableColumn<resultData,Integer> rColsub5;
#FXML
private TableColumn<resultData,Integer> rColsub6;
#FXML
private TableColumn<resultData,Integer> rColsub7;
#FXML
private TableColumn<resultData,Integer> rColsub8;
#FXML
private TableColumn<resultData,Integer> rColtotal;
//Analyze tab
#FXML
private ComboBox<option> aSelectSem;
#FXML
private Button aHighmarks;
#FXML
private Button aPassedstudent;
#FXML
private Button aFailedstudent;
#FXML
private Button aListallstudent;
#FXML
private Button adistiction;
#FXML
private Button aFirstclass;
#FXML
private Button aSecondclass;
#FXML
private TableView<analysisData> aTable;
#FXML
private TableColumn<analysisData,String> aColusn;
#FXML
private TableColumn<analysisData,String> aColname;
#FXML
private TableColumn<analysisData,Integer> aColsub1;
#FXML
private TableColumn<analysisData,Integer> aColsub2;
#FXML
private TableColumn<analysisData,Integer> aColsub3;
#FXML
private TableColumn<analysisData,Integer> aColsub4;
#FXML
private TableColumn<analysisData,Integer> aColsub5;
#FXML
private TableColumn<analysisData,Integer> aColsub6;
#FXML
private TableColumn<analysisData,Integer> aColsub7;
#FXML
private TableColumn<analysisData,Integer> aColsub8;
#FXML
private TableColumn<analysisData,Integer> aColtotal;
protected dbConnection dc;
protected ObservableList<studentData> data;
//private ObservableList<resultData> list;
protected String sql = "SELECT * FROM studentDet";
#Override
public void initialize(URL url, ResourceBundle rb){
this.dc = new dbConnection();
this.rSelectSem.setItems(FXCollections.observableArrayList(option.values()));
this.rSelectSem1.setItems(FXCollections.observableArrayList(option.values()));
this.aSelectSem.setItems(FXCollections.observableArrayList(option.values()));
// rTable.setItems(list);
}
//Load student data in student tab
//This is working
#FXML
private void loadStudentData(ActionEvent event) throws SQLException{
try {
Connection conn = dbConnection.getConnection();
this.data = FXCollections.observableArrayList();
ResultSet rs = conn.createStatement().executeQuery(sql);
while (rs.next()){
this.data.add(new studentData(rs.getString(1),rs.getString(2)));
}
}
catch (SQLException e){
System.err.println("error" + e);
}
this.USNcolumn.setCellValueFactory(new PropertyValueFactory<studentData,String>("USN"));
this.Namecolumn.setCellValueFactory(new PropertyValueFactory<studentData,String>( "Name"));
this.studentTable.setItems(null);
this.studentTable.setItems(this.data);
}
//This is working
//Add student in student tab
#FXML
private void addStudent(ActionEvent actionEvent) {
String sqlInsert="INSERT INTO studentDet(USN,Name) VALUES(?,?)";
try {
//here the name is same but it doesn't matter because this is a local variable
Connection conn=dbConnection.getConnection();
PreparedStatement statement=conn.prepareStatement(sqlInsert);
statement.setString(1,this.usn.getText());
statement.setString(2,this.name.getText());
statement.execute();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//This is working
//Clear feilds in Student tab
#FXML
private void clearFields(ActionEvent actionEvent)
{
this.usn.setText("");
this.name.setText("");
}
#FXML
private void rclearFields(ActionEvent actionEvent)
{
this.rusn.setText("");
this.rSub1.setText("");
this.rSub2.setText("");
this.rSub3.setText("");
this.rSub4.setText("");
this.rSub5.setText("");
this.rSub6.setText("");
this.rSub7.setText("");
this.rSub8.setText("");
}
//This is working
//Add marks to sem in Result tab
#FXML
private void addSemMarks(ActionEvent actionEvent)
{
String sqlInsert;
try {
switch (((option) this.rSelectSem.getValue()).toString()) {
case "SEM1":
sqlInsert="INSERT INTO SEM1(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
semMarksAdd(sqlInsert);
break;
case "SEM2":
sqlInsert="INSERT INTO SEM2(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
semMarksAdd(sqlInsert);
break;
case "SEM3":
sqlInsert="INSERT INTO SEM3(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
semMarksAdd(sqlInsert);
break;
case "SEM4":
sqlInsert="INSERT INTO SEM4(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
semMarksAdd(sqlInsert);
break;
case "SEM5":
sqlInsert="INSERT INTO SEM5(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
semMarksAdd(sqlInsert);
break;
case "SEM6":
sqlInsert="INSERT INTO SEM6(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
semMarksAdd(sqlInsert);
break;
case "SEM7":
sqlInsert="INSERT INTO SEM7(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
semMarksAdd(sqlInsert);
break;
case "SEM8":
sqlInsert="INSERT INTO SEM8(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
semMarksAdd(sqlInsert);
break;
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
private void semMarksAdd(String sqlinsert){
//String sqlInsert="INSERT INTO SEM1(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
try {
//here the name is same but it doesn't matter because this is a local variable
Connection conn=dbConnection.getConnection();
PreparedStatement statement=conn.prepareStatement(sqlinsert);
statement.setString(1,this.rusn.getText());
statement.setString(2,this.rSub1.getText());
statement.setString(3,this.rSub2.getText());
statement.setString(4,this.rSub3.getText());
statement.setString(5,this.rSub4.getText());
statement.setString(6,this.rSub5.getText());
statement.setString(7,this.rSub6.getText());
statement.setString(8,this.rSub7.getText());
statement.setString(9,this.rSub8.getText());
statement.execute();
conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
//#########################################################Stable upto here
//From here it is giving error
//TO LOAD STUDENT MARKS DATA IN RESULT TABLE
/*#FXML
private void loadResultData(ActionEvent actionEvent){
String sqlLoad;
try {
switch (((option) this.rSelectSem.getValue()).toString()) {
case "SEM1":
sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM1";
loadRdata(sqlLoad);
break;
case "SEM2":
sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM2";
loadRdata(sqlLoad);
break;
case "SEM3":
sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM3";
loadRdata(sqlLoad);
break;
case "SEM4":
sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM4";
loadRdata(sqlLoad);
break;
case "SEM5":
sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM5";
loadRdata(sqlLoad);
break;
case "SEM6":
sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM6";
loadRdata(sqlLoad);
break;
case "SEM7":
sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM7";
loadRdata(sqlLoad);
break;
case "SEM8":
sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM8";
loadRdata(sqlLoad);
break;
}
}
catch (Exception ex){
ex.printStackTrace();
}
}*/
#FXML
public void loadResultData(ActionEvent actionEvent) {
String sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM1";
loadRdata(sqlLoad); //Error here
}
/*private void loadResultData(ActionEvent actionEvent) {
String sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM1";
rc.loadRdata(sqlLoad);
}*/
private ObservableList<resultData> list = FXCollections.observableArrayList();
private void loadRdata(String sqlLoad) {
try {
Connection conn = dbConnection.getConnection();
this.list = FXCollections.observableArrayList();
//System.out.println("Hello"); //working
ResultSet rs = conn.createStatement().executeQuery(sqlLoad);
while (rs.next()) {
//System.out.println(rs.getString(1)); //Working
this.list.add(new resultData(rs.getString(1), rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6), rs.getInt(7), rs.getInt(8), rs.getInt(9), rs.getInt(10)));
}
// this.rColusn.setCellValueFactory("Helloworld"); //working
this.rColusn.setCellValueFactory(new PropertyValueFactory<resultData, String>("rColUsn"));
this.rColsub1.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub1"));
this.rColsub2.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub2"));
this.rColsub3.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub3"));
this.rColsub4.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub4"));
this.rColsub5.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub5"));
this.rColsub6.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub6"));
this.rColsub7.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub7"));
this.rColsub8.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub8"));
this.rColtotal.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColTotal"));
System.out.println("after block1");
list.clear();
System.out.println("after block2");
rTable.setItems(list);// CAUSING NULL POINTER EXCEPTION
System.out.println("after block3");//NOT PRINTING
}
catch (Exception e) {//System.out.println(" "+e);
System.err.println("error" + e);
}
}
}
resultData.java
package Home;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class resultData {
private final StringProperty rColUsn;
//private final StringProperty rColName;
private final IntegerProperty rColSub1;
private final IntegerProperty rColSub2;
private final IntegerProperty rColSub3;
private final IntegerProperty rColSub4;
private final IntegerProperty rColSub5;
private final IntegerProperty rColSub6;
private final IntegerProperty rColSub7;
private final IntegerProperty rColSub8;
private final IntegerProperty rColTotal;
public resultData(String usn,Integer sub1,Integer sub2,Integer sub3,Integer sub4,Integer sub5,Integer sub6,Integer sub7,Integer sub8,Integer total){
System.out.println("result data");// TWO TIMES IT WORKS,BUT THE THERE ARE 10 COLUMNS AND IT SHOULD PRINT 10 TIMES.
rColUsn = new SimpleStringProperty(usn);
//this.rColName = new SimpleStringProperty(name);
rColSub1 = new SimpleIntegerProperty(sub1);
rColSub2 = new SimpleIntegerProperty(sub2);
rColSub3 = new SimpleIntegerProperty(sub3);
rColSub4 = new SimpleIntegerProperty(sub4);
rColSub5 = new SimpleIntegerProperty(sub5);
rColSub6 = new SimpleIntegerProperty(sub6);
rColSub7 = new SimpleIntegerProperty(sub7);
rColSub8 = new SimpleIntegerProperty(sub8);
rColTotal = new SimpleIntegerProperty(total);
}
public String getrColUsn() {
return rColUsn.get();
}
public StringProperty rColUsnProperty() {
return rColUsn;
}
public void setrColUsn(String rColUsn) {
this.rColUsn.set(rColUsn);
}
public int getrColSub1() {
return rColSub1.get();
}
public IntegerProperty rColSub1Property() {
return rColSub1;
}
public void setrColSub1(int rColSub1) {
this.rColSub1.set(rColSub1);
}
public int getrColSub2() {
return rColSub2.get();
}
public IntegerProperty rColSub2Property() {
return rColSub2;
}
public void setrColSub2(int rColSub2) {
this.rColSub2.set(rColSub2);
}
public int getrColSub3() {
return rColSub3.get();
}
public IntegerProperty rColSub3Property() {
return rColSub3;
}
public void setrColSub3(int rColSub3) {
this.rColSub3.set(rColSub3);
}
public int getrColSub4() {
return rColSub4.get();
}
public IntegerProperty rColSub4Property() {
return rColSub4;
}
public void setrColSub4(int rColSub4) {
this.rColSub4.set(rColSub4);
}
public int getrColSub5() {
return rColSub5.get();
}
public IntegerProperty rColSub5Property() {
return rColSub5;
}
public void setrColSub5(int rColSub5) {
this.rColSub5.set(rColSub5);
}
public int getrColSub6() {
return rColSub6.get();
}
public IntegerProperty rColSub6Property() {
return rColSub6;
}
public void setrColSub6(int rColSub6) {
this.rColSub6.set(rColSub6);
}
public int getrColSub7() {
return rColSub7.get();
}
public IntegerProperty rColSub7Property() {
return rColSub7;
}
public void setrColSub7(int rColSub7) {
this.rColSub7.set(rColSub7);
}
public int getrColSub8() {
return rColSub8.get();
}
public IntegerProperty rColSub8Property() {
return rColSub8;
}
public void setrColSub8(int rColSub8) {
this.rColSub8.set(rColSub8);
}
public int getrColTotal() {
return rColTotal.get();
}
public IntegerProperty rColTotalProperty() {
return rColTotal;
}
public void setrColTotal(int rColTotal) {
this.rColTotal.set(rColTotal);
}
}
studentData.java
package Home;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class studentData{
private final StringProperty USN;
private final StringProperty Name;
public studentData(String usn,String name){
this.USN=new SimpleStringProperty(usn);
this.Name=new SimpleStringProperty(name);
}
public String getUSN() {
return USN.get();
}
public StringProperty USNProperty() {
return USN;
}
public void setUSN(String USN) {
this.USN.set(USN);
}
public String getName() {
return Name.get();
}
public StringProperty nameProperty() {
return Name;
}
public void setName(String name) {
this.Name.set(name);
}
}
Commnd Line Output:
"C:\Program Files\Java\jdk-9.0.1\bin\java" "-javaagent:C:\Program
Files\JetBrains\IntelliJ IDEA 2017.2.5\lib\idea_rt.jar=55700:C:\Program
Files\JetBrains\IntelliJ IDEA 2017.2.5\bin" -Dfile.encoding=UTF-8 -
/* WHEN CLICKING TABLE DISPLAY BUTTON FIRST TIME */
result data
result data
after block1
after block2
errorjava.lang.NullPointerException
/* WHEN CLICKING TABLE DISPLAY BUTTON SECOND TIME*/
errorjava.lang.NullPointerException
result data
result data
after block1
after block2
You need to set a value into the table.
This is not going to work (remove from your loadRdata method):
this.rTable.setItems(null); // giving error here:Null pointer Exception
this.rTable.setItems(this.list);
Initialize your ObservableList first:
private ObservableList<resultData> list = FXCollections.observableArrayList();
Then in your method loadRdata clear the values rather than initializing like you are now.
list.clear();
At the end of you initialize() method you can add the values
rTable.setItems(list);