2012-12-28 36 views
0

我使用jComboBox來顯示日期,月份和年份,並將jComboBox中的第一項添加爲DD,MM,YY(我在模型屬性中將第一項設置爲「DD,YY」 JComboBox中的)。爲了顯示日期和年份值我用loop.The代碼如下..如何列出jComboBox值

//for date 
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {           
    for(int k=1;k<=31;k++) 
    { 

     jComboBox1.addItem(k); 
    } 
} 

    //for year 
    private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {           
    for(int n=1980;n<=2012;n++) 
    { 

     jComboBox3.addItem(n); 
    } 
}       

所以我的問題是,當我選擇DD和YY的JComboBox它並沒有動態顯示值。例如,當我點擊列表中的DD jComboBox時,它將顯示另一個項目作爲DD,然後如果我選擇僅意味着它顯示DD和YY的所有值。爲什麼??以及如何動態顯示它。

回答

1
+0

我uisng NetBeans.I想知道如何通過循環添加日,月,年的JComboBox,我想在列表中DD,MM中的第一項,YY.How怎麼做? –

+0

不是JComboBox從getSelectedItem返回值 – mKorbel

1

代碼初始化JComboBox中就可以了用戶點擊,這是錯誤的

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {           

////////////here you put the action like getSelectedItem() which return the item 

} 
+0

我使用** netbeans IDE ** m我想要jComboBox中的第一項作爲日期的DD,MM作爲月份,YY作爲YEAR,然後我用for循環來添加各自的值..並且我也嘗試作爲私人無效jComboBox3ActionPerformed(java.awt.event.ActionEvent evt)jComboBox.setSelectedItem(「DD」);對於(int n = 1980; n <= 2012; n ++) {jComboBox3.addItem(n);} 它不會添加DD作爲jComboBox中的第一項,如果我使用循環..怎麼做? ?? –

1

檢查這個代碼

public class ComboBoxDatePicker extends JPanel implements ItemListener { 

    private static final long serialVersionUID = 1L; 
    private JComboBox myDay; 
    private JComboBox myMonth; 
    private JComboBox myYear; 
    private Collection<ItemListener> myListeners; 

    public ComboBoxDatePicker(String name) { 
     this(name, new Date(System.currentTimeMillis())); 
    } 

    public ComboBoxDatePicker(String name, Date date) { 
     super(new FlowLayout(FlowLayout.LEFT)); 
     myListeners = new HashSet<ItemListener>(); 

     myDay = new JComboBox(new RangeModel(1, 31)); 
     myMonth = new JComboBox(new RangeModel(1, 12)); 
     myYear = new JComboBox(new RangeModel(2000, 2020)); 

     Calendar calendar = Calendar.getInstance(); 
     calendar.setTime(date); 
     myDay.setSelectedItem(calendar.get(Calendar.DAY_OF_MONTH)); 
     myMonth.setSelectedItem(calendar.get(Calendar.MONTH) + 1); 
     myYear.setSelectedItem(calendar.get(Calendar.YEAR)); 

     myDay.addItemListener(this); 
     myMonth.addItemListener(this); 
     myYear.addItemListener(this); 

     add(new JLabel(name)); 
     add(myDay);   
     add(myMonth); 
     add(myYear); 

     doLayout(); 
    } 

    public void setEnabled(boolean enabled) { 
     myDay.setEnabled(enabled); 
     myMonth.setEnabled(enabled); 
     myYear.setEnabled(enabled); 
    } 

    public Date getDate() { 
     if (!myDay.isEnabled()) { 
      return null; 
     } 

     Calendar calendar = Calendar.getInstance(); 
     calendar.clear(); 
     calendar.set(Calendar.DAY_OF_MONTH, (Integer) myDay.getSelectedItem()); 
     calendar.set(Calendar.MONTH, (Integer) myMonth.getSelectedItem() - 1); 
     calendar.set(Calendar.YEAR, (Integer) myYear.getSelectedItem()); 
     return calendar.getTime(); 
    } 

    private static class RangeModel implements ComboBoxModel { 

     private int myMin; 
     private int myMax; 
     private Object mySelection; 

     public RangeModel(int min, int max) { 
      myMin = Math.min(min, max); 
      myMax = Math.max(min, max); 
      mySelection = myMin; 
     } 
     public Object getSelectedItem() { 
      return mySelection; 
     } 
     public void setSelectedItem(Object anItem) { 
      mySelection = anItem; 
     } 
     public int getSize() { 
      return myMax - myMin + 1; 
     } 
     public Object getElementAt(int index) { 
      return myMin + index; 
     } 
     public void addListDataListener(ListDataListener l) { 
     } 
     public void removeListDataListener(ListDataListener l) { 
     } 
    } 

    public void addItemListener(ItemListener l) { 
     myListeners.add(l); 
    } 

    public void itemStateChanged(ItemEvent e) { 
     for (ItemListener listener : myListeners) { 
      listener.itemStateChanged(e); 
     } 
    } 
} 

,你可以使用這個在任何你需要

JFrame frame = new JFrame() ; 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    ComboBoxDatePicker chooser = new ComboBoxDatePicker("Choose Date"); 
    frame.add(chooser); 
    frame.pack(); 
    frame.setVisible(true); 
0

如果您使用jframe,那麼您可以編寫代碼以動態地將值添加到組合框中t他WindowActivated的JFrame的事件是這樣的:

private void formWindowActivated(java.awt.event.WindowEvent evt) { 
// TODO add your handling code here: 
    //for Day 
    for(int i=1;i<=31;i++) 
    { 
     jComboBox1.addItem(i); 
    } 
    //for Month 
    for(int i=1;i<=12;i++) 
    { 
     jComboBox2.addItem(i); 
    } 
}