Search code examples
javajcomboboxtablecelleditor

Background colour of combo box cell editor


I have been trying to find a solution for this for the past few days and its driving me crazy. I have a table in which I set the selection colour to yellow. I also set the background of the cell editor component to yellow so that the colour remains the same when the cell is being edited. I do that by overriding the prepareEditor method as such:

 @Override
 public Component prepareEditor(TableCellEditor editor, int row, int col) {
      Component c = super.prepareEditor(editor, row, col);
      c.setBackground(Color.YELLOW);
      c.setFont(myFont);
      return c;
 }

This is working fine for all columns except for a column in which I assign a combo box as the cell editor. Once I start editing a cell in that column the background becomes white. The background colour in the popup menu is Yellow but the background colour in the selected value box remains white. I tried adding a focus listener to the combo box but all that I was able to do was change the background of the popup items and not the background of the selected item. I tried adding the focus listener to the combo box itself as such:

 myComboBox.addFocusListener(new FocusListener() {//code here});

and to the editor component as such:

myComboBox.getEditor().getEditorComponent().addFocusListener(new FocusListener() {//code    here});

and none of these worked. Can someone please point out what Im doing wrong? Thanks.


Solution

  • I managed to find a solution to the problem and Im posting it here in case anyone faces the same problem. the following is the code:

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import javax.swing.BorderFactory;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.table.TableCellEditor;
    import javax.swing.table.TableColumn;
    import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
    import org.jdesktop.swingx.autocomplete.ComboBoxCellEditor;
    
    public class ComboTest {
    
    JTable table;
    JComboBox comboBox;
    
    ComboTest(){
        String[] headings = {"Type", "Reference Number", "Amount"};
        Object[][] data = {
                {"Cash", "123", "2000"},
                {"Online", "333", "1200"},
                {"Bank Transfer", "667", "800"}
        };
        comboBox = new JComboBox(new String[] {"Cash", "Cheque", "Bank Transfer", "Credit Card", "Online"});
        AutoCompleteDecorator.decorate(comboBox);       
        JFrame jfrm = new JFrame("Example");
        jfrm.getContentPane().setLayout(new FlowLayout());
        jfrm.setSize(500, 160);
        table = new JTable(data, headings);
        table.setSelectionBackground(Color.GREEN);      
        TableColumn ledgerColumn = table.getColumnModel().getColumn(0);
        ledgerColumn.setCellEditor(new ComboBoxCellEditor(comboBox));
    //This is the code that changes the colour of the combo Box when it is selected.
        comboBox.getEditor().getEditorComponent().addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent arg0) {
                comboBox.getEditor().getEditorComponent().setBackground(Color.GREEN);
            }
            public void focusLost(FocusEvent arg0) {
                comboBox.getEditor().getEditorComponent().setBackground(Color.WHITE);
            }
        });
        JScrollPane jscrlp = new JScrollPane(table);
        jfrm.getContentPane().add(jscrlp);
        jfrm.setVisible(true);
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable () {
            public void run() {
                new ComboTest();
            }
        });
    
    }
    
    }