programing

Java Swing에 사용할 수있는 좋은 무료 날짜 및 시간 선택기가 있습니까?

yoursource 2021. 1. 14. 23:24
반응형

Java Swing에 사용할 수있는 좋은 무료 날짜 및 시간 선택기가 있습니까?


Java Swing에 사용할 수있는 좋은 무료 날짜 및 시간 선택기가 있습니까?

사용할 수있는 날짜 선택기가 많지만 날짜 및 시간 선택기는 없습니다. 이것은 내가 지금까지 만난 가장 가까운 것 입니다. 날짜 및 시간 선택기를 찾고 있습니다 .

아무도?


시간 선택기의 경우 JSpinner를 사용하고 시간 값만 표시하는 JSpinner.DateEditor를 설정할 수 있습니다.

JSpinner timeSpinner = new JSpinner( new SpinnerDateModel() );
JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "HH:mm:ss");
timeSpinner.setEditor(timeEditor);
timeSpinner.setValue(new Date()); // will only show the current time

swingx JXDatePicker 구성 요소를 확장 할 수 있습니다.

"JXDatePicker는 시간없이 날짜 만 처리합니다. 사용자가 날짜와 시간을 선택할 수 있도록해야하는 경우가 많습니다. 이것은 JXDatePicker를 사용하여 날짜와 시간을 함께 처리하는 방법의 예입니다."

http://wiki.java.net/twiki/bin/view/Javadesktop/JXDateTimePicker

편집 :이 기사는 웹에서 사라졌지 만 SingleShot이 발견함에 따라 인터넷 아카이브에서 여전히 사용할 수 있습니다. 확실히하기 위해 전체 작업 예제는 다음과 같습니다.

import org.jdesktop.swingx.calendar.SingleDaySelectionModel;
import org.jdesktop.swingx.JXDatePicker;

import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.DateFormatter;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.*;
import java.awt.*;

/**
 * This is licensed under LGPL.  License can be found here:  http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * This is provided as is.  If you have questions please direct them to charlie.hubbard at gmail dot you know what.
 */
public class DateTimePicker extends JXDatePicker {
    private JSpinner timeSpinner;
    private JPanel timePanel;
    private DateFormat timeFormat;

    public DateTimePicker() {
        super();
        getMonthView().setSelectionModel(new SingleDaySelectionModel());
    }

    public DateTimePicker( Date d ) {
        this();
        setDate(d);
    }

    public void commitEdit() throws ParseException {
        commitTime();
        super.commitEdit();
    }

    public void cancelEdit() {
        super.cancelEdit();
        setTimeSpinners();
    }

    @Override
    public JPanel getLinkPanel() {
        super.getLinkPanel();
        if( timePanel == null ) {
            timePanel = createTimePanel();
        }
        setTimeSpinners();
        return timePanel;
    }

    private JPanel createTimePanel() {
        JPanel newPanel = new JPanel();
        newPanel.setLayout(new FlowLayout());
        //newPanel.add(panelOriginal);

        SpinnerDateModel dateModel = new SpinnerDateModel();
        timeSpinner = new JSpinner(dateModel);
        if( timeFormat == null ) timeFormat = DateFormat.getTimeInstance( DateFormat.SHORT );
        updateTextFieldFormat();
        newPanel.add(new JLabel( "Time:" ) );
        newPanel.add(timeSpinner);
        newPanel.setBackground(Color.WHITE);
        return newPanel;
    }

    private void updateTextFieldFormat() {
        if( timeSpinner == null ) return;
        JFormattedTextField tf = ((JSpinner.DefaultEditor) timeSpinner.getEditor()).getTextField();
        DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
        DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();
        // Change the date format to only show the hours
        formatter.setFormat( timeFormat );
    }

    private void commitTime() {
        Date date = getDate();
        if (date != null) {
            Date time = (Date) timeSpinner.getValue();
            GregorianCalendar timeCalendar = new GregorianCalendar();
            timeCalendar.setTime( time );

            GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY, timeCalendar.get( Calendar.HOUR_OF_DAY ) );
            calendar.set(Calendar.MINUTE, timeCalendar.get( Calendar.MINUTE ) );
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);

            Date newDate = calendar.getTime();
            setDate(newDate);
        }

    }

    private void setTimeSpinners() {
        Date date = getDate();
        if (date != null) {
            timeSpinner.setValue( date );
        }
    }

    public DateFormat getTimeFormat() {
        return timeFormat;
    }

    public void setTimeFormat(DateFormat timeFormat) {
        this.timeFormat = timeFormat;
        updateTextFieldFormat();
    }

    public static void main(String[] args) {
        Date date = new Date();
        JFrame frame = new JFrame();
        frame.setTitle("Date Time Picker");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DateTimePicker dateTimePicker = new DateTimePicker();
        dateTimePicker.setFormats( DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.MEDIUM ) );
        dateTimePicker.setTimeFormat( DateFormat.getTimeInstance( DateFormat.MEDIUM ) );

        dateTimePicker.setDate(date);

        frame.getContentPane().add(dateTimePicker);
        frame.pack();
        frame.setVisible(true);
    }
}

Use the both combined.. that's what i did:

public static JPanel buildDatePanel(String label, Date value) {
JPanel datePanel = new JPanel();

JDateChooser dateChooser = new JDateChooser();
if (value != null) {
    dateChooser.setDate(value);
}
for (Component comp : dateChooser.getComponents()) {
    if (comp instanceof JTextField) {
    ((JTextField) comp).setColumns(50);
    ((JTextField) comp).setEditable(false);
    }
}

datePanel.add(dateChooser);

SpinnerModel model = new SpinnerDateModel();
JSpinner timeSpinner = new JSpinner(model);
JComponent editor = new JSpinner.DateEditor(timeSpinner, "HH:mm:ss");
timeSpinner.setEditor(editor);
if(value != null) {
    timeSpinner.setValue(value);
}

datePanel.add(timeSpinner);

return datePanel;
}

There is the FLib-JCalendar component with a combined Date and Time Picker.


As you said Date picker is easy, there are many out there.

As for a Time picker, check out how Google Calendar does it when creating a new entry. It allows you to type in anything while at the same time it has a drop down in 30 mins increments. The drop down changes when you change the minutes.

If you need to allow the user to pick seconds, then the best you can do is a typable/drop down combo

ReferenceURL : https://stackoverflow.com/questions/654342/is-there-any-good-and-free-date-and-time-picker-available-for-java-swing

반응형