Results 1 to 3 of 3
- 08-18-2008, 01:55 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 3
- Rep Power
- 0
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?
- 08-18-2008, 10:57 PM #2
You could also make a Map for this instead of using arrays: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); } }
Java Code:Map<String, Integer> map = new HashMap<String, Integer>(); map.put("Jan", Integer.valueOf(31)); ... int days = ((Integer)map.get("Jan")).intValue();
- 08-21-2008, 11:35 AM #3
Member
- Join Date
- Aug 2008
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
JComboBoxes
By Keerti in forum New To JavaReplies: 3Last Post: 07-28-2008, 06:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks