当 JSpinner 在运行时动态更改时,我需要能够使用 SpinnerDateModel 和 DateEditor 更改 JSpinner 设置的最大值和最小值。
设置 JSpinners 的初始化代码如下:
jSpinner_StartDateTime = new javax.swing.JSpinner();
jSpinner_EndDateTime = new javax.swing.JSpinner();
jSpinner_StartDateTime.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), null, null, java.util.Calendar.HOUR_OF_DAY));
jSpinner_StartDateTime.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSpinner_StartDateTimeStateChanged(evt);
}
});
jSpinner_EndDateTime.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), null, null, java.util.Calendar.HOUR_OF_DAY));
jSpinner_EndDateTime.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSpinner_EndDateTimeStateChanged(evt);
}
});
jSpinner_StartDateTime.setEditor(new JSpinner.DateEditor(jSpinner_StartDateTime, "yyyy-MM-dd hh:mm:ss.SSS"));
jSpinner_EndDateTime.setEditor(new JSpinner.DateEditor(jSpinner_EndDateTime, "yyyy-MM-dd hh:mm:ss.SSS"));
我有两个在运行时通过程序更改的日期变量,我需要能够在运行时根据这些变量动态设置 JSpinner 的最大值、最小值和值。
Date StartDateTime, EndDateTime;
当我使用下面的代码时,用户界面没有更新。
jSpinner_StartDateTime.setModel(new javax.swing.SpinnerDateModel(StartDateTime, StartDateTime, EndDateTime, java.util.Calendar.SECOND));
jSpinner_EndDateTime.setModel(new javax.swing.SpinnerDateModel(EndDateTime, StartDateTime, EndDateTime, java.util.Calendar.SECOND));
但是,如果我使用下面的代码,日期变量中的正确值将显示在相应的 JSpinner 上,但这些值不限于范围。
jSpinner_StartDateTime.getModel().setValue(StartDateTime);
jSpinner_EndDateTime.getModel().setValue(EndDateTime);
我无法弄清楚问题是什么和/或我做错了什么和/或如何实现我的需要。 请提供任何帮助/建议。
更新:
添加一个工作示例来演示我的问题:
import java.util.Date;
import java.util.Random;
import javax.swing.JSpinner;
public class SpinnerIssueExample extends javax.swing.JFrame {
private javax.swing.JButton jButton_Update;
private javax.swing.JSpinner jSpinner_EndDateTime;
private javax.swing.JSpinner jSpinner_StartDateTime;
private Date StartDateTime, EndDateTime;
private boolean flag = false;
public SpinnerIssueExample() {
initComponents();
{
jSpinner_StartDateTime.setEditor(new JSpinner.DateEditor(jSpinner_StartDateTime, "yyyy-MM-dd hh:mm:ss.SSS"));
jSpinner_EndDateTime.setEditor(new JSpinner.DateEditor(jSpinner_EndDateTime, "yyyy-MM-dd hh:mm:ss.SSS"));
}
updateDates();
}
private void updateDates() {
do {
StartDateTime = new Date((2020 + (int) Math.round(Math.random() * (2025 - 2020))),
(1 + (int) Math.round(Math.random() * (12 - 1))),
(1 + (int) Math.round(Math.random() * (27 - 1))),
(0 + (int) Math.round(Math.random() * (23))),
(0 + (int) Math.round(Math.random() * (59))),
(0 + (int) Math.round(Math.random() * (59))));
EndDateTime = new Date((2020 + (int) Math.round(Math.random() * (2025 - 2020))),
(1 + (int) Math.round(Math.random() * (12 - 1))),
(1 + (int) Math.round(Math.random() * (27 - 1))),
(0 + (int) Math.round(Math.random() * (23))),
(0 + (int) Math.round(Math.random() * (59))),
(0 + (int) Math.round(Math.random() * (59))));
} while (StartDateTime.after(EndDateTime));
{
System.out.println("StartDateTime -> " + StartDateTime);
System.out.println("EndDateTime -> " + EndDateTime);
}
// Alternatively use different methods I used that did not fulfill my requirements.
// This will demonstrate my issue.
if (flag) {
jSpinner_StartDateTime.setModel(new javax.swing.SpinnerDateModel(StartDateTime, StartDateTime, EndDateTime, java.util.Calendar.SECOND));
jSpinner_EndDateTime.setModel(new javax.swing.SpinnerDateModel(EndDateTime, StartDateTime, EndDateTime, java.util.Calendar.SECOND));
flag = false;
} else {
jSpinner_StartDateTime.getModel().setValue(StartDateTime);
jSpinner_EndDateTime.getModel().setValue(EndDateTime);
flag = true;
}
}
private void initComponents() {
jSpinner_StartDateTime = new javax.swing.JSpinner();
jSpinner_EndDateTime = new javax.swing.JSpinner();
jButton_Update = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jSpinner_StartDateTime.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), null, null, java.util.Calendar.SECOND));
jSpinner_EndDateTime.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), null, null, java.util.Calendar.SECOND));
jButton_Update.setText("Update Dates");
jButton_Update.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_UpdateActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSpinner_StartDateTime, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
.addComponent(jSpinner_EndDateTime)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton_Update)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSpinner_StartDateTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSpinner_EndDateTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton_Update)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
private void jButton_UpdateActionPerformed(java.awt.event.ActionEvent evt) { updateDates(); }
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(SpinnerIssueExample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SpinnerIssueExample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SpinnerIssueExample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SpinnerIssueExample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SpinnerIssueExample().setVisible(true);
}
});
}
}
如果运行它并单击按钮加载随机日期,旋转器中的日期将交替更新。
下面的示例输出: 正如您在第一张图片中看到的,值会更新。 在第二个中他们没有。 每次单击更新日期按钮时都会重复。 当它在 UI 中更新时,它不会将日期限制为开始日期和结束日期。
下面的代码对我有用。 谢谢@Abra。
import java.util.Date;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
/**
*
* @author Anil Puvvada
*/
public class SpinnerDatesExample extends javax.swing.JFrame {
private javax.swing.JButton jButton_Update;
private javax.swing.JSpinner jSpinner_EndDateTime;
private javax.swing.JSpinner jSpinner_StartDateTime;
private SpinnerDateModel StartDateModel, EndDateModel;
private Date StartDateTime, EndDateTime;
//private boolean flag = false;
public SpinnerDatesExample() {
initComponents();
{
jSpinner_StartDateTime.setEditor(new JSpinner.DateEditor(jSpinner_StartDateTime, "yyyy-MM-dd hh:mm:ss.SSS"));
jSpinner_EndDateTime.setEditor(new JSpinner.DateEditor(jSpinner_EndDateTime, "yyyy-MM-dd hh:mm:ss.SSS"));
}
{
StartDateModel = (SpinnerDateModel) jSpinner_StartDateTime.getModel();
EndDateModel = (SpinnerDateModel) jSpinner_EndDateTime.getModel();
}
updateDates();
}
private void updateDates() {
do {
StartDateTime = new Date((2020 + (int) Math.round(Math.random() * (2025 - 2020))),
(1 + (int) Math.round(Math.random() * (12 - 1))),
(1 + (int) Math.round(Math.random() * (27 - 1))),
(0 + (int) Math.round(Math.random() * (23))),
(0 + (int) Math.round(Math.random() * (59))),
(0 + (int) Math.round(Math.random() * (59))));
EndDateTime = new Date((2020 + (int) Math.round(Math.random() * (2025 - 2020))),
(1 + (int) Math.round(Math.random() * (12 - 1))),
(1 + (int) Math.round(Math.random() * (27 - 1))),
(0 + (int) Math.round(Math.random() * (23))),
(0 + (int) Math.round(Math.random() * (59))),
(0 + (int) Math.round(Math.random() * (59))));
} while (StartDateTime.after(EndDateTime));
{
System.out.println("StartDateTime -> " + StartDateTime);
System.out.println("EndDateTime -> " + EndDateTime);
}
// Alternatively use different methods I used that did not fulfill my requirements.
// This will demonstrate my issue.
/*if (flag) {
jSpinner_StartDateTime.setModel(new javax.swing.SpinnerDateModel(StartDateTime, StartDateTime, EndDateTime, java.util.Calendar.SECOND));
jSpinner_EndDateTime.setModel(new javax.swing.SpinnerDateModel(EndDateTime, StartDateTime, EndDateTime, java.util.Calendar.SECOND));
flag = false;
} else {
jSpinner_StartDateTime.getModel().setValue(StartDateTime);
jSpinner_EndDateTime.getModel().setValue(EndDateTime);
flag = true;
}*/
{
StartDateModel.setStart(StartDateTime);
StartDateModel.setEnd(EndDateTime);
StartDateModel.setValue(StartDateTime);
}
{
EndDateModel.setStart(StartDateTime);
EndDateModel.setEnd(EndDateTime);
EndDateModel.setValue(EndDateTime);
}
}
private void initComponents() {
jSpinner_StartDateTime = new javax.swing.JSpinner();
jSpinner_EndDateTime = new javax.swing.JSpinner();
jButton_Update = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jSpinner_StartDateTime.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), null, null, java.util.Calendar.HOUR_OF_DAY));
jSpinner_EndDateTime.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), null, null, java.util.Calendar.HOUR_OF_DAY));
jButton_Update.setText("Update Dates");
jButton_Update.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_UpdateActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSpinner_StartDateTime, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
.addComponent(jSpinner_EndDateTime)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton_Update)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSpinner_StartDateTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSpinner_EndDateTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton_Update)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
private void jButton_UpdateActionPerformed(java.awt.event.ActionEvent evt) {
updateDates();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SpinnerDatesExample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SpinnerIssueExample().setVisible(true);
}
});
}
}