0

I am using a jTable which is populated with mysql db data using rs2xml

table.setModel(DbUtils.resultSetToTableModel(rs));

I have some columns that are displayed by boolean values, but these must become checkboxes. I understand that i have to write my own AbstractTableModel, but I don't know how...

Can one of you give an example of how you extend the AbstractTableModel and use it in your code?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Matthijs
  • 145
  • 2
  • 13
  • Unless the underlying data type represents a Boolean (and assuming that rs2XML honors the database columns) there's little you can do. The better choice would be to use your own TableModel, which you can control, and parse the results from the ResultSet – MadProgrammer Sep 23 '15 at 08:58
  • Hi thanks for your reply, I have tried a mysql bit(1) datatype and a tinyint(1) datatype, mysql has no real boolean(true, false) datatype sadly enough... – Matthijs Sep 23 '15 at 09:08
  • Then I would assume that rs2xml either isn't honoring the column type or is simply ignoring it – MadProgrammer Sep 23 '15 at 09:15

1 Answers1

1

I have some columns that are displayed by boolean values, but these must become checkboxes.

Then you can override the getColumnClass(...) method of the JTable:

JTable table = new JTable(...)
{
    @Override
    public Class getColumnClass(int column)
    {
        for (int row = 0; row < getRowCount(); row++)
        {
            Object o = getValueAt(row, column);

            if (o != null)
            {
                return o.getClass();
            }
        }

        return Object.class;
    }
};

Or as suggested you can create your own TableModel. This is not difficult, again all you really need to do is implement the getColumnClass(...) method, but you need to write your own code to load the data into the TableModel.

See the TableFromDatabase.java example code found Table From Database for example code to replace your DbUtils class.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Yes thanks camickr, it works!!!, i made my own class for JTable, MyTable with your code and used it. I don't understand the code but it works... thanks a lot. – Matthijs Sep 23 '15 at 16:59