JAVA. JTABLE - How to count currently selected row(s) - java

I need to count selected row for removing multiple rows. Please help with the steps.
Example in the picture I want to delete/remove those selected rows at the same time.

table.getSelectedRows().length

Related

Does NatTable need a manual refresh after repaintCell?

I have the following code snippet.
int index = getEventList().indexOf(myObj);
SelectionLayer selectionLayer = getGlazedListsGridLayer()
.getBodyLayerStack().getSelectionLayer();
getNatTable().repaintCell(0,selectionLayer.getRowIndexByPosition(index));
When I run the code shown above, the affected cells only get repainted after I click on the table displayed in the GUI. If I comment out that code and use getNatTable().refresh(); it repaints without me having to first click on the table.
Is there a way to have a cell repainted without having to click on the table displayed in the GUI? I would hate to have to call refresh() for a large table where this code may be executed many times.
No you don't need to perform some additional step in order to trigger the repainting. The issue in your code is the usage of wrong index values. You have a grid so column 0 is the row header from the NatTable perspective. I suppose you want to redraw the first column of the body, which is index 1 from the NatTable point of view. Also the row index is incorrect, as you calculate the index in the SelectionLayer but actually need the index in the table, which is at minimum +1 if you only have a column header row.
Actually your code should work by adding 1 on the index if you have one row header column and one column header row in the grid.
getNatTable().repaintCell(1, selectionLayer.getRowIndexByPosition(index) + 1);

Editing a table in JavaFx

I have created a table in JavaFx. I used reflection to populate the table with numbers 1 to 100. This table contains a zone number and a description. There are 100 zones. I want the table to be editable. I have used the following code to make the cells editable.
zonesTable.setEditable(true);
zone.setEditable(true);
zone.setCellFactory(TextFieldTableCell.<Zones>forTableColumn());
description.setEditable(true);
description.setCellFactory(TextFieldTableCell.<Zones>forTableColumn());
zone.setCellValueFactory(new PropertyValueFactory<Zones, String>("rZoneNumber"));
description.setCellValueFactory(new PropertyValueFactory<Zones, String>("rDescription"));
for(int i = 0; i < 100; i++){
data.add(new Zones(i + "", ""));
}
zonesTable.setItems(data);
At the moment, this code adds numbers to the zone column and makes the zone and description column editable. However, after I type a value into the column and click the next row, my values that I input into the table disappear. I have no idea why. What do I need to do to cause my typed values to stay visible in the table after I select a different row than the one I am editing? Thanks in advance!
However, after I type a value into the column and click the next row,
my values that I input into the table disappear.
It doesn't work because there's a severe, embarrassing bug in JavaFX that Oracle refuses to fix.
The solution for you would be to press enter before you click the next row. But of course you can't request that from your users.
You may find a workaround here.
If you want to upvote the fix and comment on the bug, here's the issue.
Not enough code to determine how you work with the table.
You could tableColumn.setOnEditCommit(new CustomEventHandler)
You could tableColumn.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter() { your logic});
Those still only commit changes in the cell when Enter is pressed. So you will still need your own implementation based on which event you consider to commit edition.

How can delete/hide the mid row(5)th row before it listed in jtable

Here i have an 1-10 row is listed in jtable i want to delete/hide the 5th row before it listed in jtable.
i set the rowheight but it affected the cellselection.Is there any way to hide/delete the row without affected the normal flow code?
If i remove the row it will throws ArrayIndexoutofBoundException.
in my project executed means one gui open in that gui listed the some string. In here we can add the more string via Add Button on popup Button
Here what i need is i have to hide the particular string. That string is placed on 1st row.
i need to hide the string from end user.
now u hope understand.
You can use the JTable row filtering support in order to hide certain rows without deleting them from the model. Also see this: How can I filter rows in a JTable?
You can eliminate rows in the table by calling the removeRow() method. If you want to just hide it instead of elimintaing it you need to customize the JTable's model to meet your specs on what to display.
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
using DefaultTableModel with JTable you should be able to use model.removeRow(int row) function to remove A row from JTable. There is no way to hide a row based on index as much as i know. However, If you need to hide and re-show mechanism you need to save the row prior to delete it and Save the removedRow in a ArrayList to re-use them.. Something as follows:
List<Vector>deletedRows = new ArrayList<>();
Vector removingRow = (Vector) model.getDataVector().get(5);
deletedRows.add(removingRow);
model.removeRow(5);

How to restrain checking single checkbox in multiple columns in Jtable

I am trying to implement a Jtable which includes three check-box tables like this:
Can you tell me how to set a single selection group of checkboxes which only allows 1 selected check-box in a single row at any time?
I know of nothing out-of-the-box for doing this. I´d have a TableModelListener check these columns every time a change is made and call setValueAt on the checkboxes as needed.

Avoiding duplicate row selection in JTable

I need to avoid duplicate row selection in JTable. If previously selected row is selected again, I don't want to fire mouseevent or need some API which can tell me if it is a duplicate row selection. I can maintain indexes for previously selected rows, but want to avoid it , if possible
I am using JIDE table APIs in my functionality, so if there is any JIDE related method which can indicate such scenario.
Thanks

Categories