【问题标题】:Background colour of combo box cell editor组合框单元格编辑器的背景颜色
【发布时间】:2014-04-25 16:08:48
【问题描述】:

在过去的几天里,我一直在努力寻找解决方案,这让我发疯了。我有一个表,我在其中将选择颜色设置为黄色。我还将单元格编辑器组件的背景设置为黄色,以便在编辑单元格时颜色保持不变。我通过重写 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});

这些都不起作用。有人可以指出我做错了什么吗?谢谢。

【问题讨论】:

  • 也许如果你有一些“准备运行”的代码,尝试找到解决方案以提供帮助会更容易!
  • 通常,在JTable 中调整单元格渲染的最佳方法是实现一个TableCellRenderer,它接收现有渲染器或子类希望使用的项目(JLabel,@ 987654328@, JComboBox) 然后应用您的任何自定义样式,然后从 getTableCellRendererComponent() 方法返回它。我不知道样式 JComboBoxes 的细节,因为我从来没有在 JTable 中修改它们,否则我会为您提供一些示例代码。
  • @user3245747 您可以将您现在拥有的内容发布为MCVE 吗?有组合框所在单元格的颜色和组合框中所选项目的颜色。我需要看看在哪里发生了什么。

标签: java jcombobox tablecelleditor


【解决方案1】:

您可能需要覆盖单元格渲染器。在您的 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);
    }
}

【讨论】:

  • 还是不行。这会更改弹出窗口的背景颜色,但不会更改所选项目框的背景。
  • 啊哈,所以你想同时改变或所选项目框的背景?
  • 更改所选项目框是我的首要任务。另一个会很好,但不是很重要。
【解决方案2】:

我设法找到了问题的解决方案,并将其发布在这里,以防有人遇到同样的问题。以下是代码:

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

}

}

【讨论】:

    猜你喜欢
    • 2018-12-27
    • 1970-01-01
    • 2012-12-05
    • 2015-08-30
    • 2022-12-12
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    相关资源
    最近更新 更多