*
The moose likes Swing / AWT / SWT and the fly likes JTable ListSelection problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable ListSelection problem" Watch "JTable ListSelection problem" New topic
Author

JTable ListSelection problem

Master Rice
Greenhorn

Joined: Aug 05, 2001
Posts: 8
I have a JTable with row selection listener handled by the following code.
Say I select row 2 of the table: After selecting the row, the method getSelectedTeam() goes off and does its thing okay,
but upon then returning to the original table, row 2 remains selected and therefore I cannot select it again... I can't figure out how to enable reselection of a row.
I have tried the clearSelection() method as commented out in the code, which appears to work but throws an arrayIndexOutOfBoundsException.
Can somebody help please?

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // Choose only 1 team at a time
ListSelectionModel cellSelectionModel = table.getSelectionModel();
cellSelectionModel.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
if(!e.getValueIsAdjusting()){
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
selectedRow = lsm.getMinSelectionIndex();
System.out.println("Row " + selectedRow + " is now selected.");
getSelectedTeam();
// lsm.clearSelection();
}
}
});
keiyia jackson
Ranch Hand

Joined: Jul 16, 2001
Posts: 49
try this (some part may be of no use):
[code]
private boolean ALLOW_COLUMN_SELECTION= false;
private boolean ALLOW_ROW_SELECTION= true;

table.setSelectionMode
(ListSelectionModel.SINGLE_SELECTION);
if (ALLOW_ROW_SELECTION)
{
ListSelectionModel row =table.getSelectionModel();
row.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
//Ignore extra messages.
if (e.getValueIsAdjusting())
return;
lsm =(ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty())
{
output.setText("No rows are selected.");
output.setText("\n");
}
else
{
selectedRow = lsm.getMinSelectionIndex();
if (resultObjects!=null)
{

try
{
if (selectedRow<resultObjects.length)>
output.setText(SNReflection.print(resultObjects[selectedRow], false));
System.out.println("Row " + selectedRow
+ " is now selected.");
}
catch (Exception exc)
{
exc.printStackTrace();
System.out.print(exc);
output.setText("Unable to display object.");
}
}
else
output.setText("No data found");
}
}
});
}
else
{
table.setRowSelectionAllowed(false);
}
if (ALLOW_COLUMN_SELECTION)
{ // false by default
if (ALLOW_ROW_SELECTION)
{
//We allow both row and column selection, which
//implies that we *really* want to allow individual
//cell selection.
table.setCellSelectionEnabled(true);
}
table.setColumnSelectionAllowed(true);
ListSelectionModel colSM =
table.getColumnModel().getSelectionModel();
colSM.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
//Ignore extra messages.
if (e.getValueIsAdjusting())
return;
lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty())
{
output.setText("No columns are selected.");
//output.append(SNReflection.print("No columns are selected."));

output.setText("\n");
}
else
{
int selectedCol = lsm.getMinSelectionIndex();
//output.setText("Column " + selectedCol +
//" is now selected.");
System.out.println("Column " + selectedCol +
" is now selected.");
output.setText("\n");
}
}
});
}
[code]
use what you need omit the rest
 
Consider Paul's rocket mass heater.
 
subject: JTable ListSelection problem
 
Similar Threads
Jtable Event Handling
Jtable Cell and ENTER key
ListSelectionListener and double clicks
JTable refresh problems
lost focus in JTable