
se.wfh.libs.common.gui.widgets.WDateTimeTextField Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gui Show documentation
Show all versions of gui Show documentation
Some helper classes i needed for several projects (GUI / Swing parts)
package se.wfh.libs.common.gui.widgets;
import java.awt.GridLayout;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JPanel;
import se.wfh.libs.common.gui.AbstractWValidatingComponent;
public class WDateTimeTextField extends
AbstractWValidatingComponent {
private static final long serialVersionUID = 1L;
private final WDateTextField date;
private final WTimeTextField time;
public WDateTimeTextField() {
this(null);
}
public WDateTimeTextField(final Date defval) {
super(defval, new JPanel(new GridLayout(1, 0, 0, 0)));
date = new WDateTextField(defval);
time = new WTimeTextField(defval);
date.addDataChangedListener(event -> valueChanged(getValue()));
time.addDataChangedListener(event -> valueChanged(getValue()));
getComponent().add(date);
getComponent().add(time);
changeBackground(getValueInternal() != null);
}
@Override
public Date getValue() {
return getValueInternal();
}
private Date getValueInternal() {
Date result = null;
if (date != null && time != null && date.getValue() != null
&& time.getValue() != null) {
Calendar dateC = new GregorianCalendar();
Calendar dateT = new GregorianCalendar();
dateC.setTime(date.getValue());
dateT.setTime(time.getValue());
dateC.set(Calendar.HOUR_OF_DAY, dateT.get(Calendar.HOUR_OF_DAY));
dateC.set(Calendar.MINUTE, dateT.get(Calendar.MINUTE));
result = dateC.getTime();
}
return result;
}
@Override
public boolean isInputValid(final Date value) {
return value != null;
}
@Override
public void setValue(final Date value) {
date.setValue(value);
time.setValue(value);
changeBackground(isValidValue(value));
updatePreviousValue(value);
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
date.setEnabled(enabled);
time.setEnabled(enabled);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy