设置自定义 UI 后 JComboBox 丢失键盘事件

发布于 2024-12-10 21:15:28 字数 1047 浏览 0 评论 0原文

我有以下问题:

我需要为 JComboBoxComponent 设置自定义 UI(以修改颜色、箭头按钮等)。目前,我正在构造函数中执行此操作,如下所示:

public MyComboBox() {
   setUI(new MyComboBoxUI);
}

问题是,以这种方式设置 UI 后,我以某种方式释放了组合框弹出窗口中列表的所有 InputMapActionMap 内容,即它不会使用箭头键向上或向下滚动列表。

我在这里做错了什么?

这是代码:

public class CurrencyPairComboBox extends JComboBox { 

    public CurrencyPairComboBox() {
        setUI(new CurrencyPairComboBoxUI());
    }
}

class CurrencyPairComboBoxUI extends BasicComboBoxUI {

    @Override
    public void installUI(JComponent c) {
       super.installUI(c);

       listBox.setSelectionBackground(Color.BLACK);
       listBox.setSelectionForeground(Color.WHITE);
    }

    @Override
    protected JButton createArrowButton() {
       arrowButton = new JButton();
       arrowButton.setIcon(OrderWidgetUIConstants.DROPDOWN_ARROW_ICON);
       arrowButton.setRolloverIcon(OrderWidgetUIConstants.DROPDOWN_ARROW_HOVER_ICON);
       return arrowButton;
    }    
}

I have the following problem :

I need to set a custom UI for a JComboBoxComponent (to modify colors, arrow button etc.) Currently, I'm doing it in a constructor, like this :

public MyComboBox() {
   setUI(new MyComboBoxUI);
}

Problem is, after setting UI in such way, I somehow loose all InputMap and ActionMap contents for list in combo box popup, i.e. it doesn't scroll list up or down with arrow keys.

What am I doing wrong here?

Here's the code :

public class CurrencyPairComboBox extends JComboBox { 

    public CurrencyPairComboBox() {
        setUI(new CurrencyPairComboBoxUI());
    }
}

class CurrencyPairComboBoxUI extends BasicComboBoxUI {

    @Override
    public void installUI(JComponent c) {
       super.installUI(c);

       listBox.setSelectionBackground(Color.BLACK);
       listBox.setSelectionForeground(Color.WHITE);
    }

    @Override
    protected JButton createArrowButton() {
       arrowButton = new JButton();
       arrowButton.setIcon(OrderWidgetUIConstants.DROPDOWN_ARROW_ICON);
       arrowButton.setRolloverIcon(OrderWidgetUIConstants.DROPDOWN_ARROW_HOVER_ICON);
       return arrowButton;
    }    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱绻入梦 2024-12-17 21:15:28

我尝试了您在此处发布的代码,我没有看到任何键盘问题,一切都按我的预期工作

输入图像描述这里 在此处输入图像描述

import java.awt.*;
import javax.swing.*;

class ComboBoxTest extends JFrame {

    private static final long serialVersionUID = 1L;
    private JComboBox comboBox;
    private ImageIcon infoIcon = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon");
    private ImageIcon warnIcon = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon");

    ComboBoxTest() {
        String[] items = {"Item1", "Item2"};
        comboBox = new JComboBox(items);
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        c.add(comboBox);
        comboBox.setUI(new MyUI());
    }

    public JFrame getCurrentInstance() {
        return this;
    }

    public static void main(String[] args) {
        ComboBoxTest frame = new ComboBoxTest();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    class MyUI extends javax.swing.plaf.basic.BasicComboBoxUI {

        @Override
        protected JButton createArrowButton() {
            JButton btn = new JButton();
            btn.setIcon(infoIcon);
            btn.setRolloverIcon(warnIcon);
            return btn;
        }
    }
}

I tried code that you posted here, I didn't see any Keyboard issue(s), all works as I expected

enter image description here enter image description here

import java.awt.*;
import javax.swing.*;

class ComboBoxTest extends JFrame {

    private static final long serialVersionUID = 1L;
    private JComboBox comboBox;
    private ImageIcon infoIcon = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon");
    private ImageIcon warnIcon = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon");

    ComboBoxTest() {
        String[] items = {"Item1", "Item2"};
        comboBox = new JComboBox(items);
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        c.add(comboBox);
        comboBox.setUI(new MyUI());
    }

    public JFrame getCurrentInstance() {
        return this;
    }

    public static void main(String[] args) {
        ComboBoxTest frame = new ComboBoxTest();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    class MyUI extends javax.swing.plaf.basic.BasicComboBoxUI {

        @Override
        protected JButton createArrowButton() {
            JButton btn = new JButton();
            btn.setIcon(infoIcon);
            btn.setRolloverIcon(warnIcon);
            return btn;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文