如何在我的JTable列中设置JXDatePicker的最小日期。我需要在JXDatePicker中显示/启用大于当前日期的日期,在日历下面的日期应该在日历中显示,以便用户不能选择那些日期。怎么做?请帮帮我。下面是我的代码。
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableColumn;
import org.jdesktop.swingx.table.DatePickerCellEditor;
public class TableWithDate {
private static void createAndShowGUI() {
JFrame frame = new JFrame("TableWithDate");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(new Object[][] { { "1", new Date() } },
new Object[] { "Id", "Time" });
TableColumn dateColumn = table.getColumnModel().getColumn(1);
dateColumn.setCellEditor(new DatePickerCellEditor());
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
答案 0 :(得分:2)
JXDatePicker
字段具有protected
访问权限,您必须扩展DatePickerCellEditor
并提供该功能......
public class BoundDatePickerCellEditor extends DatePickerCellEditor {
public BoundDatePickerCellEditor() {
super();
}
public BoundDatePickerCellEditor(DateFormat dateFormat) {
super(dateFormat);
}
public void setLowerBound(Date date) {
datePicker.getMonthView().setLowerBound(date);
}
public void setUpperBound(Date date) {
datePicker.getMonthView().setLowerBound(date);
}
}
可运行的示例......
import java.awt.EventQueue;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.TableColumn;
import org.jdesktop.swingx.table.DatePickerCellEditor;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JTable table = new JTable(new Object[][]{{"1", new Date()}},
new Object[]{"Id", "Time"});
TableColumn dateColumn = table.getColumnModel().getColumn(1);
BoundDatePickerCellEditor picker = new BoundDatePickerCellEditor();
picker.setLowerBound(new Date());
dateColumn.setCellEditor(picker);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class BoundDatePickerCellEditor extends DatePickerCellEditor {
public BoundDatePickerCellEditor() {
super();
}
public BoundDatePickerCellEditor(DateFormat dateFormat) {
super(dateFormat);
}
public void setLowerBound(Date date) {
datePicker.getMonthView().setLowerBound(date);
}
public void setUpperBound(Date date) {
datePicker.getMonthView().setLowerBound(date);
}
}
}
如果你今天想要排除,也可以使用类似......
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
picker.setLowerBound(cal.getTime());
...例如