[英]how to disable dates in date picker when select a date in other datepicker
i have 2 datepickers show in the image. 我有2个日期选择器显示在图像中。
here first i select issuedate then the returndate datepicker shows the dates after the isuue dates . 这里首先我选择issuedate那么returndate日期选择器显示isuue日期后的日期 。 The issuedate to backword dates are not visible for return datepicker
返回datepicker不显示后台日期的已颁发日期
the code for datepicker... datepicker的代码......
JLabel lblIssueDate = new JLabel("Issue Date");
lblIssueDate.setBounds(85, 47, 137, 14);
issuePanel.add(lblIssueDate);
issueDate = new JXDatePicker();
java.util.Date utilDate=new Date();
java.sql.Date sqldate=new java.sql.Date(utilDate.getTime());
issueDate.setDate(sqldate);
issueDate.setBounds(232, 44, 184, 20);
issuePanel.add(issueDate);
JLabel lblReturnDate = new JLabel("Return Date");
lblReturnDate.setBounds(85, 113, 137, 14);
issuePanel.add(lblReturnDate);
returnDate = new JXDatePicker();
returnDate.setBounds(232, 110, 184, 20);
issuePanel.add(returnDate);
Basically, you can set the upper and lower acceptable bounds of the date picker via its JXMonthView
. 基本上,您可以通过其
JXMonthView
设置日期选择器的上下可接受范围。 This will restrict the dates that the user can pick 这将限制用户可以选择的日期
For example... 例如...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.JXDatePicker;
public class TestDatePicker {
public static void main(String[] args) {
new TestDatePicker();
}
public TestDatePicker() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JXDatePicker issueDate;
private final JXDatePicker returnDate;
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
JLabel lblIssueDate = new JLabel("Issue Date");
add(lblIssueDate, gbc);
issueDate = new JXDatePicker();
returnDate = new JXDatePicker();
issueDate.addPropertyChangeListener("date", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("date");
Date selectedDate = issueDate.getDate();
returnDate.getMonthView().setLowerBound(selectedDate);
}
});
java.util.Date utilDate = new Date();
java.sql.Date sqldate = new java.sql.Date(utilDate.getTime());
issueDate.setDate(sqldate);
gbc.gridx++;
add(issueDate, gbc);
gbc.gridx = 0;
gbc.gridy++;
JLabel lblReturnDate = new JLabel("Return Date");
add(lblReturnDate, gbc);
gbc.gridx++;
add(returnDate, gbc);
}
}
}
Now if you want to be able to pick the dates BEFORE the IssueDate
, you can use setUpperBound
instead. 现在,如果您希望能够在
IssueDate
之前选择日期,则可以使用setUpperBound
。
If you want to exclude the selected date as well, then you need to use a Calendar
and roll it by a day in the required direction to included it... 如果您还要排除所选日期,则需要使用
Calendar
并按所需方向将其滚动一天以包含它...
I would also highly recommend make use of appropriate layout managers as they will deal with the variety of differences between systems and how fonts are renderer, amongst other things. 我还强烈建议使用适当的布局管理器,因为它们将处理系统之间的各种差异以及字体渲染的方式等等。
Swing is also designed to work with layout managers, so choosing to ignore them can have lots of funny side effects... Swing也可以与布局管理器一起使用,因此选择忽略它们会产生很多有趣的副作用......
Take a look at Laying Out Components Within a Container 看看在容器中布置组件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.