Listener to a JCheckBox in a JTable

Hi!

I have a JTable with several rows of data. I need to be able to invoke my own methods(override a listener of some sort)AND get all values from the other columns on the same row whena user manually changes value of a JCheckBox in the table.

How can I do this? Can you post example code of an easy example!

thx for any help..

[368 byte] By [CbbLea] at [2007-11-27 6:20:58]
# 1
Add a TableModelListener to the TableModel. An event is fired whenever the data in a cell changes.
camickra at 2007-7-12 17:36:56 > top of Java-index,Desktop,Core GUI APIs...
# 2
I don't want an event when the data in a cell changes. I want the event when a user manually changes the value of the checkbox in the table.
CbbLea at 2007-7-12 17:36:56 > top of Java-index,Desktop,Core GUI APIs...
# 3
And thats what happens. When you click on the check box the Boolean value in the TableModel is toggled between TRUE and FALSE.
camickra at 2007-7-12 17:36:56 > top of Java-index,Desktop,Core GUI APIs...
# 4
yes, but it also happens when the table is being updated for instance from another thread...I need it only when the user does it.
CbbLea at 2007-7-12 17:36:56 > top of Java-index,Desktop,Core GUI APIs...
# 5
AbstractCellEditor.addCellEditorListener(...);
camickra at 2007-7-12 17:36:56 > top of Java-index,Desktop,Core GUI APIs...
# 6
problem is I can't get the other values on the same row where the event occured when the editingStopped() method has been triggered in CellEditorListener
CbbLea at 2007-7-12 17:36:56 > top of Java-index,Desktop,Core GUI APIs...
# 7

Here's a simple example:import java.awt.Component;

import javax.swing.DefaultCellEditor;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.UIManager;

import javax.swing.event.CellEditorListener;

import javax.swing.event.ChangeEvent;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableModel;

public class CheckBoxTableListener {

public static void main(String[] args) {

try {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

DefaultTableModel model = new DefaultTableModel(new Object[][] {{Boolean.TRUE, "1"}, {Boolean.FALSE, "2"}}, new String[] {"col1", "col2"}) {

public Class<?> getColumnClass(int columnIndex) {

Object o = getValueAt(0, columnIndex);

if (o != null)

return o.getClass();

return super.getColumnClass(columnIndex);

}

};

JTable table = new JTable(model);

BooleanEditor bEditor = new BooleanEditor();

table.setDefaultEditor(Boolean.class, bEditor);

bEditor.addCellEditorListener(new CheckBoxListener(model));

frame.add(new JScrollPane(table));

frame.pack();

frame.setVisible(true);

}

catch (Exception e) {e.printStackTrace();}

}

private static class CheckBoxListener implements CellEditorListener {

TableModel model;

public CheckBoxListener(TableModel model) {

this.model = model;

}

public void editingCanceled(ChangeEvent e) {

}

public void editingStopped(ChangeEvent e) {

BooleanEditor editor = (BooleanEditor) e.getSource();

int row = editor.getLastRow();

for (int col = 0; col < model.getColumnCount(); col++) {

System.out.println(model.getValueAt(row, col));

}

}

}

private static class BooleanEditor extends DefaultCellEditor {

int lastRow = -1;

public BooleanEditor() {

super(new JCheckBox());

JCheckBox checkBox = (JCheckBox)getComponent();

checkBox.setHorizontalAlignment(JCheckBox.CENTER);

}

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {

lastRow = row;

return super.getTableCellEditorComponent(table, value, isSelected, row, column);

}

public int getLastRow() {

return lastRow;

}

}

}

Rodney_McKaya at 2007-7-12 17:36:56 > top of Java-index,Desktop,Core GUI APIs...
# 8
> problem is I can't get the other values on the same rowYou should be able to use the JTable, getSelectedRow() method to find the row you where editing and then use the getValueAt(...) method to get the data from the columns.
camickra at 2007-7-12 17:36:56 > top of Java-index,Desktop,Core GUI APIs...
# 9
> You should be able to use the JTable, getSelectedRow() method to find the row you where editing and then use the getValueAt(...) method to get the data from the columns. Oops, that would be simpler... (-;
Rodney_McKaya at 2007-7-12 17:36:56 > top of Java-index,Desktop,Core GUI APIs...
# 10
Thanks alot, the solution with getSelectedRows was perfect.Now it works like a charm :Dthx
CbbLea at 2007-7-12 17:36:56 > top of Java-index,Desktop,Core GUI APIs...