JavaFX: Get the selected row cell's text - java

I need your help.
I am developing JavaFX project that deals with table view and forms.
My Problem is I can't get the text of the selected row in the table view.
I want to get the row cell's text using the row index or the selected row one.
Here is my code:
myTableView.getSelectionModel().selectedItemProperty().addListener( new ChangeListener() {
#Override
public void changed(ObservableValue ov, Object t, Object t1) {
TableView.TableViewSelectionModel selectionModel = myTableView.getSelectionModel();
ObservableList selectedCells = selectionModel.getSelectedCells();
TablePosition tablePosition = (TablePosition) selectedCells.get(0);
int rowIndex = tablePosition.getRow(); // yields the row that the currently selected cell is in
// I Want to get the cell's text in the row using the row_index or the selected row one
}
});
Any solution is appreciated. Thank you!

If you only care about which row is selected, assuming you have a TableView<SomeObject>, you can simply use:
List<SomeObject> selected = selectionModel.getSelectedItems();
or if your table only allows single row selection:
SomeObject selected = selectionModel.getSelectedItem();

Related

DefaultTableModel addRow method adds row in format: column_name = column_value instead of just column_value

I am experiencing a issue while trying to add new row in my JTable. My JTable is using DefaultTableModel, here is the code I use for adding a new row:
AddDialog diag = new AddDialog(MainWindow.getInstance(),"Add Entity",true,tab);
diag.setVisible(true);
if(diag.isSaved()) {
entity = diag.getEntity();
table = diag.getTableModel();
table.getEntities().add(entity);
if(tab instanceof TablePreview) {
tablePreview = (TablePreview)tab;
tableModel = (DefaultTableModel) (tablePreview.getTableView().getModel());
Object[] newRow = new Object[entity.getAttributes().size()];
int i=0;
for (Entry<String, Object> entry : entity.getAttributes().entrySet()) {
newRow[i++]=entry;
}
tableModel.addRow(newRow);
}else if(tab instanceof ChildTablePreview) {
System.out.println("Tab is instanceof ChildTablePreview");
}
}else {
System.out.println("Entity not saved!");
}
diag is instance of AddDialog which extends JDialog, and when I fill in the fields of the dialog and click save it creates an Entity class which I want to add to the table as a new row. The logic works fine, but when the row gets inserted into the table, for some reason table looks like this:
If anyone has any idea how I can fix this, I would really appreciate the help!
You need to use a custom cell renderer in your JTable.
How data appears is based on the class of the columns. The default renderer simply calls the .toString() function for the objects in the column. If the column contains a key value pair, its common for these to appear as key=value.
You need to set the renderer using the TableColumn method setCellRenderer. You can define this renderer to only display the value for the objects in that column.

JTable change view but not change row selection after filter data

When data filtered table row was change view according to result but when I select row then table select always first row of column not filtered row but I want both things filter row and same row selection please solve me problem.
jTextField2.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent ke){
trs.setRowFilter(RowFilter.regexFilter("(?i)"+jTextField2.getText(),0));
}
});
tm = (TableModel)table.getModel();
trs = new TableRowSorter(tm);
table.setRowSorter(trs);
Thanks!
ANSWER IS FOR FORMATTED CODE PURPOSE
int modelRow = table.convertRowIndexToModel(table.getSelectedRow());
DefaultTableModel model = (DefaultTableModel) table.getModel();
name=model.getValueAt(index,0).toString();
sale.setVisible(true);
sale.pack();
sale.setLocationRelativeTo(null);
sale.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
sale.jTextField5.setText(name);
dispose();
so this code is not working for you in your mouse event? does the mouse event get triggered? could you edit your question and add more code (if possible all)?

java swing - deselect a selected row in jtable

I have an instance of JTable in my java swing application. I want to deselect a selected row from this table. From This answer, JTable has provided clearSelection() method that deselect all selected rows in the table. But I want to deselect one row. How can I do this?
Have you tried:
ListSelectionModel.removeSelectionInterval(int index0, int index1)?
See this it toggles the selection of a row in jtable.
You can do this like that:
JTable table = new JTable(); // your table instance
TableModel dataModel = new DefaultTableModel(); // table model
DefaultListSelectionModel selectionModel = new DefaultListSelectionModel(); //table selection model
table.setModel(dataModel);
table.setSelectionModel(selectionModel);
int desiredRow = 0; // row which you want to deselect
selectionModel.removeSelectionInterval(desiredRow, desiredRow); // Removing selection for desired row

How to select and retrieve column with checkbox control with javafx

In my Javafx2 application I have a TableView in which I want to have CheckBoxes to select one or many column.
I find many answer to make an entire column dedicated for checkbox by row, but in my case, i want to add a checkbox for each column label in order to select only the column checked when the user clic on a button for computate a row.
I see there is a javafx.scene.control.cell.CheckBoxTableCell<S,T> but i can i use it for tableview column selection ?
Is it possible ?
Update 1 :
Thanks to #amru, it's possible to add Node into header of TableColumn object.
So i have another question, what is the best way to retrieve indexes of all selected column ? As far as I'm concerned, i search also in javafx api
This code will put a checkbox in the column header.
column.setGraphic(new CheckBox());
UPDATED:
Set the checkbox's user data with the column. You can retrieve it later when user ticked the checkbox.
EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
CheckBox cb = (CheckBox) event.getSource();
TableColumn column = (TableColumn) cb.getUserData();
if (cb.isSelected()) {
lstClm.add(column);
} else {
lstClm.remove(column);
}
for (TableColumn clm : lstClm) {
System.out.println("selected column: " + clm.getText());
}
}
};
CheckBox cb = new CheckBox();
cb.setUserData(firstDataColumn);
cb.setOnAction(handler);
firstDataColumn.setGraphic(cb);
cb = new CheckBox();
cb.setUserData(secondDataColumn);
cb.setOnAction(handler);
secondDataColumn.setGraphic(cb);

Modify cell editor for different rows in a JTable

I have a JTable with the following columns:
rowNumber | Element | Quantity
And a JButton that adds rows every time it's clicked. The column Element has a custom JComboBox cell editor that gets filled with elements from a database. However i need to do the following:
Suppose i have these elements in the JComboBox of the first row in my table:
Element1
Element2
Element3
I select Element2 from the JComboBox in the first row and then I proceed to add another row.
This new row must not show Element2 any more in its JComboBox. And the previous (first) row must not show the Element selected in the second row and so on.
I think it may help to know the expected cardinality of the Set<Element>. Accordingly, #mKorbel raises the important question of scalability, citing this related discussion. In that case, the question proposes a List<DefaultCellEditor>, when a much simpler renderer will do.
Here, a CellEditor can manage a List<DefaultComboBoxModel<Element>>, selecting the correct combo model for the row currently being edited and invoking setModel() on the editor component. As each new table row is added, the editor would add a new element to the List and adjust existing elements as required. I would expect the complexity to grow as O(n2), where n is the cardinality of the Set.
Create a custom CustomCellEditor like this.
final JComboBox<String> comboBox = new JComboBox<String>();
table.getColumnModel().getColumn(1).setCellEditor(new CustomCellEditor(comboBox){
#Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
DefaultComboBoxModel<String> model = (DefaultComboBoxModel<String>) comboBox.getModel();
model.removeAllElements();
{//Add what you need according the row.
model.addElement("X");
model.addElement("Y");
model.addElement("Z");
}
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
});

Categories