Disable Selection of Particular rows on a JTable - java

I am currently using setRowSelectionAllowed(boolean) method. In my implementation, it disables all the rows. However what I want is to to disable some rows and not all the rows. Can someone please help me that to accomplish this?
Here is my code
if (encounterId == currentencounterId)
{
getNstTemplates().getTable().setRowSelectionAllowed(true);
}
else
{
getNstTemplates().getTable().setRowSelectionAllowed(false);
}

You can insert a custom ListSelectionModel into your table that ignores or modifies selection events involving the non-selectable rows.

Related

JTable if any row is selected

I am trying to figure out how to add logic to a JTable via an "if" statement that checks to see if ANY row is selected. I know how to check if a specific row is selected but I can't seem to figure out how to check all rows.
if(tbl.isRowSelected(0)){
Obviously that is checking for a specific row.
I've also tried something like
if(tbl.isRowSelected(0-2000)){
This did not work nor did I expect it to work.
The reason for this is that I'm setting up the table so that when the user clicks a row and then hits an "edit" button a second table will appear with more data related to the row they selected. (Gets complicated here with 2D arrays inside of a hash map but I first need to get by this simple problem).
Thanks for the help in advance!
This should be possible with a ListSelectionModel, which can be retrieved using JTable::getSelectionModel()
So you can call table.getSelectionModel().isSelectionEmpty() to find out if any row is selected.
Use :
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());
}
});
use table.getSelectedRow() it returns the index of the selected row
and getSelectedColumn() return the selected columns
and there is getSelectedRows() it return index arrays of selected rows
int[] indexs=table_name.getSelectedRows();
//all the selected row are here no need to go throw
//all your rows to see if they are selected or not
for(int index_row:rows){
//you code here
}

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);

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

Adding a row to a jTable on downarrow at bottom

I have a jTable which currently displays and allows editing of a database table, I am now trying to sort adding tuples.
I am trying to get it to automatically add a row on downarrow at the bottom. So if I am at the bottom on the table and click my down arrow a new row will appear below. I just can't figure out how to do it.
Thanks
James
Action handling of JTable happens in javax.swing.plaf.basic.BasicTableUI. In your case, you probably need to register a new action for SCROLL_DOWN_CHANGE_SELECTION. In the action, check whether the current selection == last row of the table.
If that doesn't work, set a breakpoint in javax.swing.plaf.basic.BasicTableUI.Actions.actionPerformed(ActionEvent) to see which action is really executed.
JTable has a default Action for the down arrow key. If you want to change this behaviour then you need to create a custom Action. You can do this easily by using the Wrapping Actions concept to leverage the default code.
You can also look at Table Tabbing for a working example of wrapping an Action. You code for the Action would be much simpler and would be something like:
if (last row is selected)
add a new row to the table
invoke the default down arrow action
You'll need to create a KeyListener and add this to your table:
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_DOWN)
// check if selected table row = last row and if so: add new row to table model
}
greetz,
Stijn

remove focus from JTable first row

I have JTable and rows in it. By default first row is selected and focus is in it. How can I deselect first row and change focus to somewhere else that .addListener(new RowSetListener() will work in first row too.
I already try:
tableZaposlenciView1.setRowSelectionAllowed(true);
//tableZaposlenciView1.getSelectionModel().clearSelection();
//tableZaposlenciView1.setColumnSelectionInterval(0,0);
//tableZaposlenciView1.setRowSelectionInterval(false,false);
tableZaposlenciView1.changeSelection(0,0,false,false);
tableZaposlenciView1.requestFocus();
but it is not working.
Have a look at JTable's changeSelection() method. I believe it does what you want.
EDIT: If you want to clear the selection:
JTable table = ...;
table.getSelectionModel().clearSelection();
if someone else have similar problem with ADF and JTable here is solution. I achieve that by overriding first() on the VO impl. my problem can be solved.
That comes in handy in many situations:
no selection after refresh (just return null from first())
reselect a particular row after refresh (before refresh, store a row key, after refresh in first() if stored key is found, navigate to that row and return that from first())
find next matching row for user to work on after refresh of a worklist
avoid costly detail executions in a VL situation

Categories