How to configure JComboBox not to select FIRST element when created?

前端 未结 3 1554
眼角桃花
眼角桃花 2020-12-19 07:34

Problem:

Update:

From the Java SE 6 API:

public JComboBox() Creates a JComboBox with a default data model. The defa

相关标签:
3条回答
  • 2020-12-19 07:47

    use

    setSelectedItem(null);
    

    Please try with ItemListener instead of ActionListener.

    0 讨论(0)
  • 2020-12-19 07:53

    You need to remove the ItemListener before you make any changes to the combo-box and add it back when you are done.

    Something like this:

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Insets;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.SwingUtilities;
    import javax.swing.event.CaretEvent;
    import javax.swing.event.CaretListener;
    
    public class Suggestions {
    
        private JFrame frame;
        private JTextPane textPane;
        private JComboBox suggestionComboBox;
        private SuggestionComboBoxListener selectionListener;
    
        public Suggestions() {
            frame = new JFrame("Snort Ruleset IDE");
    
            textPane = new JTextPane();
            textPane.setCaretPosition(0);
            textPane.setMargin(new Insets(5, 5, 5, 5));
            textPane.addCaretListener(new SuggestionCaretListener());
            JScrollPane textEntryScrollPane = new JScrollPane(textPane);
            textEntryScrollPane.setPreferredSize(new Dimension(300, 400));
    
            selectionListener = new SuggestionComboBoxListener(frame);
            suggestionComboBox = new JComboBox();
            suggestionComboBox.setEditable(false);
            suggestionComboBox.setPreferredSize(new Dimension(25, 25));
            suggestionComboBox.addItemListener(selectionListener);
    
            JPanel suggestionPanel = new JPanel(new BorderLayout());
            suggestionPanel.add(suggestionComboBox, BorderLayout.PAGE_END);
    
            frame.getContentPane().add(textEntryScrollPane, BorderLayout.NORTH);
            frame.getContentPane().add(suggestionPanel, BorderLayout.SOUTH);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    
        private final class SuggestionCaretListener implements CaretListener {
            @Override
            public void caretUpdate(CaretEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        generateSuggestions();
                    }
                });
            }
        }
    
        public static final class SuggestionComboBoxListener implements ItemListener {
            Component parent;
            public SuggestionComboBoxListener(Component parent) {
                this.parent = parent;
            }
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                     JComboBox cb = (JComboBox) e.getSource();
                     String selection = (String) cb.getSelectedItem();
                     JOptionPane.showMessageDialog(parent, "The selected item is: " + selection, "Information",
                     JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
    
        void generateSuggestions() {
            suggestionComboBox.removeItemListener(selectionListener);
            suggestionComboBox.removeAllItems();
            for (int i = 0; i < 5; i++) {
                suggestionComboBox.addItem(Integer.toString(i));
            }
            suggestionComboBox.setEnabled(true);
            suggestionComboBox.addItemListener(selectionListener);
        }
    
        public static void main(String[] args) {
            new Suggestions();
        }
    }
    

    BTW, what you posted is not an SSCCE it is a dump of your code. An SSCCE should only have enough code to reproduce the issue you are experiencing.

    0 讨论(0)
  • 2020-12-19 08:02

    if You want that after 1st entry you made and immediately you combox is empty then just write down the under mentioned code which is:

    jComboBox1.setSelectedIndex(0);   
    

    and your combox will reset Automatically

    0 讨论(0)
提交回复
热议问题