0

how can I create a DefaultTable from my ArrayList:

public class FileModel extends AbstractTableModel implements TableModel{

    List<Object[]> data = new ArrayList<>();

    String titles[] = new String[] { "File Name", "Pages", "Media Box Height", "Media Box Width", "Trim Box Height",
            "Trim Box Width", "Path", "Error" };
    Class<?> types[] = new Class[] { String.class, Integer.class, Integer.class, Integer.class, Integer.class,  Integer.class, String.class, String.class };

I want to use it for a color renderer, because at the moment the renderer just colors "Strings"... The DefaultModel should look like:

DefaultTableModel model = new DefaultTableModel(data, columnNames) {
            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };

My current Colorrenderer looks so:

    public class ColorRenderer extends DefaultTableCellRenderer {

    static final long serialVersionUID = 1L;
    final int STATUS_COL = 7;

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int col) {

        Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
        String type = (String) table.getModel().getValueAt(row, 7);
        if ("Error" == type) {
            component.setBackground(Color.RED);
            component.setForeground(Color.WHITE);

        } else if (isSelected) {
            component.setBackground(table.getSelectionBackground());
            component.setForeground(table.getSelectionForeground());

        } else {
            component.setBackground(table.getBackground());
            component.setForeground(table.getForeground());
        }

        return component;

    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720

1 Answers1

1

Just few hints that hopefully will help you to solve your problem.

1) A common mistake is asking the model for the first row values to retrieve the column class. That is simply wrong. What if the model is empty? We'll get a NullPointerException. Since we know the model (we are modeling it) we are perfectly capable to return the appropriate class. For example:

DefaultTableModel model = new DefaultTableModel(data, columnNames) {
    @Override
    public Class<?> getColumnClass(int column) {
        // return getValueAt(0, column).getClass(); NO! Never do we have to do this.
        switch (column) {
            case 0: return String.class;
            case 1: return Integer.class;
            ...
            case 7: return String.class;
                default: throw new ArrayIndexOutOfBoundsException(column);
    }
};

2) In ColorRenderer class there's a mistake comparing strings:

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
    ...
    if ("Error" == type) { // This should be "Error".equals(type)
        ...
    }
    ...
}

See How do I compare strings in Java?

Besides, row variable represents the row index in the view not the model so this line:

String type = (String) table.getModel().getValueAt(row, 7);

Might not return the expected value if the table is sorted. You need to convert the index like this:

int modelIndex = table.convertRowIndexToModel(row);
String type = (String) table.getModel().getValueAt(modelIndex, 7);

3) About this question: "how can I create a DefaultTable from my ArrayList:"

If you want to implement your own table model instead of using DefaultTableModel, see the options examined in this Q&A.

Edit

The only think is that only Strings are colored...But I have 4 Columns with Integers, they still keep in table background color.

The answer to this depends on how do you set the default renderer but I suspect you are doing this:

table.setDefaultRenderer(Object.class, new ColorRenderer());

Which works for String class but not for Integer class because the second one has a default renderer supplied by the table and has to be explicitely replaced, while the first one doesn't. For a complete and better explanation about renderer/editors selection see Concepts: Editors and Renderers. For practical purposes just do this:

TableCellRenderer renderer = new ColorRenderer();
table.setDefaultRenderer(Number.class, renderer); // This will work for Integer, Double, Float...
table.setDefaultRenderer(Object.class, renderer);
Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • thank you very much. I've implemented the second point of your solution, so I didn't need the first anymore. It works perfectly now. The only think is that only **Strings** are colored...But I have 4 Columns with **Integers**, they still keep in table background color -.- –  Oct 27 '14 at 08:07
  • wow this is awesome! thank you :-) Now I'm really a step further –  Oct 27 '14 at 11:52
  • You are welcome :) If this answer is good enough for you then don't forget to [accept it](http://stackoverflow.com/help/accepted-answer). @okaisan1 – dic19 Oct 27 '14 at 11:55