Java – how to configure jcombobox not to select the first element when it is created?
•
Java
Question: @ h_ 403_ 2 @ update:
DefaultCombo@R_754_2419@Model model = new DefaultCombo@R_754_2419@Model(); model.setSelectedItem(null); suggestionCombo@R_754_2419@ = new JCombo@R_754_2419@(model); suggestionCombo@R_754_2419@.setModel(model);
suggestionCombo@R_754_2419@.removeAllItems(); for (int i = 0; i < suggestions.length; i++) { suggestionCombo@R_754_2419@.addItem(suggestions[i]); } suggestionCombo@R_754_2419@.setSelectedIndex(-1); suggestionCombo@R_754_2419@.setEnabled(true);
suggestionCombo@R_754_2419@ = new JCombo@R_754_2419@(); suggestionCombo@R_754_2419@.setEditable(false); suggestionCombo@R_754_2419@.setPreferredSize(new Dimension(25,25)); suggestionCombo@R_754_2419@.addActionListener(new SuggestionCombo@R_754_2419@Listener());
package components; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Insets; import java.awt.Rectangle; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JButton; import javax.swing.JCombo@R_754_2419@; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTextArea; import javax.swing.JTextPane; import javax.swing.JToolBar; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.text.AbstractDocument; import javax.swing.text.BadLocationException; import javax.swing.text.StyledDocument; public class Temp extends JFrame { JTextPane textPane; AbstractDocument doc; JTextArea changeLog; String newline = "\n"; private JCombo@R_754_2419@ suggestionCombo@R_754_2419@; private JPanel suggestionPanel; private JLabel suggestionLabel; private JButton openButton,saveButton,aboutButton; public Temp() { super("Snort Ruleset IDE"); //Create the text pane and configure it. textPane = new JTextPane(); textPane.setCaretPosition(0); textPane.setMargin(new Insets(5,5,5)); StyledDocument styledDoc = textPane.getStyledDocument(); if (styledDoc instanceof AbstractDocument) { doc = (AbstractDocument) styledDoc; //doc.setDocumentFilter(new DocumentSizeFilter(MAX_CHARACTERS)); } else { System.err.println("Text pane's document isn't an AbstractDocument!"); System.exit(-1); } JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setPreferredSize(new Dimension(700,350)); //Create the text area for the status log and configure it. //changeLog = new JTextArea(10,30); //changeLog.setEditable(false); //JScrollPane scrollPaneForLog = new JScrollPane(changeLog); //Create a JPanel for the suggestion area suggestionPanel = new JPanel(new BorderLayout()); suggestionPanel.setVisible(true); suggestionLabel = new JLabel("Suggestion is not active at the moment."); suggestionLabel.setPreferredSize(new Dimension(100,50)); suggestionLabel.setMaximumSize(new Dimension(100,50)); suggestionCombo@R_754_2419@ = new JCombo@R_754_2419@(); suggestionCombo@R_754_2419@.setEditable(false); suggestionCombo@R_754_2419@.setPreferredSize(new Dimension(25,25)); //suggestionCombo@R_754_2419@.addActionListener(new SuggestionCombo@R_754_2419@Listener()); suggestionCombo@R_754_2419@.addItemListener(new SuggestionCombo@R_754_2419@Listener()); //suggestionCombo@R_754_2419@.setSelectedIndex(-1); //add the suggestionLabel and suggestionCombo@R_754_2419@ to pane suggestionPanel.add(suggestionLabel,BorderLayout.CENTER); suggestionPanel.add(suggestionCombo@R_754_2419@,BorderLayout.PAGE_END); JScrollPane sp = new JScrollPane(suggestionPanel); JScrollPane scrollPaneForSuggestion = new JScrollPane(suggestionPanel); //Create a split pane for the change log and the text area. JSplitPane splitPane = new JSplitPane( JSplitPane.VERTICAL_SPLIT,scrollPane,scrollPaneForSuggestion); splitPane.setOneTouchExpandable(true); splitPane.setResizeWeight(1.0); //Disables the moving of divider splitPane.setEnabled(false); //splitPane.setDividerLocation(splitPane.getHeight()); //splitPane.setPreferredSize(new Dimension(640,400)); //Create the status area. JPanel statusPane = new JPanel(new GridLayout(1,1)); CaretListenerLabel caretListenerLabel = new CaretListenerLabel("Status: Ready"); statusPane.add(caretListenerLabel); //Create the toolbar JToolBar toolBar = new JToolBar(); toolBar.setFloatable(false); toolBar.setRollover(true); openButton = new JButton("Open Snort Ruleset"); toolBar.add(openButton); saveButton = new JButton("Save Ruleset"); toolBar.add(saveButton); toolBar.addSeparator(); aboutButton = new JButton("About"); toolBar.add(aboutButton); //Add the components. getContentPane().add(toolBar,BorderLayout.PAGE_START); getContentPane().add(splitPane,BorderLayout.CENTER); getContentPane().add(statusPane,BorderLayout.PAGE_END); JMenu editMenu = createEditMenu(); JMenu styleMenu = createStyleMenu(); JMenuBar mb = new JMenuBar(); mb.add(editMenu); mb.add(styleMenu); setJMenuBar(mb); //Put the initial text into the text pane. //initDocument(); textPane.setCaretPosition(0); //Start watching for undoable edits and caret changes. textPane.addCaretListener(caretListenerLabel); SwingUtilities.invokelater(new Runnable() { public void run() { textPane.requestFocusInWindow(); } }); } //This listens for and reports caret movements. protected class CaretListenerLabel extends JLabel implements CaretListener { public CaretListenerLabel(String label) { super(label); } //Might not be invoked from the event dispatch thread. public void caretUpdate(CaretEvent e) { caretInvoke(e.getDot(),e.getMark()); } protected void caretInvoke(final int dot,final int mark) { SwingUtilities.invokelater(new Runnable() { public void run() { try { Rectangle caretCoords = textPane.modelToView(dot); //Find suggestion suggestionCombo@R_754_2419@.removeAllItems(); for (int i = 0; i < 5; i++) { suggestionCombo@R_754_2419@.addItem(Integer.toString(i)); } //suggestionCombo@R_754_2419@.setSelectedItem(null); suggestionCombo@R_754_2419@.setEnabled(true); suggestionLabel.setText("The following keywords are normally used as well. Click to use keyword(s). "); //changeLog.setText("The following keywords are suggested to be used together: " + str); } catch (BadLocationException ble) { setText("caret: text position: " + dot + newline); System.out.println("Bad Location Exception"); } } }); } } public class SuggestionCombo@R_754_2419@Listener implements ItemListener { //public void actionPerformed(ActionEvent e) { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { JCombo@R_754_2419@ cb = (JCombo@R_754_2419@)e.getSource(); String selection = (String) cb.getSelectedItem(); JOptionPane.showMessageDialog(null,"Item is selected","Information",JOptionPane.INFORMATION_MESSAGE); } } } /* * Menu Creation */ //Create the edit menu. protected JMenu createEditMenu() { JMenu menu = new JMenu("Edit"); return menu; } protected JMenu createStyleMenu() { JMenu menu = new JMenu("Style"); return menu; } private static void createAndShowGUI() { //Create and set up the window. final Temp frame = new Temp(); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } //The standard main method. public static void main(String[] args) { //Schedule a job for the event dispatch thread: //creating and showing this application's GUI. SwingUtilities.invokelater(new Runnable() { public void run() { //Turn off Metal's use of bold fonts UIManager.put("swing.boldMetal",Boolean.FALSE); createAndShowGUI(); } }); } }
Solution
Before making any changes to the combo box, you need to delete the itemlistener and add it back when you are finished@ H_ 403_ 2 @ 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.JCombo@R_754_2419@; 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 JCombo@R_754_2419@ suggestionCombo@R_754_2419@; private SuggestionCombo@R_754_2419@Listener selectionListener; public Suggestions() { frame = new JFrame("Snort Ruleset IDE"); textPane = new JTextPane(); textPane.setCaretPosition(0); textPane.setMargin(new Insets(5,5)); textPane.addCaretListener(new SuggestionCaretListener()); JScrollPane textEntryScrollPane = new JScrollPane(textPane); textEntryScrollPane.setPreferredSize(new Dimension(300,400)); selectionListener = new SuggestionCombo@R_754_2419@Listener(frame); suggestionCombo@R_754_2419@ = new JCombo@R_754_2419@(); suggestionCombo@R_754_2419@.setEditable(false); suggestionCombo@R_754_2419@.setPreferredSize(new Dimension(25,25)); suggestionCombo@R_754_2419@.addItemListener(selectionListener); JPanel suggestionPanel = new JPanel(new BorderLayout()); suggestionPanel.add(suggestionCombo@R_754_2419@,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 SuggestionCombo@R_754_2419@Listener implements ItemListener { Component parent; public SuggestionCombo@R_754_2419@Listener(Component parent) { this.parent = parent; } public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { JCombo@R_754_2419@ cb = (JCombo@R_754_2419@) e.getSource(); String selection = (String) cb.getSelectedItem(); JOptionPane.showMessageDialog(parent,"The selected item is: " + selection,JOptionPane.INFORMATION_MESSAGE); } } } void generateSuggestions() { suggestionCombo@R_754_2419@.removeItemListener(selectionListener); suggestionCombo@R_754_2419@.removeAllItems(); for (int i = 0; i < 5; i++) { suggestionCombo@R_754_2419@.addItem(Integer.toString(i)); } suggestionCombo@R_754_2419@.setEnabled(true); suggestionCombo@R_754_2419@.addItemListener(selectionListener); } public static void main(String[] args) { new Suggestions(); } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码