0

How do I get values of other cells if a checkbox is selected of the corresponding JCheckBox in JTable. I add JCheckbox in JTable:

JCheckBox checkBox = new javax.swing.JCheckBox();
jTable1 = new javax.swing.JTable();

jTable1.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {    
    },
    new String [] {
        "Station", "OperationName", "TliScantime", "StartTime", "Completedtime",
        "TliScanTime-StartTime", "StartTime-CompletedTime", "Select"
    }
) {
    Class[] types = new Class [] {
        java.lang.String.class, java.lang.String.class, java.lang.String.class,
        java.lang.String.class, java.lang.String.class, java.lang.String.class,
        java.lang.String.class, java.lang.Boolean.class
    };

    public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
    }
});
jTable1.getColumn("Select").setCellEditor(new DefaultCellEditor(checkBox));
jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
jTable1.getColumnModel().getColumn(0).setPreferredWidth(120);
jTable1.getColumnModel().getColumn(1).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(2).setPreferredWidth(80);
jTable1.getColumnModel().getColumn(3).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(4).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(5).setPreferredWidth(180);
jTable1.getColumnModel().getColumn(6).setPreferredWidth(180);

jScrollPane1.setViewportView(jTable1);

Now I want to do that when some one click on the JCheckbox, it will took the value from TliScanTime-StartTime, StartTime-CompletedTime colum of the selected rows means when JCheckbox is checked.

And it will show the average time of TliScanTime-StartTime, StartTime-CompletedTime in the two separated JLables. And value will be changed dynamically, meaning the average time is changed on every click on the checkbox. I add a checkbox on every rows is created.

What would be some help with some coding example be?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
moin
  • 1
  • 1
  • By "selected rows," do you mean "all rows having the checkbox selected" or "all rows in a `MULTIPLE_INTERVAL_SELECTION` selection model?" – trashgod Aug 04 '11 at 18:43
  • please don't show code that's unrelated to the problem (f.i. column sizing has nothing to do with it) – kleopatra Aug 05 '11 at 10:02

2 Answers2

1

You may be able to adapt the approach shown here.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • It is done by cell rendering am i right can i do this using any event handling.I want to try this using jTable1.getSelectionModel().addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(ListSelectionEvent event ) { // if(jTable1.getValueAt(0,7).equals(null)){ Object b=jTable1.getValueAt(0,7); System.out.println(b); //} } }); – moin Aug 04 '11 at 18:17
  • No, a `CellEditor` is required effect a change in the model, and the default editor does not have the behavior you require. Please edit your question to show code, which is hard to read in a comment. – trashgod Aug 04 '11 at 18:23
1

For JTable, you have to implement TableModelListener.

For example,

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TableCheckboxListenerExample {

    public static void main(String[] args) {
        try {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
            DefaultTableModel model = new DefaultTableModel(new Object[][] {
                        {Boolean.FALSE, "Row 1"},
                        {Boolean.TRUE, "Row 2"}},
                    new String[]{"col1", "col2"}) {

                private static final long serialVersionUID = 1L;

                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    if (getRowCount() > 0 && getValueAt(0, columnIndex) != null) {
                        return getValueAt(0, columnIndex).getClass();
                    }
                    return super.getColumnClass(columnIndex);
                }
            };
            model.addTableModelListener(new TableModelListener() {

                @Override
                public void tableChanged(TableModelEvent e) {
                    int row = e.getFirstRow();
                    int column = e.getColumn();
                    TableModel model = (TableModel) e.getSource();
                    Object data = model.getValueAt(row, column);
                    if (data instanceof Boolean) {
                        System.out.println("Value changed in Row: " + row + " Column: " + column + " New Value = " + data);
                    }
                }
            });
            JTable table = new JTable(model);
            frame.add(new JScrollPane(table));
            frame.pack();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private TableCheckboxListenerExample() {
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mKorbel
  • 109,525
  • 20
  • 134
  • 319