-1

How do I use PropertyChangeListener with JComboBox? When I write in editable JComboBox, I get the text written immediately because I changed the icon of the arrow button of the combobox; and key listeners aren't working any more. This is what I tried, but I don't know how to complete:

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

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

    }       

});
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Calm Sea
  • 181
  • 2
  • 4
  • 15

1 Answers1

3

It's a little difficult to understand what it is you're trying to achieve from your post, but if you're interested in knowing when the editor content is changed you could try

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

Normally, I'd create a "DocumentHandler" as a concrete class and use that instead, but the example demonstrates the basic idea

UPDATED with a UI example

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

Set using the UI

Set by UI

Set using the painter

Set By Painter

Updated

This is the code that kleopatra showed you

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;
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • See also this related [example](http://stackoverflow.com/a/8302423/230513). – trashgod Aug 25 '12 at 01:27
  • +1 @trashgod thanks , but i tried using Ducument Listener and it didn't work. – Calm Sea Aug 25 '12 at 19:24
  • +1 @MadProgrammer this is what i'm trying to do (see this link): http://stackoverflow.com/questions/12040248/jcombobox-popup-menue – Calm Sea Aug 25 '12 at 19:26
  • +1 i was suggested to use property change Listener to solve the problem, i searched about it , but didnt know how to use it or how it can solve the problem . any solution ???? – Calm Sea Aug 25 '12 at 19:36
  • @CalmSea, let me see if I can get this right. You want the popup to be displayed when ever the user starts typing something into the field yes? Did you try the example above? – MadProgrammer Aug 25 '12 at 21:26
  • @CalmSea I tried kleopatra's example without issue, I've tried Trashgods example without issue (although I had to make sure I set the listeners and UI in the correct order) – MadProgrammer Aug 25 '12 at 22:21
  • @CalmSea: Please edit your question to include your current working [sscce](http://sscce.org/). – trashgod Aug 26 '12 at 05:18
  • @MadProgrammer thanks a lot ,finally , the second picture is exatly what i want can you give me the code of it please (using painter) – Calm Sea Aug 26 '12 at 14:40
  • @CalmSea Sure, I literally copied from the post kleopatra did for you. – MadProgrammer Aug 26 '12 at 19:30