addItem to ComboBox without affect the content of ComboBox's Editor

currently, i would like to add new item into the ComboBox drop down list, while I am typing. However, when I call addItem in the keyPressed event listener of my ComboBox's editor, my previous typed content will always be replaced by my added items.

how can i prevent this behavior? i just like to preserve my previous typed content.

this.jComboBox1.getEditor().getEditorComponent().addKeyListener(getjComboBox1KeyListerner());

private KeyAdapter getjComboBox1KeyListerner(){

returnnew KeyAdapter(){

publicvoid keyReleased(KeyEvent e){

System.out.println("key released");

jComboBox1.addItem("123");

// My ComboBox editor always displayed 123. No use if I setSelectedIndex to -1,

// The editor will become empty. I just like to preserve my previous typed content

// jComboBox1.setSelectedIndex(-1);

}

};

}

[1359 byte] By [KwangHooia] at [2007-11-27 2:45:27]
# 1

Override the keyTyped() event instead of keyReleased().

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 3:13:01 > top of Java-index,Desktop,Core GUI APIs...
# 2

Everytime I type something, they will be replaced with my newly added "hello". How I can preserve what I type previously, at the same time add new item into combo drop down list box?

Thanks

/*

* NewJFrame.java

*

* Created on April 29, 2007, 11:25 PM

*/

package javaapplication6;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

/**

*

* @author doraemon

*/

public class Main extends javax.swing.JFrame {

/** Creates new form NewJFrame */

public Main() {

initComponents();

this.jComboBox1.getEditor().getEditorComponent().addKeyListener(this.getKeyListener());

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

buttonGroup1 = new javax.swing.ButtonGroup();

jComboBox1 = new javax.swing.JComboBox();

jMenuBar1 = new javax.swing.JMenuBar();

jMenu1 = new javax.swing.JMenu();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jComboBox1.setEditable(true);

jMenu1.setText("Menu");

jMenuBar1.add(jMenu1);

setJMenuBar(jMenuBar1);

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(132, 132, 132)

.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap(145, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(35, 35, 35)

.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap(224, Short.MAX_VALUE))

);

pack();

}// </editor-fold>

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Main().setVisible(true);

}

});

}

private java.awt.event.KeyListener getKeyListener() {

return new KeyAdapter() {

public void keyPressed(KeyEvent evt) {

jComboBox1.addItem("hello");

}

};

}

// Variables declaration - do not modify

private javax.swing.ButtonGroup buttonGroup1;

private javax.swing.JComboBox jComboBox1;

private javax.swing.JMenu jMenu1;

private javax.swing.JMenuBar jMenuBar1;

// End of variables declaration

}

KwangHooia at 2007-7-12 3:13:01 > top of Java-index,Desktop,Core GUI APIs...
# 3
1) I already suggested you override the keyTyped() method. It works fine for me.2) When you post code don't use code from your IDE. GroupLayout is not standard until JDK6 and most people don't use JDK6 yet so most people won't be able to execute your code.
camickra at 2007-7-12 3:13:01 > top of Java-index,Desktop,Core GUI APIs...
# 4

Hi camickr,

That's strange. For my side, I handle the keyTyped event as well, but doesn't work on my side. I type 'a'. But everytime, "hello1a" will be displayed on the ComboBoxEditor.

package javaapplication6;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Test extends javax.swing.JFrame {

public Test() {

initComponents();

this.jComboBox1.getEditor().getEditorComponent().addKeyListener(this.getKeyListener());

}

private void initComponents() {

jComboBox1 = new javax.swing.JComboBox();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jComboBox1.setEditable(true);

this.getContentPane().add(jComboBox1);

pack();

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Test().setVisible(true);

}

});

}

private java.awt.event.KeyListener getKeyListener() {

return new KeyAdapter() {

public void keyTyped(KeyEvent evt) {

System.out.println("key typed!");

jComboBox1.addItem("hello " + (i++));

}

};

}

private int i = 0;

private javax.swing.ButtonGroup buttonGroup1;

private javax.swing.JComboBox jComboBox1;

private javax.swing.JMenu jMenu1;

private javax.swing.JMenuBar jMenuBar1;

}

KwangHooia at 2007-7-12 3:13:01 > top of Java-index,Desktop,Core GUI APIs...
# 5

I couldn't use your original code to test for the reason I mention above. In my test program my combo box already had some items in the combo box so it was working correctly.

With your newer version I have the same problem. The solution is to use the invokeLater() method to add the code to the end of the GUI Event Thread:

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

jComboBox1.addItem("hello " + (i++));

}

});

camickra at 2007-7-12 3:13:01 > top of Java-index,Desktop,Core GUI APIs...
# 6
Can you please post your complete code here. For me, I tested with invokeLater, I still encounter the same problem still :(
KwangHooia at 2007-7-12 3:13:01 > top of Java-index,Desktop,Core GUI APIs...
# 7

I run the program and type "1". Then I see "hello 0" displayed in the combo box editor and in the drop down. If I enter another character, the editor stays the same but the dropdown now has two values "hello 0" an "hello 1".

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Test5 extends javax.swing.JFrame {

public Test5() {

initComponents();

this.jComboBox1.getEditor().getEditorComponent().addKeyListener(this.getKeyListener());

}

private void initComponents() {

jComboBox1 = new javax.swing.JComboBox();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jComboBox1.setEditable(true);

this.getContentPane().add(jComboBox1);

pack();

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Test5().setVisible(true);

}

});

}

private java.awt.event.KeyListener getKeyListener() {

return new KeyAdapter() {

public void keyTyped(KeyEvent evt) {

System.out.println("key typed!");

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

jComboBox1.addItem("hello " + (i++));

}

});

}

};

}

private int i = 0;

private javax.swing.ButtonGroup buttonGroup1;

private javax.swing.JComboBox jComboBox1;

private javax.swing.JMenu jMenu1;

private javax.swing.JMenuBar jMenuBar1;

}

camickra at 2007-7-12 3:13:01 > top of Java-index,Desktop,Core GUI APIs...
# 8

Oh, sorry for miscommunication.

What I need for the ComboBox editor is to have a behavior same as JTextField. When I type "1", the text filed will display "1". When I type "good bye", JTextField will display "good bye"

At the same time, it was able to add in new item, which is independent from the content of JTextField, when the typing event occur.

KwangHooia at 2007-7-12 3:13:01 > top of Java-index,Desktop,Core GUI APIs...