Update: Including project (reproducible example) and final details
I currently have an application that showcases a TableView populated with simple object instances (observable list)
The objective is to highlight selected items (rows) in the TableView.
For instance, if the user presses 'Insert', I update (in the observable list) the selected object by toggling a boolean value within it. This marking allows users to perform additional actions.
Unfortunately, using
myTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
results in losing the selection whenever a key is pressed or a mouse click occurs.
To address this issue, keyboard management is implemented as follows:
public boolean implementListenerPackage(Scene s) {
//initialization...
s.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent ke) {
switch (ke.getCode()) {
case INSERT:
setObservableListObjectSelect();
break;
}
}
});
}
The structure of the objects in the observable list is relatively straightforward:
public class myObject {
private boolean selected;
private String otherStuff = "";
// Constructor, getters, and setters follow
Furthermore, there is MouseEvent handling to manage other interactions. Upon creating the TableView, the following snippet is added:
myTableView.setRowFactory(rftv-> {
TableRow<type> rowObj = new TableRow<>();
rowObj.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
if (e.getClickCount() == 2 && (!rowObj.isEmpty())) {
SomeClass.doSomethingForDoubleClick()
} else { // Single click
SomeClass.doSomethingForSingleClick()
}
}
});
return rowObj;
});
The goal is to modify the CSS of a row when the boolean value in myObject changes, allowing the user selection to remain highlighted even after clicking on another row.
Several strategies were attempted:
- Extensive research was conducted to find relevant examples, however, most examples did not align with the current scenario.
- Efforts were made to enhance the rowFactory implementation, but complications arose leading to null pointer exceptions.
- Direct modification through keyboard management seemed complex, requiring updating the selected object, identifying selected cells, and adjusting CSS individually (based on column logic).
- Exploration of implementing a binding mechanism between the object and the row proved challenging due to the differences in problem contexts.
The solution may be evident, yet remains elusive.
update: Considerations include:
- Centralized keyboard management
- Existing factory settings on the tableView.
- Presence of multiple TableViews in the original application, necessitating automatic CSS style changes rather than hardcoded solutions.
A concise code excerpt:
package application;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.stage.Stage;
...
public class Main extends Application {
Label lbl01 = new Label("Information");
@Override
public void start(Stage primaryStage) {
try {
...
});
KeyboardManagement km = new KeyboardManagement();
km.implementListener(scene, lbl01, tv1);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
package application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.input.KeyCode;
...
public class KeyboardManagement {
public KeyboardManagement() {
}
...
}
package application;
public class MyObject {
private boolean keyboardSelected;
private String dataA;
private String dataB;
private String dataC;
public MyObject(boolean keyboardSelected, String dataA, String dataB, String dataC) {
super();
this.keyboardSelected = keyboardSelected;
this.dataA = dataA;
this.dataB = dataB;
this.dataC = dataC;
}
...
}