Reputation: 461
I have my tableview generated dynamically and filled out with data from my database in postgresql. That part works fine. Now I would like to be able to have onclick listener for each row in that table, so whenever I click on a row, another window pops out, for example. Moreover, I would like to know not the number of the row clicked, but information from that row. So for example in output:
When I click lets say on July, I want to have 111.0 value stored somewhere. I would be happy with simply printing it out for now. (picture is not mine, I used it only for demonstrative purposes)
Here is my code:
//TABLE VIEW AND DATA
private ObservableList<ObservableList> data;
private TableView tableview;
//MAIN EXECUTOR
public static void main(String[] args) {
launch(args);
}
//CONNECTION DATABASE
public void buildData(){
data = FXCollections.observableArrayList();
try{
Connection c = DBConnect.getDBConnection();
ResultSet rs = getRecords();
/**********************************
* TABLE COLUMN ADDED DYNAMICALLY *
**********************************/
for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++){
//We are using non property style for making dynamic table
final int j = i;
TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleObjectProperty(param.getValue().get(j));
}
});
tableview.getColumns().addAll(col);
// System.out.println("Column ["+i+"] ");
}
/********************************
* Data added to ObservableList *
********************************/
while(rs.next()){
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
//Iterate Column
row.add(rs.getString(i));
}
// System.out.println("Row [1] added "+row );
data.add(row);
}
//FINALLY ADDED TO TableView
tableview.setItems(data);
}catch(Exception e){
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
@Override
public void start(Stage stage) throws Exception {
//TableView
tableview = new TableView();
buildData();
//Main Scene
Scene scene = new Scene(tableview);
stage.setScene(scene);
stage.show();
}
public ResultSet getRecords() throws SQLException
{
Connection connection = getDBConnection();
java.sql.Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM table");
return rs;
}
I tried with this code but it does not work (error says that I cannot cast Object to UserClass.
UserClass selectedUser = new UserClass();
final Clipboard clipboard = Clipboard.getSystemClipboard();
tableview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener()
{
public void changed(ObservableValue observable, Object oldValue,Object newValue)
{
UserClass selectedUser = (UserClass)newValue;
ClipboardContent content = new ClipboardContent();
content.putString(selectedUser.toString());
clipboard.setContent(content);
}
});
If there is any other, preferably easier and more straightforward way I would greatly appreciate it
Upvotes: 0
Views: 1250
Reputation: 3654
As far as I understand you can do so: add cell factory for columns of the TableView, and for each cell there is a link on TableRow, via:
public final ReadOnlyObjectProperty<TableRow> tableRowProperty
A TableRow object has all these useful properties, such as setting on<...>Click listeners.
Each cell has them all too.
Upvotes: 2