Getting java.lang.ClassCastException when using "DefaultTableCellRenderer"

Answered

Hi, 
I am using DefaultTableCellRenderer to cast my JTable cells and I get java.lang.ClassCastException. My plugin works fine on all the versions before 2019.2 . I get this error only in IntelliJ IDEA Community 2019.2. Please find the code and the error from below. 
```((DefaultTableCellRenderer) scanResultsTable.getTableHeader().getDefaultRenderer()) 
.setHorizontalAlignment(SwingConstants.LEADING);```

Error: 
```java.lang.ClassCastException: com.intellij.ui.table.JBTable$JBTableHeader$2 cannot be cast to javax.swing.table.DefaultTableCellRenderer```

Can someone please help me figure out what cause this issue since it is only occurs in IntelliJ Community 2019.2 .

3 comments
Comment actions Permalink

Hi!
Could you explain what is your plugin supposed to do?
At first sight it looks like you are trying to fix the header alignment for left-to-right languages, aren't you? In this case I suppose we'd better fix it in platform.

0
Comment actions Permalink

Default renderer for JBTable headers was changed (and no longer can be cast to the `DefaultTableCellRenderer` or `JComponent`).

You can update your code like this (it should work with all IDE versions):

      TableCellRenderer renderer = table.getTableHeader().getDefaultRenderer();
     TableCellRenderer newRenderer = new TableCellRenderer() {
       @Override
       public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
         Component delegate = renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
         if (delegate instanceof JLabel) {
           ((JLabel)delegate).setHorizontalAlignment(SwingConstants.LEADING);
         }
         return delegate;
       }
     };
     table.getTableHeader().setDefaultRenderer(newRenderer);
2
Comment actions Permalink

Hi @Aleksey Pivovarov: Thanks for the reply. I will try this out.

0

Please sign in to leave a comment.