First of all, apologies for the amount of questions about borders. I'm now having trouble getting the border for the JComboBox to leave.
The solution offered in response to the question Remove border from JComboBox did not work.
And explicitly removing the border before passing the JComboBox to the DefaultCellEditor
also didn't work.
Test class:
public class TableStackOf extends javax.swing.JFrame {
public TableStackOf() {
initComponents();
JComboBox editableCombobox = new JComboBox(new String[]{"Hello", "World"});
editableCombobox.setEditable(true);
editableCombobox.setBorder(BorderFactory.createEmptyBorder());
table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(editableCombobox));
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
table = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));
table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null}
},
new String [] {
"Title 1", "Title 2"
}
));
table.setGridColor(new java.awt.Color(218, 218, 218));
table.setRowHeight(20);
table.setSelectionBackground(new java.awt.Color(210, 225, 237));
table.setSelectionForeground(new java.awt.Color(0, 0, 0));
jScrollPane1.setViewportView(table);
getContentPane().add(jScrollPane1);
pack();
}
public static void main(String args[]) {
try {
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
java.awt.EventQueue.invokeLater(() -> {
new TableStackOf().setVisible(true);
});
}
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable table;
}
Also tried a PropertyChangeListener
but it didn't work:
PropertyChangeListener changeListener = (evt) -> {
JTable source = (JTable) evt.getSource();
if ("tableCellEditor".equals(evt.getPropertyName())) {
if (source.isEditing()) {
Component component = ((DefaultCellEditor) source.getCellEditor()).getComponent();
if (component instanceof JComboBox) {
JComboBox combobox = (JComboBox) component;
combobox.setBorder(BorderFactory.createEmptyBorder());
}
}
}
};
What's odd is that setting the border to editableCombobox.setBorder(BorderFactory.createLineBorder(Color.WHITE));
does work, but if I set the border to EmptyBorder, it goes back to the default 1px grey border for some reason.
It's also worth nothing that I'm using the Windows LaF.