Results 1 to 4 of 4
Thread: Serious Problem with JComboBox
- 02-02-2010, 02:08 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
Serious Problem with JComboBox
When the model of a combo box is changed by setting it to a certain index, usually using setSelectedIndex() or setSelectedItemAt(int index), the software (code) calls actionPerformed. This results in multiple stack calls to actionPerformed, that then unwind. This is undesirable if you have other code in actionPerformed that should only be go through once per actionPerformed call. Essentially combobox is like a recursive function.
How can you prevent this?
Here's more detail:
actionPerformed()
{
....//code
if ( event.getSource() == monthBox || event.getSource == yearBox )
{
....//do this
}
}
So every time setSelectedIndex or setSelectedItem is used on a combo box, it calls actionPerformed multiple times before even finishing the first original actionPerformed call.Last edited by Plissken; 02-02-2010 at 02:14 AM.
-
I see no recursion in my small demo program that tests ItemListener and ActionListener:
Perhaps you should post a compilable program that demonstrates the issue.Java Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.Random; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; public class ComboBoxListenerTest { private static void createAndShowUI() { final String[] items = {"Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"}; final DefaultComboBoxModel comboModel = new DefaultComboBoxModel(items); final JComboBox comboBox = new JComboBox(comboModel); final Random random = new Random(); comboBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { System.out.println("ItemListener triggered. Item: " + e.getItem()); } }); comboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("ActionListener triggered. Item: " + comboBox.getSelectedItem()); } }); JButton changeModel = new JButton("Change Model"); changeModel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int i = random.nextInt(comboModel.getSize()); comboBox.setSelectedIndex(i); } }); JPanel panel = new JPanel(); panel.add(comboBox); panel.add(changeModel); JFrame frame = new JFrame("ComboBoxListenerTest"); frame.getContentPane().add(panel); 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(); } }); } }
- 02-02-2010, 06:47 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
The demo code you've written (thanks for taking the time to do that) works fine because there is a separate new actionPerformed for each component instead of one actionPerformed function for all components.
I've solved the problem though. I used a boolean as a switch to suppress the calls to actionPerformed.
-
Similar Threads
-
Activate JComboBox 1 when object is selected in JComboBox 2...
By bahumbaba in forum AWT / SwingReplies: 2Last Post: 12-10-2009, 01:58 PM -
Problem with Jcombobox in Tablecell
By yernikumar in forum AWT / SwingReplies: 1Last Post: 03-19-2009, 08:00 AM -
Accessibility of JComboBox problem
By abedules78 in forum AWT / SwingReplies: 0Last Post: 12-26-2008, 01:11 PM -
I need help with JComboBox
By eva21 in forum New To JavaReplies: 1Last Post: 11-28-2008, 10:05 PM -
Problem with JComboBox and Jlist
By java_fun2007 in forum New To JavaReplies: 2Last Post: 05-07-2008, 08:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks