JImmy
JImmy

Reputation: 11

DefaultTableCellRenderer doesn't detect Integer and Date

so in my old laptop (java version 1.8.0_292), the code was working. My new laptop (java version 15.0.3), the code was not working.

String[] arrColumnName = new String[]{"Name", "Sex", "Balance", "Date"};
Date date = new Date();

Object[][] data = {
    {"Alex", "Man", 15000, date},
    {"Beatrix", "Woman", 25000, date}};

This is the DefaultTableModel :

DefaultTableModel model = new DefaultTableModel() {
    @Override
    public Class getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 2: return Integer.class;
            case 3: return Date.class;
            default: return String.class;
        }
    }
};

This is the Code :

public table() {
    setPreferredSize(null);
    setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    setAutoscrolls(true);

    model.setDataVector(data, arrColumnName);
    setModel(model);
    
    TableCellRenderer tableRenderer = new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            System.out.println(column + " " + String.valueOf(value));
            
            if(value instanceof Integer) {
                System.out.println("value is Integer");
            } else if(value instanceof Date) {
                System.out.println("value is Date");
            } else {
                System.out.println("value is String");
            }
            
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); //To change body of generated methods, choose Tools | Templates.
        }
        
        
        
    };
    setDefaultRenderer(Object.class, tableRenderer);
}

I try to detect Integer and Date with this code and this is the result :

0 Alex

value is String

1 Man

value is String

0 Beatrix

value is String

1 Woman

value is String

As you can see, Integer and Date were not detected.

But, it was working fine, when I run the same code on my old laptop.

Please help. Sorry for my terrible english.

Best Regards,

Upvotes: 0

Views: 32

Answers (0)

Related Questions