我使用jComboBox显示日期,月份,年份,我将jComboBox中的第一项添加为DD,MM,YY(我在jComboBox的模型属性中将第一项设置为“DD,YY”)。显示我用于循环的日期和年份值。代码如下..
//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的所有值。为什么??以及如何动态显示它。
答案 0 :(得分:1)
不知道,更快帮助发布SSCCE,简短,可运行,可编辑,仅约JFrame
(由FlowLayout
或GridLayout
奠定)和三JComboBoxes
请参阅JComboBox.getSelectedItem
或将ItemListener添加到JComboBox
我将使用JSpinner with SpinnerListModel/SpinnerDateModel代替JComboBox
答案 1 :(得分:1)
你的代码每次用户点击它时都会初始化JComboBox,这是错误的
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
////////////here you put the action like getSelectedItem() which return the item
}
答案 2 :(得分: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);
答案 3 :(得分:0)
如果您使用jframe,那么您可以编写代码以动态地将值添加到jframe的WindowActivated
事件中的组合框中,如下所示:
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);
}
}