Question

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.

Was it helpful?

Solution 2

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();
        }
    });

}

}

OTHER TIPS

You probably need to override the cell renderer. Use this in your UI manager and change the paintComponent method to your liking.

public class MyComboBoxUI extends MetalComboBoxUI {

    public MyComboBoxUI() {

    }

    public static ComponentUI createUI(JComponent c) {

        return new MyComboBoxUI();
    }

    @Override
    public void installUI(JComponent c) {

        ListCellRenderer rend = new ListCellRenderer() {

            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

                final JLabel renderer = new JLabel(value.toString()) {

                    protected void paintComponent(java.awt.Graphics g) {

                        UIDefaults uid = UIManager.getDefaults();
                        Graphics2D g2d = (Graphics2D)g;
                        Dimension d = this.getSize();
                        g2d.setPaint(new GradientPaint(0, 0, Color.red, 0, d.height, Color.orange, true));
                        g2d.fillRect(0, 0, d.width, d.height);
                        super.paintComponent(g);
                    }
                };
                renderer.setOpaque(false);
                return renderer;
            }
        };
        super.installUI(c);
        ((JComboBox)c).setRenderer(rend);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top