I understand if I did not include the line
jComboBox1.addItem("Cause Text Field To Override");
When I type “A” in the JComboBox text box, “A” will appear in the JComboBox text box.

However, if I include the addItem code, the JComboBox text box will be overridden. (The lower bound also disappears, not sure why)

I want to add addItem and showPopup without overriding the content in the JCombBoBox text box. Can I find out how I can do this?
package javaapplication5;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class NewJDialog extends javax.swing.JDialog {
public NewJDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
jComboBox1.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
jComboBox1.addItem("Cause Text Field To Override");
jComboBox1.showPopup();
}
});
}
@SuppressWarnings("unchecked")
private void initComponents() {
jComboBox1 = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jComboBox1.setEditable(true);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Hello", "World", "Bye" }));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(124, 124, 124)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(92, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(76, 76, 76)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(204, Short.MAX_VALUE))
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
NewJDialog dialog = new NewJDialog(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
private javax.swing.JComboBox jComboBox1;
}
source
share