ComboBox: support using as cell renderer (e.g. in JTable)

This commit is contained in:
Karl Tauber
2021-05-04 21:39:08 +02:00
parent 067501cbe7
commit cacf0ea987
4 changed files with 90 additions and 30 deletions

View File

@@ -134,6 +134,7 @@ public class FlatComponents2Test
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
cm.getColumn(2).setCellRenderer( new TestComboBoxTableCellRenderer() );
cm.getColumn(2).setCellEditor( new DefaultCellEditor( new JComboBox<>( months ) ) );
JComboBox<String> editableComboBox = new JComboBox<>( months );
editableComboBox.setEditable( true );
@@ -1219,4 +1220,23 @@ public class FlatComponents2Test
return this;
}
}
//---- class TestComboBoxTableCellRenderer --------------------------------
private static class TestComboBoxTableCellRenderer
extends JComboBox<String>
implements TableCellRenderer
{
@Override
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column )
{
setModel( new DefaultComboBoxModel<>( new String[] { String.valueOf( value ) } ) );
setBackground( isSelected ? table.getSelectionBackground() : table.getBackground() );
setForeground( isSelected ? table.getSelectionForeground() : table.getForeground() );
setBorder( null );
return this;
}
}
}