Results 1 to 5 of 5
  1. #1
    swayam07 is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Default 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?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    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.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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);
        }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    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?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. How to interconnect two JComboBoxes?
    By swayam07 in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 11:35 AM
  2. JComboBoxes
    By Keerti in forum New To Java
    Replies: 3
    Last Post: 07-28-2008, 06:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •