Reputation: 20359
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.
Editor:
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;
}
}
}
Renderer:
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);
}
}
}
In the main form I have the next 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);
Upvotes: 2
Views: 885
Reputation: 17648
There are two issues that I see here :
*Duplicate placed AWT/SWING components are a no-no ! *
1) Adding the same component twice to a panel causes issues. Swing doesn't like the same component to exist in different locations --- the state of the gui, dedrawing, etc, is all based on a model of one component - one location. I've had similar problems to yours in the past, where a component went blank because it was being added multiple times...
Missing variables ?
2) The variables jPanel1/jPanel2 are not defined anywhere in your code. I assume that this is not a problem in your actual code, though --- so maybe if you show those definitions, there could be other problems.
Upvotes: 1