I want to implement a JButton ("Delete row") to delete the row currently selected in a JTable.
JTable.getSelectedRow() works within the selection listener, but does not work out of it.
When the user presses the "Delete row", how can I figure out which row has been selected?
PD: So far, my idea is to create a variable, automatically updated each time the selection listener is called, and when the user presses the button check the value of this variable. However, it does not look very nice...
While going through my ListSelectionListener inner class, I found the next line
m_view.getInputsTable().clearSelection();
Comment the line, and keep working :D
Related
I have a JTable (master) to which I added a ListSelectionListener to catch when a row is selected and populate another JTable (detail) according the selected row.
Now some requirements have changed and if come conditions are met, when the user clicks into another row, I should show a JOptionPane with YES/NO options, and only if YES is clicked then the new row can be selected.
How can I achieve this? Shall I use always ListSelectionListener? I don't think so as it is raised only after the selection is done.
yes, for sure
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
// do some actions here, for example
// print first column value from selected row
System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString());
}
});
when the user clicks into another row,
What about when the user used the up/down arrow keys to move to another row?
The default behaviour in both cases is to select the row automatically.
You might be able to override the changeSelection(...) method of the JTable. I believe this is the method that is invoked by the mouse or keyboard logic to change row selection.
So you would add your logic to display the option pane and then if "Yes" is selected you would invoke super.changeSelection(...).
However, I would find it frustrating to try and use the keyboard to scroll down the table if this option pane keeps popping up. Remember to design a user interface that support both mouse and keyboard effectively.
I have a very basic problem with jtable. I have a jtable that has multiple columns with one of column having a button. When i click on that button a panel drops, and asks to select an option from given options. When i select that option, value replaces in one of the column.
Now, i want when i select multiple rows, and do the same thing as above, it should replace that column in all the selected rows.
Problem: Currently, my table is losing selection when i am clicking the button in one of the column in jtable after multiple row selection.
I searched google and stackoverflow a lot, but could not find anything meaningful. Anyhelp or sample code is appreciated.
Thanks
If I understood your problem correctly then the solution is fairly simple.
First of all the issue probably occurs because once you click on the button java sets a new focus on the button and therefore clearing the focus on the other rows. That won´t be a problem in a single selection because you still click into the selected row, however doing that with multiple rows in one go won´t work that way.
To solve this you need to save your previous selections in something like an ArrayList and after the whole option thing you can apply the changes to every element in the ArrayList and reload the table.
A cleaner and more intuitive approach though would be to place the button outside of the JTable.
I am working on an application that uses a TableView and has a Table Menu Button to add or remove columns from the list.
Since I wanted my column headers to have tooltips, I had no choice but to create a label and use it in the following manner in :
// Some code here
TableColumn col;
// some code here
col.setGraphic(header_title);
The problem with this is that when the program runs, the table menu button shows a list of empty text:
On the other hand when I do:
// Some code here
TableColumn col;
// some code here
col.setText(rs.getString("column_title"));
col.setGraphic(header_title);
I can see the text on the column menu, but the actual titles are appended to the graphic:
I have tried to look for a way to perform a setContentDisplay(GRAPHIC_ONLY), but this does not seem to exist for TableColumn, and I am not sure how to access the header node in order to set this setting.
Any help would be greatly appreciated.
Just forget about the inbuilt table menu button. It's absolutely minimal and not worth mentioning. If you e. g. want to click away 10 columns you have to click on the button, hide the column, click on the button, hide the column, etc. In other words: It closes as soon as you press a menu item.
You can't even extend it with e. g. a hide all and a show all button. And it's buggy: When the last column gets hidden, the menu button vanishes as well, so you have to restart your application if you want to see anything in your table again.
Just create your own table menu. There are 2 examples on this gist:
example which uses a lookup, i. e. works without reflection
example which uses reflection
Then you can adapt whatever header you want and whatever menu items you want.
I have this problem: added a jcheckbox in a jtable. In my model this jcheckbox is loaded with getValueAt and used the method setValueAt when selected by the user.
No problem until I push the confirm button, when all my row selected (true value) disappear and the loop doesn't read anything with true value.
Where is my mistake? How can I update the model before pressing my confirmation button?
Sounds like you are looking for:
table.putClientProperty("terminateOnFocusLost", true);
This tells the table to try to commit any ongoing edits when the focus is transfered to the outside, that is to a component which is not a child of the table.
I have a JTable with 4 columns that are required. When a users finishes one cell, they can click out of the row. I want to force the user to fill in all values before they can continue. Is this possible?
Thanks
Create a popup dialog to add a new row of data to the table. Then the dialog "Save" button can check to make sure all four fields are filled in.