How to convert true to Boolean.TRUE?

Possible duplicate:
Set Jtable / Column Renderer for Boolean objects

Now I have in my Jtable:

enter image description here

but I want:

enter image description here

Therefore, I think I should hide my truths / lies in the object, for example:

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

Now I get the following data:

  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 to objects so that I have ticks?

UPDATE

IsTrueorFalse Method:

 public boolean isTrueorFalse() { return isTrueorFalse; } 
+4
source share
4 answers

JTable has built-in boolean support , then the visualizer / editor shows JCheckBox

need to redefine the column in XxxTableModel with the corresponding column class

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

  • for better help, publish SSCCE soon , demonstrating your problem, short, executable, compiled. otherwise each answer here may be a shot in the dark

+5
source

If you are sure that you want data as an array, then you can do it below. I'm not sure if this is what you need.

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

Then you can set the data in a tabular model as shown below

 tableModel.set((Integer)data[i][0], (Boolean) data[i][1]); 
+2
source
  data[i][0]=Boolean.valueOf(row.isTrueorFalse()); 

this will not create any new boolean objects, just Boolean.TRUE or Boolean.FALSE.

+2
source

You must create your own Model Table .

Find the following point:

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

And the following:

On the other hand, SimpleTableDemo automatically created a table model, it does not know that the # of Years column contains numbers (which usually should be right justified and have a certain format). He also does not know that a vegetarian column contains logical values ​​that can be represented by flags.

The design should be as follows:

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

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

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

+2
source

Source: https://habr.com/ru/post/1441728/


All Articles