Note that there are some explanatory texts on larger screens.

plurals
  1. POJPanel as TableCellEditor disappearing
    primarykey
    data
    text
    <p>I've made a custom TableCellRenderer that displays a JPanel. When the JPanel contains interactive elements I want those to work too, so I made a custom TableCellEditor. It works, but there is some weird behavior when clicking through the JPanels. Sometimes a JPanel disappears when clicked. When I only set a custom renderer there are no problems at all, except for the JPanels not being interactive. So the problem must be in the TableCellEditor.</p> <p>Editor:</p> <pre><code>public class PanelTableCellEditor extends AbstractCellEditor implements TableCellEditor { private Object _component; @Override public Object getCellEditorValue() { return _component; } @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { _component = value; if (value instanceof JPanel) { ((Component) value).setForeground(UIManager.getColor("List.selectionForeground")); ((Component) value).setBackground(UIManager.getColor("List.selectionBackground")); return ((Component) value); } else { return null; } } } </code></pre> <p>Renderer:</p> <pre><code>public class PanelTableCellRenderer extends Component implements TableCellRenderer { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (value instanceof JPanel) { if (isSelected || hasFocus) { ((Component) value).setForeground(UIManager.getColor("List.selectionForeground")); ((Component) value).setBackground(UIManager.getColor("List.selectionBackground")); } else { ((Component) value).setForeground(UIManager.getColor("Panel.foreground")); ((Component) value).setBackground(UIManager.getColor("Panel.background")); } return ((Component) value); } else { return new DefaultTableCellRenderer().getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); } } } </code></pre> <p>In the main form I have the next code:</p> <pre><code>DefaultTableModel model = new DefaultTableModel(); model.addColumn(null, new Object[] { jPanel1, jPanel2, jPanel1, jPanel2, jPanel1, jPanel2, jPanel1, jPanel2, jPanel1, jPanel2, jPanel1 }); jTable1.setModel(model); TableColumn column = jTable1.getColumnModel().getColumn(0); column.setCellRenderer(new PanelTableCellRenderer()); column.setCellEditor(new PanelTableCellEditor()); jTable1.setRowHeight(50); jTable1.setTableHeader(null); jScrollPane2.setColumnHeaderView(null); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload