0

I know that a table cell in a JTable automatically gets a Checkbox if you set its class to Boolean.

However, I have a column in my JTable, which contains integer values. Is it possible to add Checkboxes to those (non-boolean) cells like in my poor drawing here:

https://i.stack.imgur.com/AfJ5x.png

My goal is not to check those columns for true/false values, but rather to select the corresponding table rows and do something with them. I know that I could use multiple selection intervals in my ListSelectionModel instead, but I'd find it much more appealing with checkboxes.

camickr
  • 321,443
  • 19
  • 166
  • 288
Phreneticus
  • 349
  • 2
  • 11

3 Answers3

3

Yes. Aggregate the numeric and boolean attributes into a single Value and provide a suitable 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 and illustrated below. In outline,

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());

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • When I do that, I get a ClassCastException, because I cannot convert from String to Value... I tried changing the value inside the Value-class to String, but still I can't cast like this. So how could I achieve the same thing with Strings instead of Doubles or Integers ? – Phreneticus Aug 21 '17 at 08:34
  • Verify that your `TableModel` actually contains instances of `Value` and has an implementation of `getColumnClass()` that returns `Value.class`. – trashgod Aug 21 '17 at 09:30
  • Ah I Just forgot to Change my getValueAt method, now it works, thank you very much ! – Phreneticus Aug 21 '17 at 11:45
  • I can't select the checkboxes now though... which method do I have to alter, so that I can select the checkboxes and listen for changes in their true/false state ? – Phreneticus Aug 21 '17 at 12:04
  • Check your implementation of `isCellEditable()`. – trashgod Aug 21 '17 at 12:38
0

One way is to override the getColumnClass() method in your table model. See JavaDoc.

The other one is an own TableCellEditor and / or TableCellRenderer

Maia
  • 71
  • 1
  • 9
-1

Why not just create a new class which contains both a label and a checkbox and add that to your 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);
 }
}
Mackattack
  • 143
  • 2
  • 6
  • (1-) that is not how a JTable works. A JTable uses renderers to display the data. – camickr Aug 08 '17 at 14:42
  • @camickr Yes, maybe I should have made it clearer that you would add it through a renderer and not just assume that the OP would know how to. – Mackattack Aug 08 '17 at 14:55
  • 1
    A renderer won't help because it just paints the data. It is not a real component. It doesn't allow you to "click" on the check box. – camickr Aug 08 '17 at 20:09
  • @camickr I see! Then I had misunderstood what it did exactly. – Mackattack Aug 11 '17 at 09:52