JTable checkbox select problem

J

JavaEnquirer

Hi,

I have a JTable in which the first column is a checkbox ( The table
model getColumnClass method returns Boolean for this column )

Now, when a row gets selected, I also want the checkbox to become
selected. To acieve this, I implemented the

public void valueChanged(ListSelectionEvent e) method as follows:

super.valueChanged(e);
if (e.getValueIsAdjusting() )
{
TableModel model .......
int index = e.getFirstIndex();

// the setValueAt method ensures that only one
// box may be checked at a time
model.setValueAt(new Boolean(true),index,0);

}

Although the code above exhibits the desired checkbox behaviour i.e.
when I select a row ( at any column ), the checkbox updates
appropriately, I HAVE NOW LOST THE DEFAULT ROW SELECTION BEHAVIOUR -
even though, I call super.valueCahnged(e)

It doesn't matter where I call super.valueChanged, I can't get the
default selection behavior WITH my checkbox style group behaviour.

Many thanks in advance.
 
V

Vova Reznik

I don't see any problem, here is simple example:

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

public class TableSelectionTest extends JFrame implements
ListSelectionListener {
private final int COLUMN_COUNT = 5;
private TblModel model;
public TableSelectionTest() {
initialize();
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
private void initialize() {
List data = new ArrayList();
for (int i = 0; i < 10; i++) {
Object record[] = new Object[COLUMN_COUNT];
record[0] = Boolean.FALSE;
for (int j = 1; j < COLUMN_COUNT; j++) {
record[j] = new Integer(j);
}
data.add(record);
}
model = new TblModel(data);
JTable table = new JTable(model);
table.getSelectionModel().setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(this);
JScrollPane scroll = new JScrollPane(table);
getContentPane().add(scroll, BorderLayout.CENTER);
}
public static void main(String[] args) {
TableSelectionTest f = new TableSelectionTest();
f.show();
}
class TblModel extends AbstractTableModel {

private List data;
public TblModel(List data) {
this.data = data;
}
public int getColumnCount() {
return COLUMN_COUNT;
}
public int getRowCount() {
return data == null ? 0 : data.size();
}
public void setValueAt(Object value, int rowIndex, int columnIndex) {
getRecord(rowIndex)[columnIndex] = value;
super.fireTableCellUpdated(rowIndex, columnIndex);
}
public Object getValueAt(int rowIndex, int columnIndex) {
return getRecord(rowIndex)[columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
public Class getColumnClass(int columnIndex) {
if (data == null || data.size() == 0) {
return Object.class;
}
Object o = getValueAt(0, columnIndex);
return o == null ? Object.class : o.getClass();
}
private Object[] getRecord(int rowIndex) {
return (Object[]) data.get(rowIndex);
}
}
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
int index = e.getFirstIndex();
model.setValueAt(Boolean.TRUE, index, 0);
}
}
}
 
J

JavaEnquirer

Ah, if I NOT the getValuesAdjusting check, things get better. I can now
select a row, but as soon as I release the mouse button the selection
disappears - though, the checkbox does update correctly.

i.e.

public void valueChanged(ListSelectionEven­t e)
{
super.valueChanged(e);
if (!e.getValueIsAdjusting() )
{
TableModel model .......
int index = e.getFirstIndex();

// the setValueAt method ensures that only one
// box may be checked at a time
model.setValueAt(new Boolean(true),index,0);
}
 
R

Roland

I don't see any problem, here is simple example:

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

public class TableSelectionTest extends JFrame implements
ListSelectionListener {
private final int COLUMN_COUNT = 5;
private TblModel model;
public TableSelectionTest() {
initialize();
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
private void initialize() {
List data = new ArrayList();
for (int i = 0; i < 10; i++) {
Object record[] = new Object[COLUMN_COUNT];
record[0] = Boolean.FALSE;
for (int j = 1; j < COLUMN_COUNT; j++) {
record[j] = new Integer(j);
}
data.add(record);
}
model = new TblModel(data);
JTable table = new JTable(model);
table.getSelectionModel().setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(this);
JScrollPane scroll = new JScrollPane(table);
getContentPane().add(scroll, BorderLayout.CENTER);
}
public static void main(String[] args) {
TableSelectionTest f = new TableSelectionTest();
f.show();
}
class TblModel extends AbstractTableModel {

private List data;
public TblModel(List data) {
this.data = data;
}
public int getColumnCount() {
return COLUMN_COUNT;
}
public int getRowCount() {
return data == null ? 0 : data.size();
}
public void setValueAt(Object value, int rowIndex, int
columnIndex) {
getRecord(rowIndex)[columnIndex] = value;
super.fireTableCellUpdated(rowIndex, columnIndex);
}
public Object getValueAt(int rowIndex, int columnIndex) {
return getRecord(rowIndex)[columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
public Class getColumnClass(int columnIndex) {
if (data == null || data.size() == 0) {
return Object.class;
}
Object o = getValueAt(0, columnIndex);
return o == null ? Object.class : o.getClass();
}
private Object[] getRecord(int rowIndex) {
return (Object[]) data.get(rowIndex);
}
}
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
int index = lsm.getMinSelectionIndex();

// OR:
// int index = lsm.getMaxSelectionIndex();
// Because: selection mode is SINGLE_SELECTION,
// So: minSelectionIndex == maxSelectionIndex

model.setValueAt(Boolean.TRUE, index, 0);
}
}

Tiny adjustment. You need the ListSelectionModel to get the index of the
selected item. ListSelectionEvent holds the first and last index of
items that might have changed, which might be an unselection or a
selection.

Without this, when you haven't selected any row yet, and the maiden row
you select is the last row, the checkbox on the first row gets marked.
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
F

Filip Larsen

JavaEnquirer wrote
I have a JTable in which the first column is a checkbox ( The table
model getColumnClass method returns Boolean for this column )

[...]

Although the code above exhibits the desired checkbox behaviour i.e.
when I select a row ( at any column ), the checkbox updates
appropriately, I HAVE NOW LOST THE DEFAULT ROW SELECTION BEHAVIOUR -
even though, I call super.valueCahnged(e)

I often find in Swing programming that separating requests in time using
invokeLater helps a lot. Instead of making application level calls to
the Swing API in the middle of a listener callback you put them on the
event queue so that they are performed when the callbacks are done.


Regards,
 
J

JavaEnquirer

Many thanks,

I was indeed simply using the wrong index - should have got it from the
ListSelectionModel.
 
Joined
Mar 14, 2007
Messages
1
Reaction score
0
Even Easier

public class extractSelectionTable extends JTable implements ListSelectionListener{
public extractSelectionTable(AbstractTableModel model) {
super(model);
}
public void valueChanged(ListSelectionEvent e) {
AbstractTableModel model = (AbstractTableModel)this.getModel();
if (e.getValueIsAdjusting()) {
//esentially set all to false
int index = e.getFirstIndex();
model.setValueAt(Boolean.FALSE, index, 2);
index = e.getLastIndex();
model.setValueAt(Boolean.FALSE, index, 2);
//afer this your selection is the only one selected
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top