• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

JTable ListSelection problem

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
});
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic