Make a whole Row editable on button click - java

I want to make a whole Row editable...e.g if user selected column 1 than click on edit button the table will show the whole row in editable form...it will be great if any one can give only code upon button event.

I want to make a whole Row editable...e.g if user selected column 1
than click on edit button the table will show the whole row in
editable form...it will be great if any one can give only code upon
button event.
I think have to use JToggleButton, because (JButtons) simple click is immediatelly finalized in comparing with JToggleButton (has two states)
for JTable have to set proper SelectionMode and to override public boolean isCellEditable(int row, int col) {

Related

JTable: waiting for row to be selected according user input

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.

Checkbox column editing on JTable

I have a JTable in which one column is a checkbox. I want to let the user edit the checkbox by mouse drag: if they clicked one checkbox and drag mouse into other checkbox, they will get the same result with the first checkbox clicked.
One approach is to specify ListSelectionModel.MULTIPLE_INTERVAL_SELECTION for the table's selection model, as shown here. Click and drag to select contiguous rows, or add the shift, control or command modifiers to select disparate rows. In a suitable Action, update the TableModel to reflect the selection, as shown here. The JTable will update itself in response.

How can I deselect one row in JTable?

I have JTable with ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
When I use CTRL+mouse click, rows are selected and all are right. But if I want to deselect only one selected row and press CTRL+mouse click to one selected row, all rows are deselected, not only one. Then if I press CTRL+mouse click to some row again, previouse rows are selected again, but without row, that I wanted to deselect.
I want to deselect only one row using CTRL+mouse click. How can I do that?
EDIT:
I had this in my code:
table.getColumnModel().setColumnSelectionAllowed(true);
I found that if I remove this line then all works fine. But can JTable work correctly although 'ColumnSelectionAllowed' is true?
Try method clearSelection on JTable .
it works by calling clearSelection on the ListSelectionModel
If you are looking for deselection one row from many, use the below code:
table.getSelectionModel().removeSelectionInterval(rowFrom, rowTo);
for deselecting one row:
table.getSelectionModel().removeSelectionInterval(row, row);
you can use multiple selection like this
table.getSelectionModel().removeSelectionInterval(1, 3); //1,2,3 starting from 0
table.getSelectionModel().removeSelectionInterval(5, 5); //5
to add it back, use:
table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);
you can use multiple selection like this
table.getSelectionModel().addSelectionInterval(1, 2); //1,2
table.getSelectionModel().addSelectionInterval(4, 4); //4
to set it to specific range, which will clear your old selection first,
table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);
Which it is equal to:
table.clearSelection();
table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);

JCheck box when placed in JTable cell not responding to mouse clicks

I am a newbie to Swings
Can some on please help me
I have a scenario: in which I placed JCheck boxes in the 3rd column of JTable by overriding its column class to return Boolean for the 3rd column.
So it appears as though we have check boxes in the 3rd column of the table...... lately I discovered that the table's list selection event gets fired only based on the "cell focus" in which the check box is present but not the check box itself.
i.e if the focus is on a particular cell (of 3rd column), the toggling of check box in the cell does not fire the event....
Am I missing some thing......
in which I placed JCheck boxes in the 3rd column of JTable by
overriding its column class to return Boolean for the 3rd column.
see Oracles JTable tutorial, to try code examples from tutorial
Boolean value represents JCheckBox in JTable
i.e if the focus is on a particular cell (of 3rd column), the toggling
of check box in the cell does not fire the event....
Am I missing some thing......
have to override public boolean isCellEditable(int row, int col)

Item always selected in JTable

I have created a JTable where the included items are provided with radio buttons in order to enable users to select any of these once at a time. In order to show always the first item selected when the JTable is initialised (or when the item previously selected has been deleted) what method should I use?
You should see the JTable Javadoc and particularly at :
getCellRect(int row, int column, boolean includeSpacing)
and
scrollRectToVisible(Rectangle aRect)
Something like
table.scrollRectToVisible(table.getCellRect(table.getSelectedRows()[0], 0, true));
should suit you.

Categories

Resources