Results 1 to 5 of 5
Thread: problem with JComboBoxes?
- 08-26-2008, 04:44 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 3
- Rep Power
- 0
problem with JComboBoxes?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class myframe extends JFrame implements ItemListener {
JComboBox ch1,ch3;
JComboBox ch2,ch4;
String[] months = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug"," Sep","Oct","Nov","Dec"};
myframe()
{
setLayout(null);
ch1 = new JComboBox();
ch2 = new JComboBox(months);
ch1.setBounds(100,100,100,30);
ch2.setBounds(400,100,100,30);
add(ch1);
add(ch2);
ch3 = new JComboBox();
ch4 = new JComboBox(months);
ch3.setBounds(100,200,100,30);
ch4.setBounds(400,200,100,30);
add(ch3);
add(ch4);
show();
setDayCombo();
ch2.addItemListener(this);
ch4.addItemListener(this);
ch1.addItemListener(this);
ch3.addItemListener(this);
}
private void setDayCombo()
{
int[] daysOfMonths = {31,29,31,30,31,30,31,31,30,31,30,31};
int index = ch2.getSelectedIndex();
int days = daysOfMonths[index];
ch1.removeAllItems();
for(int i = 0; i < days; i++)
{
ch1.addItem(Integer.valueOf(i+1));
}
int index1 = ch4.getSelectedIndex();
int days1 = daysOfMonths[index1];
ch3.removeAllItems();
for(int i = 0; i < days1; i++)
{
ch3.addItem(Integer.valueOf(i+1));
}
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==ch2 || ie.getSource()==ch4)
setDayCombo();
if(ie.getSource()==ch1 || ie.getSource()==ch3)
System.out.println("Hello");
}
}
class test1
{
public static void main(String[] args)
{
myframe f = new myframe();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1280,747);
f.setVisible(true);
}
}
I want that setDayCombo() should be called when any month from ch2 or ch4 is selected, and 'Hello' should be printed when a day from ch1 or ch3 is selected. But, it's not working. It prints multiple 'Hello's when i select from ch1 or ch3, and prints even more 'Hello's when i select from ch2 or ch4.Plz tell me how can it be done?
- 08-26-2008, 06:11 PM #2
Have you tried debugging your code?
For example what is the value of ie.getSource() when itemStateChanged is called?
Add some println() statements to see.
Are there other causes for the itemStateChanged method to be called? Again add some println() to see.
To see who called a method add:
try{throw new Exception("who called");}catch(Exception x) {x.printStackTrace();}
to the method.
- 08-26-2008, 06:42 PM #3
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; class myframeRx extends JFrame implements ItemListener { JComboBox ch1,ch3; JComboBox ch2,ch4; String[] months = { "Jan","Feb","Mar","Apr","May","Jun","Jul", "Aug"," Sep","Oct","Nov","Dec" }; myframeRx() { setLayout(null); ch1 = new JComboBox(); ch2 = new JComboBox(months); ch1.setBounds(100,100,100,30); ch2.setBounds(400,100,100,30); setDayCombo(ch2); add(ch1); add(ch2); ch3 = new JComboBox(); ch4 = new JComboBox(months); ch3.setBounds(100,200,100,30); ch4.setBounds(400,200,100,30); add(ch3); add(ch4); // show(); deprecated method, see api setDayCombo(ch4); ch2.addItemListener(this); ch4.addItemListener(this); ch1.addItemListener(this); ch3.addItemListener(this); } private void setDayCombo(JComboBox combo) { int[] daysOfMonths = {31,29,31,30,31,30,31,31,30,31,30,31}; if(combo == ch2) { int index = ch2.getSelectedIndex(); int days = daysOfMonths[index]; ch1.removeAllItems(); // A new item will be selected in the loop below // which will generate an ItemEvent for this component. for(int i = 0; i < days; i++) { ch1.addItem(Integer.valueOf(i+1)); } } if(combo == ch4) { int index1 = ch4.getSelectedIndex(); int days1 = daysOfMonths[index1]; ch3.removeAllItems(); for(int i = 0; i < days1; i++) { ch3.addItem(Integer.valueOf(i+1)); } } } public void itemStateChanged(ItemEvent ie) { // Filter for selected events. if(ie.getStateChange() == ItemEvent.SELECTED) { JComboBox combo = (JComboBox)ie.getSource(); if(combo == ch2 || combo == ch4) { setDayCombo(combo); } if(combo == ch1 || combo == ch3) { int n = (combo == ch1) ? 1 : 3; System.out.println("Hello from ch" + n); } } } public static void main(String[] args) { myframeRx f = new myframeRx(); f.setDefaultCloseOperation(EXIT_ON_CLOSE); f.setSize(600,300); f.setVisible(true); } }
- 08-26-2008, 07:01 PM #4
How will people learn to debug their code if someone gives them the answer? Are we trying to teach people how to solve their problems or are we solving their problems for them?
-
Similar Threads
-
How to interconnect two JComboBoxes?
By swayam07 in forum New To JavaReplies: 2Last Post: 08-21-2008, 11:35 AM -
JComboBoxes
By Keerti in forum New To JavaReplies: 3Last Post: 07-28-2008, 06:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks