3

Possible Duplicate:
Set Jtable/Column Renderer for booleans

Right now I have in my Jtable:

enter image description here

but I want to have:

enter image description here

Therefore ,I guess, I have to covert my trues/false's into object like:

  Object rowData[][] = { { "1", Boolean.TRUE }, { "2", Boolean.TRUE }, { "3", Boolean.FALSE },
      { "4", Boolean.TRUE }, { "5", Boolean.FALSE }, }; 

Right now I get the data like this:

    int i=0;
    data=new Object[tupel.size()][1];
    while(i<tupel.size()){
        row=tupel.get(i);
        data[i][0]=new Boolean(row.isTrueorFalse());//my "Boolean method"
        i++;
    }
}

So my question is:

How to convert my data into Objects so that I have diplayed the ticks?

UPDATE

The isTrueorFalse Method:

public boolean isTrueorFalse() {
        return isTrueorFalse;
}
Community
  • 1
  • 1
maximus
  • 11,264
  • 30
  • 93
  • 124

4 Answers4

5

JTable has built_in support for Boolean value, then renderer/editor shows JCheckBox

have to override column in the XxxTableModel with proper Column Class

@Override
public Class getColumnClass(int column) {
    return getValueAt(0, column).getClass();
}
  • for example

  • for better help sooner post an SSCCE demonstrated your issue, short, runnable, compilable. otherwise every answers here could be shots to the dark

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

If you are sure that you want data as array like that then you can do below. I am not sure that is what you require though.

data[i][0]= i;
data[i][1] = row.isTrueorFalse();

You can then set data in table model like below

tableModel.set((Integer)data[i][0], (Boolean) data[i][1]);
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • ok I understand, first i get the data, but then how do you convert it into an Object to get the ticks. Pls would you be so kind to explain it a little bit further? – maximus Oct 24 '12 at 05:58
  • @maximus you need to set values to tableModel first as index and second as boolean then it will display check boxes – Amit Deshpande Oct 24 '12 at 06:01
2
    data[i][0]=Boolean.valueOf(row.isTrueorFalse());

this won't create any new objects of Boolean, just Boolean.TRUE or Boolean.FALSE.

irreputable
  • 44,725
  • 9
  • 65
  • 93
2

You should create your own Table Model.

Look for the following point:

If the programmer does not provide a table model object, JTable automatically creates an instance of DefaultTableModel.

And the following:

SimpleTableDemo's automatically created table model, on the other hand, does not know that the # of Years column contains numbers (which should generally be right aligned and have a particular format). It also does not know that the Vegetarian column contains boolean values, which can be represented by check boxes.

The construction should be something as follows:

JTable table = new JTable(new MyTableModel());

instead of using JTable(Object[][] rowData, Object[] columnNames) or JTable(Vector rowData, Vector columnNames). In this case it uses the DefaultTableModel, which is not intelligent enough to display your booleans as ticks (check boxes).

It also has a sample code to help you achieve what you are trying to achieve.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142