Setting a Minimum Date for JXDatePicker in a JTable column

1.3k views Asked by At

How can i set a minimum date for the JXDatePicker in my JTable column.That is i need to show/enable only the date greater than current date in the JXDatePicker,date below current date should be desabled in the calender so that user can't select those dates.How to do that?Please help me.Below is my code.

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();
        }
    });
}
}
1

There are 1 answers

14
MadProgrammer On BEST ANSWER

The JXDatePicker field has protected access, you'll have to extend DatePickerCellEditor and provide that functionality...

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);          
    }

}

Runnable example...

Picker

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);
        }

    }
}

If you want to disclude today as well, you could use something like...

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
picker.setLowerBound(cal.getTime());

...for example