JComboBox.removeAllItems event in 1.4.2
I've just installed 1.4.2 and noticed that JComboBox.removeAllItems now fires an ActionEvent which it didn't do in 1.4.1. This was a little inconvenient for me as I was using this event to update another field when a new selection was made in the combo. The event on removeAllItems resulted in a new NullPointerException in my code. It was easily worked around but a little tricky to spot seeing as I wasn't expecting this behaviour.
Was this a mistake in previous versions to not fire this action or a mistake to add it to the new release? I though changes like this would be left to major releases only? Am I now going to have to check all my code for bugs like this before moving to 1.4.2?
cheers,
Andy
There are quite a few references to what happens with setSelectedItem (which causes an event) when removeAllItems is called in bug parade.
I've found combo boxes to be problematic too, but they got better with each release. After all, we all have a set of regression tests to be applied when upgrading, don't we?
Similar Messages
-
How to fire JComboBox itemStateChanged event manually?
hello:
I want to know how to fire JComboBox itemStateChanged event manually.
thank you
-DanielCall setSelectedIndex or setSelectedItem.
-
JDK1.6 JComboBox: No event sent on same selection
Hi,
In JDK1.5, with a combobox, when selecting from the dropdown list the item already selected, it sends an ActionEvent, but no ItemEvent.
In JDK1.6.0_01, no event ActionEvent is sent!
When the combobox is taken as a table cell editor, selecting the same item does not make automatically the cell quit the edition mode!, as the change is based on the ActionEvent listening!
We have to click somewhere else to make the edition stop!
Can someone explain this event strategy change between 1.5 and 1.6?
And how to make the combobox disappear from the table cell?
Just below a simple piece of code to check the combobox event sending.
Herve.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class ComboActionEvent extends JFrame {
private void run() {
JComboBox cb = new JComboBox(new String[]{"a", "b", "c", "d"});
cb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println ("Action event!");
getContentPane().add (cb);
pack();
setVisible(true);
public static void main (String[] arg) {
new ComboActionEvent().run();
}Hmmm
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6481977
and there are various threads in the forums about this, for example:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=5116064 -
JComboBox Action Event problems
I have a complex UI with which I am having action event issues. Basically, the panel displays info about an object, which can be selected from within the panel through a button/popup window mechanism. A JComboBox (#1) selection determines the contents of another JComboBox (#2) on the same panel. Currently an ActionEvent fires when the user makes a selection using #1 that updates #2. This fuctions correctly with repeated testing so long as a new object is not selected.
When a new object is selected from within this panel this ActionEvent no longer fires. JComboBox #1 will be updated correctly and #2 will reflect the initial selection in #1, but user selections in #1 will not fire the ActionEvent and thus #2 is never updated.
Any help is appreciated. I feel like I'm missing something basic here, but I've stared at it for long enough...By the way, the workaround is to use the ItemListener as the ItemStateChanged Event is fired reliably.
-
below is resource
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ComboBoxTest extends JFrame{
private JLabel sqlLabel = new JLabel("SQL:");
private JComboBox sqlComboBox = new JComboBox();
public ComboBoxTest() {
sqlComboBox.setEditable(true);
sqlComboBox.addItem("");
sqlComboBox.setBackground(Color.WHITE);
sqlComboBox.setPreferredSize(new Dimension(500,25));
sqlComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED){
if(sqlComboBox.getSelectedIndex()==-1){
String currentSQL=(String)event.getItem();
System.out.println("currentSQL : "+currentSQL);
sqlComboBox.addItem(currentSQL);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(sqlLabel);
cp.add(sqlComboBox);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
public static void main(String[] args){
new ComboBoxTest();
}my intent is that let user write sql in JComboBox,this program will send sql when user click enter,and JComboBox will use addItem() to add the sql to JComboBox,
problem is below:
when I clicked enter one more time,why just can arosed only itemStateChanged event,I hope this program would arose itemStateChanged event when user clicked enter everytime
another:
when user choosed the sql that has been used addItem() to add JComboBox;
why then clicked enter would not arose any itemStateChanged event1) I've answered several of your previous postings. Not once have you bothered to follow up the posting to confirm the suggestion worked, or didn't work. A simple Thank You goes a long way.
2) Several times I've also suggested that Swing related questions should be posted on the Swing forum. (This is where the Swing experts hang out).
3) I don't understand your question, so I can't help anyway. -
Distiinguish JComboBox editing event from selection event
In an editable JComboBox I need the event that the selection via popup has changed and not due to finish the editing of an entry.
For test I added listener for ItemStateChanged and ActionEvent. The sequence is
a) for selection change (from 'bbb' to 'aaa')
itemStateChanged: java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=bbb,stateChange=DESELECTED] on coFilter
itemStateChanged: java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=aaa,stateChange=SELECTED] on coFilter
actionPerformed : java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=comboBoxChanged,when=1129716754377,modifiers=Button1] on coFilter
b) after editing the current entry has been finished (from 'aaa' to 'aaa123')
itemStateChanged: java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=aaa,stateChange=DESELECTED] on coFilter
itemStateChanged: java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=aaa123,stateChange=SELECTED] on coFilter
actionPerformed : java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=comboBoxChanged,when=1129716711382,modifiers=] on coFilter
actionPerformed : java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=comboBoxEdited,when=1129716711382,modifiers=] on coFilter
Is there a simple way to catch the first situation only and not reacting for the second sequence?
Thanks in advance
Wolfgang R.In an editable JComboBox I need the event that the selection via popup has changed and not due to finish the editing of an entry.
For test I added listener for ItemStateChanged and ActionEvent. The sequence is
a) for selection change (from 'bbb' to 'aaa')
itemStateChanged: java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=bbb,stateChange=DESELECTED] on coFilter
itemStateChanged: java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=aaa,stateChange=SELECTED] on coFilter
actionPerformed : java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=comboBoxChanged,when=1129716754377,modifiers=Button1] on coFilter
b) after editing the current entry has been finished (from 'aaa' to 'aaa123')
itemStateChanged: java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=aaa,stateChange=DESELECTED] on coFilter
itemStateChanged: java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=aaa123,stateChange=SELECTED] on coFilter
actionPerformed : java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=comboBoxChanged,when=1129716711382,modifiers=] on coFilter
actionPerformed : java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=comboBoxEdited,when=1129716711382,modifiers=] on coFilter
Is there a simple way to catch the first situation only and not reacting for the second sequence?
Thanks in advance
Wolfgang R. -
JComboBox arrow event...?
Hello everyone:
I use JComboBox for list data from database, but some times these data are changed form another window(Panel) so I need to refresh this comboBox and fill it with the new data.
I did that but the problem is the event....!! I used the actionListener but this works just when I choose the item from the comboBox....!!
I need an event called when I click on the arrow of comboBox not when I choose item from the comboBox.....?
or if there is any event that I can add it to panel so when I switch to the panel that contain the comboBox I call the method that refresh the comboBox data on the event.....!!?
Please help meA simple example...
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ComboFill {
private static final String COMBO_PANEL = "Combo Panel";
private static final String TEXT_AREA_PANEL = "Text Area Panel";
private JPanel mainPanel = new JPanel();
private ComboPanel comboPanel = new ComboPanel();
private TextAreaPanel textAreaPanel = new TextAreaPanel();
private CardLayout cardlayout = new CardLayout();
private JPanel cardLayoutHolderPanel = new JPanel(cardlayout);
public ComboFill() {
JButton swapButton = new JButton("Swap Panels");
swapButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
swapBtnActionPerformed();
JPanel btnPanel = new JPanel();
btnPanel.add(swapButton);
cardLayoutHolderPanel.add(textAreaPanel.getPanel(), TEXT_AREA_PANEL);
cardLayoutHolderPanel.add(comboPanel.getPanel(), COMBO_PANEL);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(cardLayoutHolderPanel, BorderLayout.CENTER);
mainPanel.add(btnPanel, BorderLayout.SOUTH);
private void swapBtnActionPerformed() {
String text = textAreaPanel.getText();
String[] items = text.split("\n");
comboPanel.updateComboModel(items);
cardlayout.next(cardLayoutHolderPanel);
public JComponent getComponent() {
return mainPanel;
private static void createAndShowUI() {
JFrame frame = new JFrame("Combo Fill");
frame.getContentPane().add(new ComboFill().getComponent());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
class ComboPanel {
private JPanel panel = new JPanel();
private DefaultComboBoxModel model = new DefaultComboBoxModel();
private JComboBox combo = new JComboBox(model);
public ComboPanel() {
panel.add(combo);
public JPanel getPanel() {
return panel;
public void updateComboModel(String[] items) {
model.removeAllElements();
for (String item : items) {
model.addElement(item);
class TextAreaPanel {
private static final String[] ITEMS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private JPanel panel = new JPanel(new BorderLayout());
private JTextArea textArea = new JTextArea(20, 40);
public TextAreaPanel() {
for (String item : ITEMS) {
textArea.append(item + "\n");
panel.add(new JScrollPane(textArea), BorderLayout.CENTER);
public JPanel getPanel() {
return panel;
public String getText() {
return textArea.getText();
} -
I am working on a swing application in which i have to populate data in a jcombobox (onchange event)
from an .xml file .but the application gets hanged when the size of xml is big (350-400 records)Welcome to the Sun forums.
>
I am working on a swing application in which i have to populate data in a jcombobox (onchange event) from an .xml file .but the application gets hanged when the size of xml is big (350-400 records)>Just what are these 'records', great works of literature?!? I cannot believe that any Java based XML API (even a DOM based one) would suffer an OutOfMemoryError on an XML document that was any less than 350-400 thousand records, for any sensibly sized record.
But as a general tip. Some XML APIs - notably DOM based - will need to create a much larger object in memory, than for example, using SAX to parse XML.
If even a SAX based API is failing, it is possibly due to a memory leak in the code, rather than the actual size of the XML.
Edit 1:
Now that I reread BDLH's reply, I think it more closely addresses the actual problem (frozen GUI, not OOME). Even then, my query on the number of records stands, surely it cannot be less than 500 records that is 'freezing' the GUI?
Edited by: AndrewThompson64 on Apr 29, 2009 3:33 PM -
JComboBox event notification when select first entry in pop-up menu JDK 6
I recently began testing a Java GUI, I originally developed with JDK 1.5.0_11, with JDK 1.6.0_3. I have used the JComboBox in several applications developed using JDK 1.3.x and 1.4.x over the years. In every one of these earlier JDKs the JComboBox widgets all behaved the same. When you select the JComboBox widget, the pop-up menu appears and you can select any of the items from the menu list. Having made the selection with either a mouse click or a key press, an event notification is sent. Typically, an ActionEvent as well as either a mouse click event or a keypressed event may be sent. When testing with 1.6.0_x versions of the JDK and JRE, I can't account for any event notification being sent when selecting the first item at the top of the pop-up menu as the initial selection. If I select some other item on the list and then the first item, it works as it is supposed to work, but only after selecting another item first.
I've placed the JComboBox in a JDialog and a JPanel. There are NO AWT widgets in any containers of the application. The same behavior seems to exist regardless of what other containing Swing component I place the JComboBox in. I'm running these applications on Windows XP, service pack 2. The system is an AMD 64 bit dual processor with 4Gb of RAM. The essential code follows:
private JComboBox getJcboAlias()
/* Note here that I am using a defaultComboModel as I have always done */
jcboAliases = new JComboBox(urls);
*jcboAliases.setEditable(false);* // Note here that the JComboBox is NOT editable
jcboAliases.addActionListener(new ActionListener() // ActionListener only receives a notification if the second or
{ // another item in the pop-menu list is selected...but never the
public void actionPerformed(ActionEvent ae) // first item in the list
String s = (String) jcboAliases.getSelectedItem();
for (int i = 0; i < connections.size(); i++)
conAlias = (String[]) connections.get(i);
if (s.equals(conAlias))
isSelected = true;
break;
jlblName.setVisible(true);
jlblAlias.setVisible(true);
jlblUID.setVisible(true);
jlblPWD.setVisible(true);
jtxtName.setText(conAlias[0]);
jtxtName.setVisible(true);
jtxtAddress.setText(conAlias[1]);
jtxtAddress.setVisible(true);
jtxtUID.setText(conAlias[2]);
jtxtUID.setVisible(true);
jtxtPWD.setVisible(true);
jtxtPWD.setText("");
jtxtPWD.requestFocus();
return jcboAliases;
} I'm using a non-editable JComboBox because there is a pop-up menu with this and not with the JList widget. JComboBox behaves more like the JList MicroSoft counterpart. Another code snippet follows in which the JComboBox is editable. The same errant behavior occurs when selecting the first item from the pop-up menu: private JComboBox getJcboPCMember()
jcboPCMember = new JComboBox(PCViewerCustom.getCurrentMembers());
jcboPCMember.setBounds(250, 103, 180, 20);
jcboPCMember.setToolTipText("PATHCHECK(ER) member name - Example: CKPPTHCK");
jcboPCMember.setSelectedIndex(Integer.valueOf(PCViewerCustom.getCurrentHost(3)));
jcboPCMember.setEditable(true); // Note here that this JComboBox IS editable
jcboPCMember.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent ae)
if (ae.getActionCommand().equals("comboBoxEdited"))
boolean copy = false;
for (int i = 0; i < jcboPCMember.getItemCount(); i++)
if (jcboPCMember.getSelectedItem().equals(jcboPCMember.getItemAt(i)))
copy = true;
break;
if (!copy)
if (jcboPCMember.getItemCount() > 0)
if (jcboPCMember.getItemAt(0).equals("No Entries"))
jcboPCMember.removeItem("");
jcboPCMember.removeItem("No Entries");
jcboPCMember.insertItemAt((String) jcboPCMember.getSelectedItem(), 0);
else
jcboPCMember.removeItem("");
jcboPCMember.addItem((String) jcboPCMember.getSelectedItem());
enableJbtnOK();
else
if (jcboPCMember.getSelectedIndex() >= 0)
PCViewerCustom.setCurrentHost(3, Integer.toString(jcboPCMember.getSelectedIndex()));
enableJbtnOK();
else
JOptionPane.showMessageDialog(null, "Name not selected", "Error", JOptionPane.ERROR_MESSAGE);
return jcboPCMember;
I am able to add a new entry to the JComboBox, however, I am still unable to select the first item from the pop-up menu and fire any kind of notification event. I have not seen this behavior before in any earlier versions of the JDK and it's playing havoc with my ability to deploy the application into a JDK or JRE V6 environment, without adding a bunch of additional code to pre-select the first item on the list, which pretty much defeats the purpose of the JComboBox. I'll be the first to admit I've done something wrong, but i've built a number of test scenarios now on two systems runing Win XP SP2 and I get the same errant behavior. I've also added in event listeners for a MouseListerner, a KeyListener and an ItemListener. Still no event notification on this first item in the list. Again, however, if I select one of the other items from the pop-up menu list and then select the first item, all is well. Imagine selling that method of operation to a user.... It occurs to me that this must be a bug in the V6 JComboBox. I wanted to post this here first, however, in order to determine if this is, in fact, a bug - in other words, am I the only one seeing this, or is this a more widespread issue? Any assistance in making this determination is greatly appreciated.
David Baker
Edited by: Galstuk on Nov 27, 2007 12:21 PMhere is the other file as well:
Harold Timmis
[email protected]
Orlando,Fl
*Kudos always welcome
Attachments:
VI2.vi 5 KB -
Event to watch for JComboBox _finished_ selection?
When my app's JComboBoxes are clicked I can capture every
single change while the user is trying to decide with the
up arrow and down arrow keys. This allows some really neat
and dynamic things such as changing other components "live" or
in real time as the user navigates up and down. I make
extensive use of this Java event capture. The tutorial with
a little pig is a good example of that kind of event.
However, what I need additionally is to detect when the user has
made up her mind and the selecting process is finished. This
occurs for instance, when the <TAB> key is pressed and as a
result, the focus is transfered to the next component.
So, how can I detect not the fine grain event, up&down event,
but the higher level selection?
TIA,
-Ramon F. HerreraI would suggest adding an action listener to the combo box and seeing if that will capture the event you are looking for.
-
JComboBox: how to capture the internal list events?
Hi
Does anybody know how to capture JComboBox internal list events, what I mean
is when I click the box arrow button a list of the items pops up and as you move the mouse inside that list up and down, an items gets highlighted.
So I am interested in capturing those events - basically when the next item from the list is highlighted.
ThanksYou can add an item listener to the combo box for that. IIRC, your listener will get
notified twice: once for the deselection of the previous item, and once for the new
item's selection.
: jay -
How to handle editing events in a JComboBox
I have a editable JComboBox. User can select value either using the drop-down box or by editing.
I am able to get the drop-down selct event. However, I am not sure how should I handle the edit event on JComboBox.
Particularly, here is the use-case I wish to handle.
User edited the JComboBox by clicking back space key. I want to fire an event once user is done editing. How can I find if user is done editing ?User, please always tell us your jdev version.
In general you use a valueChangeListener which is fired when the user tabs out of the field...
Timo -
Event Handling of 2 JComboBoxes in 1 Frame
Hello,
thank you very much for your help in the following problem:
problem: Differntiating events of 2 JComboBoxes in 1 JFrame.
solution trial: differentiating via information of an Event-object ("list0","list1"). This solution works as stand-alone Frame, but integrating it in menu of another Frame the List-Information turns dependent on the frequency of clicking the JComboBoxes to "list2", "list3",...
Object o = Event.getSource();
String ComboInf = String.valueOf(o);
ComboInf=ComboInf.substring(14,19);//here the 2 ComboBoxes are differentiated
if(ComboInf.equals("list0")//only works for stand-alone Jframe
-->ComboBox1
if(ComboInf.equals("list1")//only works for stand-alone Jframe
-->ComboBox2Thank you very much again for your answer
BJoaCast the event source object to what you added the listener to, here JComboBox
JComboBox comboBox1 = new JComboBox();
public void actionPerformed(ActionEvent e)
JComboBox combo = (JComboBox)e.getSource();
if(combo == comboBox1)
doSomethingWith comboBox1();
if(combo == comboBox2)
} -
Event handling of JComboBox outside a JTable
Hi to All,
I've a question about JTables and JComboBoxes... I'm working on a application which holds a JTable and several JButtons and JCombBoxes. When the user clicks on a cell in the JTable, this cell will get the focus and the text will be highlighted. An action will be performed when the user clicks on a JButton (this just works fine). When the user clicks on a JComboBox also an action must be performed... but this won't work. I guess it won't work because of the JComboBox will draw a popup, and this popup will be cover the cell with the focus...
I could not catch any event of the JComboBox...
How can I check for a click on a JComboBox when an other component (JTable cell) has the focus?
Thnx,Hi Swetha,
Button controls in a table are available only in version 2.0 of the .NET PDK.
In order to use Button in a table and handle its events u will have to upgrade.
Further information about using input controls in a table is available in the How to section of the PDK documentation.
Thanks, Reshef -
JComboBox event confusion: not enough generated? :-(
Hi,
I am slightly confused about the events generated by the combobox.
I have a combobox and and actionlistener registered on it. If I programmatically use setSelectedIndex on the combobox, and event is generated and the actionlistener is triggered. If I do a setSelectedIndex on the same combobox within the code of the action listener, no event is generated, though the index does get updated. Though I am happy it doesn't generate that event, I don't understand why it doesn't. Anyone?
Rene'Instead of using ActionListener you should use ItemListener for combo box. This is an example
comboBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(ItemEvent e) {
try {
JComboBox box = (JComboBox) e.getSource();
System.out.println(box.getSelectedItem());
} catch (ArrayIndexOutOfBoundsException exp) {}
});Note: Item Listener only listens when you change something in Combo Box. If you click on a combo box, and selects the same value, item listeners doesn't call.
Hope this helps.
Maybe you are looking for
-
Hi , we have uploaded a logo using SE78. Is there anyway by which we can know the file path where this logo is uploaded in SAP?
-
I CANT GET MY PRINTER TO WORK ON GOOGLE CLOUD I TRIED CHANGING EMAIL
I HAVE AN OFFICEJET PRO 8600 ALL IN ONE EPRINTER 911A IT WONT LET ME SHARE WITH MY PHONE IT WORKED BEFORE
-
New to Iphone ...purchased Music but don't see the charges in my bank account
I am a brand new iPhone 5 user; actually a brand new apple user. I purchased some music last night and this morning, but I don't see the charges showing as pending in my bank account. Do they not show up as pending? or do they accumulate for a few da
-
Install 10gR2 RAC w/ iscsi chokes "filesystem too small for journal"
Following Jeff Hunter guide to install RAC on 10gR2 on Oracle Linux All has gone well to the point of: [root@lin1 ~]# mkfs.ocfs2 -b 4k -C 32K -N 4 -L oracrsfiles /dev/sde1 mkfs.ocfs2 1.2.4 Filesystem too small for a journal [root@lin1 firefox]# df -m
-
Spinning Beachball: what's going on?
Hello everyone. I keep getting the spinning beachball with even the simplest of actions. What do I mean? Allow me to elaborate. This is happening so often that I've ran several virus scans, I've done the permissions things, run daily, weekly and mont