组合框细胞编辑器的背景颜色

时间:2014-04-24 13:02:37

标签: java jcombobox tablecelleditor

过去几天我一直试图为此寻找解决方案,这让我发疯。我有一个表格,我将选择颜色设置为黄色。我还将单元格编辑器组件的背景设置为黄色,以便在编辑单元格时颜色保持不变。我通过覆盖prepareEditor方法来做到这一点:

 @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;
 }

除了我将组合框指定为单元格编辑器的列外,所有列都可以正常工作。一旦我开始编辑该列中的单元格,背景就会变为白色。弹出菜单中的背景颜色为黄色,但所选值框中的背景颜色仍为白色。我尝试在组合框中添加焦点监听器,但我能够做的就是更改弹出项目的背景而不是所选项目的背景。我尝试将焦点侦听器添加到组合框本身:

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

以及编辑器组件:

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

并且这些都没有奏效。有人可以指出我做错了什么?感谢。

2 个答案:

答案 0 :(得分:0)

您可能需要覆盖单元格渲染器。在您的UI管理器中使用此功能,并根据您的喜好更改paintComponent方法。

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

答案 1 :(得分:0)

我设法找到问题的解决方案,我在这里发布,以防任何人面临同样的问题。以下是代码:

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

}

}