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

    Default How to interconnect two JComboBoxes?

    I have a frame in which I have two JComboBoxes - 'dt' for days of a month and 'mt' for the months of a year(2008, in my frame).
    I want that the number of days in 'dt' should change with the month selected in 'mt'.
    Eg.- If I select 'JAN' in 'mt', there should be 31 days available in 'dt', if I select 'FEB' in 'mt' ,there should be only 29 days available in 'dt', and so on for all months.

    How is it possible to interconnect both the JComboBoxes in such manner?

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

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Coordination implements ActionListener {
        JComboBox dayCombo;
        JComboBox monthCombo;
        String[] months = {
            "Jan", "Feb", "Mar", "Apr", "May", "Jun",
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
        };
    
        public void actionPerformed(ActionEvent e) {
            setDayCombo();
        }
    
        private void setDayCombo() {
            int[] daysOfMonths = {
                31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
            };
            int index = monthCombo.getSelectedIndex();
            int days = daysOfMonths[index];
            dayCombo.removeAllItems();
            for(int i = 0; i < days; i++) {
                dayCombo.addItem(Integer.valueOf(i+1));
            }
        }
    
        private JPanel getContent() {
            monthCombo = new JComboBox(months);
            monthCombo.addActionListener(this);
            dayCombo = new JComboBox();
            setDayCombo();
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1.0;
            panel.add(monthCombo, gbc);
            panel.add(dayCombo, gbc);
            return panel;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new Coordination().getContent());
            f.setSize(300,175);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    You could also make a Map for this instead of using arrays:
    Java Code:
    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("Jan", Integer.valueOf(31));
    ...
    int days = ((Integer)map.get("Jan")).intValue();

  3. #3
    swayam07 is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

Similar Threads

  1. JComboBoxes
    By Keerti in forum New To Java
    Replies: 3
    Last Post: 07-28-2008, 07: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
  •