How do I get values of another cell if a checkbox is selected of the corresponding checkbox in jtable?
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?
You may be able to adapt the approach shown here.
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() {
}
}
精彩评论