如何配置JComboBox创建时不选择FIRST元素?

如何配置JComboBox创建时不选择FIRST元素?,第1张

如何配置JComboBox创建时不选择FIRST元素?

在对组合框进行任何更改之前,需要删除ItemListener并在完成后将其重新添加。

像这样:

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

顺便说一句,您发布的不是SSCCE,而是代码的转储。SSCCE应该仅具有足够的代码来重现您遇到的问题。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5490698.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存