将propertychangelistener添加到jcombobox

qzwqbdag  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(290)

很难说出这里要问什么。这个问题模棱两可,含糊不清,不完整,过于宽泛,或者是修辞性的,不能以现在的形式得到合理的回答。有关澄清此问题以便重新打开的帮助,请访问帮助中心。
8年前关门了。
如何使用 PropertyChangeListenerJComboBox ? 当我用可编辑的 JComboBox ,因为我更改了组合框箭头按钮的图标,所以我立即得到了文本;关键的听众不再工作了。这是我尝试过的,但我不知道如何完成:

editor = (JTextComponent) jComboBox1.getEditor().getEditorComponent();

editor.addPropertyChangeListener(new PropertyChangeListener()
{
    @Override
    public void propertyChange(PropertyChangeEvent evt) 
    {

    }       

});
cedebl8k

cedebl8k1#

要理解你想从你的文章中获得什么是有点困难的,但是如果你有兴趣知道什么时候编辑的内容被改变了,你可以试试

final JComboBox combo = new JComboBox();
combo.setEditable(true);
((JTextComponent) combo.getEditor().getEditorComponent()).getDocument().addDocumentListener(new DocumentListener() {
    protected void updatePopup() {

        if (combo.isDisplayable()) {

            if (!combo.isPopupVisible()) {

                combo.showPopup();

            }

        }

    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        updatePopup();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        updatePopup();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        updatePopup();
    }
});

通常,我会创建一个“documenthandler”作为一个具体的类并使用它,但是这个示例演示了基本思想
使用ui示例更新

public class TestComboBox extends JFrame {

    public TestComboBox() {

        setTitle("Test");
        setSize(200, 200);
        setLayout(new GridBagLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        final JComboBox combo = new JComboBox();

        /****You have to do this first
       ****Doing this invalidates any previous listeners
       ****/
        combo.setUI(ColorArrowUI.createUI(combo));

        combo.setEditable(true);
        ((JTextComponent) combo.getEditor().getEditorComponent()).getDocument().addDocumentListener(new DocumentListener() {
            protected void updatePopup() {

                if (combo.isDisplayable()) {

                    if (!combo.isPopupVisible()) {

                        combo.showPopup();

                    }

                }

            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                updatePopup();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updatePopup();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updatePopup();
            }
        });

        combo.setModel(createComboBoxModel());

        add(combo);

        setVisible(true);

    }

    protected ComboBoxModel createComboBoxModel() {

        DefaultComboBoxModel model = new DefaultComboBoxModel();

        File file = new File("../TestWords/Words.txt");
        BufferedReader reader = null;

        try {

            reader = new BufferedReader(new FileReader(file));
            String text = null;
            while ((text = reader.readLine()) != null) {

                model.addElement(text);

            }

        } catch (Exception e) {
        } finally {

            try {
                reader.close();
            } catch (Exception e) {
            }

        }

        return model;

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        }
        new TestComboBox();
    }

    public static class ColorArrowUI extends BasicComboBoxUI {

        public static ComboBoxUI createUI(JComponent c) {
            return new ColorArrowUI();
        }

        @Override
        protected JButton createArrowButton() {
            return new BasicArrowButton(
                            BasicArrowButton.SOUTH,
                            Color.cyan, Color.magenta,
                            Color.yellow, Color.blue);
        }
    }
}

使用ui设置

使用画师设置

更新
这是kleopatra给你看的密码

Painter painter = new Painter<JComponent>() {

    @Override
    public void paint(Graphics2D g, JComponent object, int width, int height) {

        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);

    }

};

JButton org = null;
for (int i = 0; i < combo.getComponentCount(); i++) {
    if (combo.getComponent(i) instanceof JButton) {
        org = (JButton) combo.getComponent(i);
        UIDefaults buttonDefaults = new UIDefaults();
        buttonDefaults.put("ComboBox:\"ComboBox.arrowButton\"[Enabled].foregroundPainter", painter);
        org.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
        org.putClientProperty("Nimbus.Overrides", buttonDefaults);
        break;
    }
}

相关问题