Search in sources :

Example 1 with JXMonthView

use of org.jdesktop.swingx.JXMonthView in project azure-tools-for-java by Microsoft.

the class DatePickerCellEditor method getTableCellEditorComponent.

@Override
public Component getTableCellEditorComponent(JTable jTable, Object value, boolean b, int row, int col) {
    this.value = null;
    ((JComponent) this.getComponent()).setBorder(new LineBorder(JBColor.BLACK));
    try {
        Class columnClass = jTable.getColumnClass(col);
        if (columnClass == Object.class) {
            columnClass = String.class;
        }
        checkPackageAccess(columnClass);
        checkAccess(columnClass.getModifiers());
        this.constructor = columnClass.getConstructor(String.class);
    } catch (Exception ignored) {
        return null;
    }
    final Component component = super.getTableCellEditorComponent(jTable, value, b, row, col);
    if (!isCellDate(jTable, row, col)) {
        return component;
    }
    JButton button = new JButton("...");
    button.setPreferredSize(new Dimension(button.getPreferredSize().height, 40));
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            final JXMonthView monthView = new JXMonthView();
            monthView.setSelectionMode(DateSelectionModel.SelectionMode.SINGLE_SELECTION);
            monthView.setTraversable(true);
            monthView.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    String date = new SimpleDateFormat().format(monthView.getSelectionDate());
                    ((JTextField) component).setText(date);
                }
            });
            JDialog frame = new JDialog();
            frame.getContentPane().add(monthView);
            frame.setModal(true);
            frame.setAlwaysOnTop(true);
            frame.setMinimumSize(monthView.getPreferredSize());
            frame.pack();
            frame.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width) / 2 - frame.getWidth() / 2, (Toolkit.getDefaultToolkit().getScreenSize().height) / 2 - frame.getHeight() / 2);
            frame.setVisible(true);
        }
    });
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(component, BorderLayout.CENTER);
    panel.add(button, BorderLayout.LINE_END);
    return panel;
}
Also used : JXMonthView(org.jdesktop.swingx.JXMonthView) ActionEvent(java.awt.event.ActionEvent) LineBorder(javax.swing.border.LineBorder) ActionListener(java.awt.event.ActionListener) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 SimpleDateFormat (java.text.SimpleDateFormat)1 LineBorder (javax.swing.border.LineBorder)1 JXMonthView (org.jdesktop.swingx.JXMonthView)1