Comment configurer JComboBox de ne pas sélectionner le PREMIER élément lors de sa création?

Problème:

Mise à jour:

À partir de la version Java SE 6 API:

public JComboBox() Crée un JComboBox
avec une valeur par défaut du modèle de données. La valeur par défaut
modèle de données est une liste vide
objets. Utiliser addItem pour ajouter des éléments. Par
par défaut le premier élément de données
le modèle est sélectionné.

Donc j'ai changé pour une JComboBox(modèle) que l'API dit:

public JComboBox(ComboBoxModel unmodèle)
Crée un JComboBox qui prend son
éléments à partir d'un existant ComboBoxModel.
Depuis le ComboBoxModel est fourni, un
combo créé à l'aide de cette
constructeur ne crée pas de valeur par défaut
de zone de liste déroulante modèle
et peut avoir un impact sur la façon dont le
insérer, supprimer et ajouter des méthodes se comportent.

J'ai essayé le suivant:

    DefaultComboBoxModel model = new DefaultComboBoxModel();
    model.setSelectedItem(null);
    suggestionComboBox = new JComboBox(model);
    suggestionComboBox.setModel(model);

Mais ne pouvait pas le faire fonctionner, le premier élément est toujours en cours de sélection.

Quelqu'un qui peut venir avec un exemple de travail serait très apprécié.


Partie ancienne de la poste:

Je suis en utilisant JComboBox, et essayé d'utiliser setSelectionIndex(-1) dans mon code (ce code est placé dans caretInvoke())

suggestionComboBox.removeAllItems();
    for (int i = 0; i < suggestions.length; i++) {
        suggestionComboBox.addItem(suggestions[i]);
    }
    suggestionComboBox.setSelectedIndex(-1);
    suggestionComboBox.setEnabled(true);

C'est le réglage initial lorsqu'il a été ajouté un volet:

    suggestionComboBox = new JComboBox();
    suggestionComboBox.setEditable(false);
    suggestionComboBox.setPreferredSize(new Dimension(25, 25));
    suggestionComboBox.addActionListener(new SuggestionComboBoxListener());

Lorsque le caretInvoke déclenche la zone de liste déroulante de l'initialisation, avant même que l'utilisateur sélectionne un élément, le actionPerformed est déjà déclenché (j'ai essayé un JOptionPane ici):
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

Le problème, c'est que Mon programme autoinserts le texte sélectionné lorsque l'utilisateur sélectionne un élément dans la liste déroulante. Donc, sans que l'utilisateur en sélectionnant n'importe quoi, il est automatiquement inséré déjà.

Comment puis-je surmonter le problème dans cette situation? Merci.

Voici mon SSCCE: (enfin)

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");
//Create the text pane and configure it.
textPane = new JTextPane();
textPane.setCaretPosition(0);
textPane.setMargin(new Insets(5, 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));
suggestionComboBox = new JComboBox();
suggestionComboBox.setEditable(false);
suggestionComboBox.setPreferredSize(new Dimension(25, 25));
//suggestionComboBox.addActionListener(new SuggestionComboBoxListener());
suggestionComboBox.addItemListener(new SuggestionComboBoxListener());
//suggestionComboBox.setSelectedIndex(-1);
//add the suggestionLabel and suggestionComboBox to pane
suggestionPanel.add(suggestionLabel, BorderLayout.CENTER);
suggestionPanel.add(suggestionComboBox, 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
suggestionComboBox.removeAllItems();
for (int i = 0; i < 5; i++) {
suggestionComboBox.addItem(Integer.toString(i));
}
//suggestionComboBox.setSelectedItem(null);
suggestionComboBox.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 SuggestionComboBoxListener implements ItemListener {
//public void actionPerformed(ActionEvent e) {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JComboBox cb = (JComboBox)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();
}
});
}
}
Ne publiez pas de photos. Publier vos SSCCE: sscce.org.
Je vais essayer, j'en ai trop sans rapport avec les codes.
SSCCE Posté. Merci
Comme mentionné ci-dessous, il n'est pas un SSCCE. Votre question est à propos d'une boîte combo et un champ de texte. Si vous avez seulement besoin d'un cadre avec ces deux composants. La barre de menu et la substance est indépendante et ne fait que compliquer l'exemple. L'idée d'un SSCCE est de repartir de zéro et de simplifier le code. Pendant que vous utilisez ce processus autant de fois que vous trouver votre erreur qui n'est pas évident lorsque l'on regarde de 1000 lignes de code. Aussi, notez comment vous avez obtenu une réponse de 10 minutes après que vous avez posté l'SSCCE qui montre l'intérêt d'en créer un pour chaque question.
A noté. Je dois donc supprimer tout jusqu'à ce que je atteindre quelque chose que affiche toujours l'erreur du programme? Je vois.

OriginalL'auteur Alex Cheng | 2010-05-04