是否可以将 JCheckBoxes 放在不包含布尔值的 JTable 单元格中?
Is it possible to put JCheckBoxes in JTable cells, which do NOT contain booleans?
我知道如果将 JTable 中的 table 单元格设置为布尔值,它会自动获得一个复选框。
但是,我的 JTable 中有一列包含整数值。是否可以将复选框添加到那些(非布尔值)单元格中,就像我在此处的糟糕绘图中那样:
我的目标不是检查这些列的 true/false 值,而是检查 select 相应的 table 行并对它们进行处理。我知道我可以在我的 ListSelectionModel 中使用多个 selection 间隔,但我发现它对复选框更有吸引力。
为什么不创建一个包含标签和复选框的新 class 并将其添加到您的 table?
import java.awt.*;
import javax.swing.*;
public class LabelWithCheckBox extends JPanel{
public LabelWithCheckBox(String text){
setLayout(new GridBagLayout());
JLabel jLabel = new JLabel(text);
JCheckBox checkBox = new JCheckBox();
add(jLabel, new GridBagConstraints(
0, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0, 0
));
add(checkBox, new GridBagConstraints(
1, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0, 0
));
}
public static void main(String args[]){
JFrame frame = new JFrame();
frame.add(new LabelWithCheckBox("Label text"));
frame.setVisible(true);
frame.setSize(100, 50);
}
}
一种方法是覆盖 table 模型中的 getColumnClass()
方法。
See JavaDoc.
另一个是自己的TableCellEditor and / or TableCellRenderer
是的。将数字和布尔属性聚合到一个 Value
中,并提供合适的 renderer and editor. Your implementation of getColumnClass()
would then return Value.class
for the relevant column. A complete example using Double
is seen here, examined here,如下所示。概括地说,
class Value implements Comparable<Value> {
private Boolean selected;
private Integer value;
public Value(Boolean selected, Double value) {
this.selected = selected;
this.value = value;
}
…
}
class ValueRenderer extends JCheckBox
implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int col) {
Value v = (Value) value;
this.setSelected(v.selected);
this.setText(df.format(v.value));
…
return this;
}
}
class ValueEditor extends AbstractCellEditor implements TableCellEditor, ItemListener {
private ValueRenderer vr = new ValueRenderer();
public ValueEditor() {
vr.addItemListener(this);
}
@Override
public Object getCellEditorValue() {
return vr.isSelected();
}
…
}
…
table.setDefaultRenderer(Value.class, new ValueRenderer());
table.setDefaultEditor(Value.class, new ValueEditor());
我知道如果将 JTable 中的 table 单元格设置为布尔值,它会自动获得一个复选框。
但是,我的 JTable 中有一列包含整数值。是否可以将复选框添加到那些(非布尔值)单元格中,就像我在此处的糟糕绘图中那样:
我的目标不是检查这些列的 true/false 值,而是检查 select 相应的 table 行并对它们进行处理。我知道我可以在我的 ListSelectionModel 中使用多个 selection 间隔,但我发现它对复选框更有吸引力。
为什么不创建一个包含标签和复选框的新 class 并将其添加到您的 table?
import java.awt.*;
import javax.swing.*;
public class LabelWithCheckBox extends JPanel{
public LabelWithCheckBox(String text){
setLayout(new GridBagLayout());
JLabel jLabel = new JLabel(text);
JCheckBox checkBox = new JCheckBox();
add(jLabel, new GridBagConstraints(
0, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0, 0
));
add(checkBox, new GridBagConstraints(
1, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0, 0
));
}
public static void main(String args[]){
JFrame frame = new JFrame();
frame.add(new LabelWithCheckBox("Label text"));
frame.setVisible(true);
frame.setSize(100, 50);
}
}
一种方法是覆盖 table 模型中的 getColumnClass()
方法。
See JavaDoc.
另一个是自己的TableCellEditor and / or TableCellRenderer
是的。将数字和布尔属性聚合到一个 Value
中,并提供合适的 renderer and editor. Your implementation of getColumnClass()
would then return Value.class
for the relevant column. A complete example using Double
is seen here, examined here,如下所示。概括地说,
class Value implements Comparable<Value> {
private Boolean selected;
private Integer value;
public Value(Boolean selected, Double value) {
this.selected = selected;
this.value = value;
}
…
}
class ValueRenderer extends JCheckBox
implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int col) {
Value v = (Value) value;
this.setSelected(v.selected);
this.setText(df.format(v.value));
…
return this;
}
}
class ValueEditor extends AbstractCellEditor implements TableCellEditor, ItemListener {
private ValueRenderer vr = new ValueRenderer();
public ValueEditor() {
vr.addItemListener(this);
}
@Override
public Object getCellEditorValue() {
return vr.isSelected();
}
…
}
…
table.setDefaultRenderer(Value.class, new ValueRenderer());
table.setDefaultEditor(Value.class, new ValueEditor());