Problem:
Update:
From the Java SE 6 API:
public JComboBox () Creates a JComboBox with the default data model. By default, the data model is an empty list of objects. Use addItem to add items. By default, the first model data item will be selected.
So, I switched to JComboBox (model) as the API says:
public JComboBox (ComboBoxModel aModel) Creates a JComboBox that accepts elements from an existing ComboBoxModel. Since a ComboBoxModel is provided, the combo box created using this constructor does not create a default combo box model and can affect the insert, delete, and add methods.
I tried the following:
DefaultComboBoxModel model = new DefaultComboBoxModel(); model.setSelectedItem(null); suggestionComboBox = new JComboBox(model); suggestionComboBox.setModel(model);
But failed to get it to work, the first item is still selected.
Anyone who can come up with a working example will be very grateful.
The old part of the message:
I am using JComboBox and trying to use setSelectionIndex(-1) in my code (this code is placed in caretInvoke() )
suggestionComboBox.removeAllItems(); for (int i = 0; i < suggestions.length; i++) { suggestionComboBox.addItem(suggestions[i]); } suggestionComboBox.setSelectedIndex(-1); suggestionComboBox.setEnabled(true);
This is the initial setting when it was added to the panel:
suggestionComboBox = new JComboBox(); suggestionComboBox.setEditable(false); suggestionComboBox.setPreferredSize(new Dimension(25, 25)); suggestionComboBox.addActionListener(new SuggestionComboBoxListener());
When caretInvoke starts ComboBox initialization, even before the user selects the item, actionPerformed is already running (I tried JOptionPane here): http://i126.photobucket.com/albums/p109/eXPeri3nc3/StackOverflow/combo1.png http: // i126.photobucket.com/albums/p109/eXPeri3nc3/StackOverflow/combo2.png http://i126.photobucket.com/albums/p109/eXPeri3nc3/StackOverflow/combo3.png
The problem is that my program automatically inserts the selected text when the user selects an item from ComboBox. Therefore, without selecting anything, it is automatically inserted already.
How can I solve the problem in this situation? Thanks.
Here is my SSCCE: (finally)
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.JComboBox; 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 JComboBox suggestionComboBox; private JPanel suggestionPanel; private JLabel suggestionLabel; private JButton openButton, saveButton, aboutButton; public Temp() { super("Snort Ruleset IDE");