All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.becausetesting.commonswindow.utils.ui.JDateTimePickerUtils Maven / Gradle / Ivy

package com.github.becausetesting.commonswindow.utils.ui;

/*-
 * false
 * commons-window
 * sectionDelimiter
 * Copyright (C) 2016 Alter Hu
 * sectionDelimiter
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

/*-
 * #%L
 * commons-window
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2016 Alter Hu
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.time.LocalDateTime;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import com.github.lgooddatepicker.components.DatePicker;
import com.github.lgooddatepicker.components.DatePickerSettings;
import com.github.lgooddatepicker.components.DateTimePicker;
import com.github.lgooddatepicker.components.TimePicker;
import com.github.lgooddatepicker.components.TimePickerSettings;

public class JDateTimePickerUtils {

	public static DatePicker createDatePicker(String dateFormat) {
		URL dateImageURL = DateTimePicker.class.getResource("/images/datepickerbutton1.png");
		Image dateExampleImage = Toolkit.getDefaultToolkit().getImage(dateImageURL);
		ImageIcon dateExampleIcon = new ImageIcon(dateExampleImage);
		// Create the date picker, and apply the image icon.
		DatePickerSettings dateSettings = new DatePickerSettings();
		if (dateSettings!=null)
			dateSettings.setFormatForDatesCommonEra(dateFormat);
		DatePicker datePicker = new DatePicker(dateSettings);
		datePicker.setDateToToday();
		JButton datePickerButton = datePicker.getComponentToggleCalendarButton();
		datePickerButton.setText("");
		datePickerButton.setIcon(dateExampleIcon);
		return datePicker;
	}
	
	public static TimePicker createTimePicker(String dateFormat) {
		URL dateImageURL = TimePicker.class.getResource("/images/timepickerbutton1.png");
		Image dateExampleImage = Toolkit.getDefaultToolkit().getImage(dateImageURL);
		ImageIcon dateExampleIcon = new ImageIcon(dateExampleImage);
		// Create the date picker, and apply the image icon.
		TimePickerSettings dateSettings = new TimePickerSettings();
		if (dateSettings!=null)
			dateSettings.setFormatForMenuTimes(dateFormat);
		TimePicker datePicker = new TimePicker(dateSettings);
		datePicker.setTimeToNow();
		JButton datePickerButton = datePicker.getComponentToggleTimeMenuButton();
		datePickerButton.setText("");
		datePickerButton.setIcon(dateExampleIcon);
		return datePicker;
	}
	
	public static DateTimePicker createDateTimePicker(String dateformat,String timeformat){ 
		DatePickerSettings datePickerSettings = new DatePickerSettings();
		if (dateformat!=null)
			datePickerSettings.setFormatForDatesCommonEra(dateformat);
		TimePickerSettings timePickerSettings = new TimePickerSettings();
		if (timeformat!=null)
			timePickerSettings.setFormatForMenuTimes(timeformat);
		DateTimePicker dateTimePicker=new DateTimePicker(datePickerSettings, timePickerSettings);
		dateTimePicker.setDateTime(LocalDateTime.now());
		return dateTimePicker;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy