How to highlight dates inside JCalendar in JDateChooser? - java

I am using a JDateChooser component from an external library toedter(jcalendar1.4). I used a custom DateEvaluator by implementing IDateEvaluator which is later added to JDateChooser component:
public static class HighlightEvaluator implements IDateEvaluator {
private final List<Date> list = new ArrayList<>();
public void add(Date date) {
list.add(date);
}
public void remove(Date date) {
list.remove(date);
}
public void setDates(List<Date> dates) {
list.addAll(dates);
}
#Override
public Color getInvalidBackroundColor() {
return Color.BLACK;
}
#Override
public Color getInvalidForegroundColor() {
return null;
}
#Override
public String getInvalidTooltip() {
return null;
}
#Override
public Color getSpecialBackroundColor() {
return Color.GREEN;
}
#Override
public Color getSpecialForegroundColor() {
return Color.RED;
}
#Override
public String getSpecialTooltip() {
return "filled";
}
#Override
public boolean isInvalid(Date date) {
return false;
}
#Override
public boolean isSpecial(Date date) {
return list.contains(date);
}
}
and here is my main method:
public static void main(){
JDateChooser dateChooser = new JDateChooser();
List<Date> dates = new ArrayList<Date>();
dates.add(new Date());
HighlightEvaluator evaluator = new HighlightEvaluator();
evaluator.setDates(dates);
dateChooser.getJCalendar().getDayChooser().addDateEvaluator(evaluator);
}
Now the current date is supposed to be highlighted by the code but its not getting highlighted. Please tell a fix for this

You need to alter the Date variable a little bit. When you create a Date variable it grabs the current day month year along with time(hours, minutes, seconds, milliseconds)
However setting the same date to evaluator will not work because evaluator expects date to be devoid of the time details. So alternatively you can use Calendar object in your main function as illustrated below:
public static void main(){
JDateChooser dateChooser = new JDateChooser();
List<Date> dates = new ArrayList<Date>();
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, desiredyear);
c.set(Calendar.MONTH, desiredmonth);
c.set(Calendar.DAY_OF_MONTH, desiredday);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
dates.add(c.getTime());
HighlightEvaluator evaluator = new HighlightEvaluator();
evaluator.setDates(dates);
dateChooser.getJCalendar().getDayChooser().addDateEvaluator(evaluator);
}

Related

DatePickerCellEditor format changing when selected

I have created JTable having DatePickerCellEditor with DateFormat(dd/MM/yyyy).
The DateFormat is changing when date is selected.
like this :
here's my code :
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
final DatePickerCellEditor dateCellPrev = new DatePickerCellEditor(formatter);
dateCellPrev.addCellEditorListener(new CellEditorListener() {
#Override
public void editingStopped(ChangeEvent arg0) {
dateCellPrev.setFormats(formatter);
}
#Override
public void editingCanceled(ChangeEvent arg0) {
dateCellPrev.setFormats(formatter);
}
});
table.getColumnModel().getColumn(19).setCellEditor(dateCellPrev);
I want to change format to "dd/MM/yyyy" not like format in last picture

How do i hide or disable all the Sundays on a JDateChooser?

I want to disable all the Sundays on the JDateChooser but I don't know how.
I saw some answers earlier while searching and they're using a range with start and end but in my case it's all the sundays in the jdatechooser. It was for our school project and we're not allowed to drag and drop controls so I declared the datechooser and imported the com.toedter.calendar.JDateChooser;
Below is my code for JDateChooser. I really hope to learn more, thank you.
JDateChooser date = new JDateChooser(new Date());
date.setBounds(120,150,150,30);
sapp1.add(date);
As I mentioned in the comment to the original post, you can obtain JCalendar from JDateChooser and customize it to achieve the desired result.
JDateChooser date = new JDateChooser(new Date());
date.getJCalendar().getDayChooser().addDateEvaluator(new MyDateEvaluator());
You can set a custom IDateEvaluator which allows to makes all Sundays disabled.
#Override
public boolean isInvalid(Date date) {
return date.getDay() == 0;
}
Here is the code that makes all Sundays disabled:
import com.toedter.calendar.IDateEvaluator;
import com.toedter.calendar.JDateChooser;
import javax.swing.*;
import java.awt.*;
import java.util.Date;
public class CustomizedDateChooser {
public static void main(String[] args) {
JFrame f = new JFrame("ComboBox Example");
JDateChooser date = new JDateChooser(new Date());
date.getJCalendar().getDayChooser().addDateEvaluator(new MyDateEvaluator());
date.setBounds(200,200,200,50);
JPanel p = new JPanel();
p.add(new JLabel("Choose a Date:"));
p.add(date);
f.add(p);
f.setLayout(new FlowLayout());
f.setSize(400, 500);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
private static class MyDateEvaluator implements IDateEvaluator {
#Override
public boolean isSpecial(Date date) {
return false;
}
#Override
public Color getSpecialForegroundColor() {
return null;
}
#Override
public Color getSpecialBackroundColor() {
return null;
}
#Override
public String getSpecialTooltip() {
return null;
}
#Override
public boolean isInvalid(Date date) {
return date.getDay() == 0;
}
#Override
public Color getInvalidForegroundColor() {
return null;
}
#Override
public Color getInvalidBackroundColor() {
return null;
}
#Override
public String getInvalidTooltip() {
return null;
}
}
}

Java-JDatePicker Not Displaying anything

i am trying to get the date from the JDatePicker but my problem is eventhough i selected the date its not displaying in the result textbox.
here is my code
can anyone help to how to solve this problem.
public class Datepicker extends JFrame {Datepicker(){
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
UtilDateModel model=new UtilDateModel();
Properties p=new Properties();
p.put("text.today","Today");
p.put("text.month","Month");
p.put("text.year","Year");
JDatePanelImpl panel=new JDatePanelImpl(model, p);
JDatePickerImpl datepic=new JDatePickerImpl(panel,null);
datepic.setBounds(220,350,120,30);
frame.add(datepic);
Date selectedDate=(Date)datepic.getModel().getValue();
frame.setSize(300, 300);
frame.setVisible(true);}
ThankYou.
Please, instead of
JDatePickerImpl datepic=new JDatePickerImpl(panel,null);
do this as follow, it's worked for me :
datePicker = new JDatePickerImpl(datePanel,new DateLabelFormatter());
public class DateLabelFormatter extends AbstractFormatter {
private String datePattern = "yyyy-MM-dd";
private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);
#Override
public Object stringToValue(String text) throws ParseException {
return dateFormatter.parseObject(text);
}
#Override
public String valueToString(Object value) throws ParseException {
if (value != null) {
Calendar cal = (Calendar) value;
return dateFormatter.format(cal.getTime());
}
return "";
}
}

custom spinnerdatemodel not doing anything

custom spinnerdatemodel with overriden getPrevious() and getNext() is not doing anything. what am i doing wrong?
Here is my code. This looks correct to me; so yeah, any help would be much appreciated. Thanks!
/**
* Custom spinner model for the times (hhmm)
*/
class SpinnerTimeModel extends SpinnerDateModel {
/**
* Constructor
*/
public SpinnerTimeModel() {
cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
setValue(cal.getTime());
setStart(cal.getTime());
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
setEnd(cal.getTime());
}
/**
* Returns a time 30 minutes prior to the current time
*
* #return a time 30 minutes prior to the current time
*/
#Override
public Object getPreviousValue() {
Calendar previous = Calendar.getInstance();
previous.setTime(getDate());
previous.add(Calendar.MINUTE, -30);
return previous.getTime();
}
/**
* Returns a time 30 minutes after the current time
*
* #return a time 30 minutes after the current time
*/
#Override
public Object getNextValue() {
Calendar next = Calendar.getInstance();
next.setTime(getDate());
next.add(Calendar.MINUTE, 30);
return next.getTime();
}
private Calendar cal;
}
if you set the JSpinner editor type as DateEditor then the existing code will work fine. The code commented.
public class spinnerdemo {
public void show() {
JFrame f = new JFrame("JSpinner Demo");
f.setSize(500, 100);
f.setLayout(new GridLayout(1, 1));
JSpinner ctrlSpin = new JSpinner();
ctrlSpin.addChangeListener(new javax.swing.event.ChangeListener() {
#Override
public void stateChanged(javax.swing.event.ChangeEvent evt) {
System.out.println("" + ctrlSpin.getValue());
}
});
ctrlSpin.setModel(new SpinnerTimeModel());
//set the DateEditor
ctrlSpin.setEditor(new JSpinner.DateEditor(ctrlSpin, "dd/MM/yyyy HH:mm:ss.SS"));
f.add(ctrlSpin);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args) {
new spinnerdemo().show();
}
}

LWUIT Calendar date format

I have a LWUIT code that supposed to print today date .
The problem with me is the date printed in "Mon dd hh:mm:ss GMT+...... yyyy" format
e.g Thu Nov 28 01:00:00 GMT+03:00 2013
So I have a couple of questions
How to get the format in "yyyy-mon-dd" format.
how to add a day to the today date after conversion to "yyyy-mon-dd" .
Observe that some classes wouldn't work in J2ME like Simpledateformat class.
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
public class myLibrary extends MIDlet {
Form f;
com.sun.lwuit.Calendar cal;
Button b;
public void startApp() {
com.sun.lwuit.Display.init(this);
f = new com.sun.lwuit.Form();
cal = new com.sun.lwuit.Calendar();
b = new Button("Enter");
f.addComponent(cal);
f.addComponent(b);
b.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent acv) {
System.out.println(""+cal.getDate());
}
});
f.show();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
In order to use the java.lwuit.Calendar class, to get your date in that format you will need to substring the data from the cal.getDate().
for example
System.out.println("DAY " + cal.getDate().toString().substring(0,3));
Doing that, you will get your data and after that reorder them in a String.
To change the Date from the Calendar view you will need to use Calendar.setDate(Date d);
I suggest you to use java.util.Calendar
java.util.Calendar c = Calendar.getInstnace();
c.set(Calendar.DAY_OF_THE_MONTH, day_that_you want);
c.set(Calendar.MONTH, month_that_you want);
c.set(Calendar.YEAR, year_that_you want);
java.lwuit.Calendar cal = new java.lwuit.Calendar();
cal.setDate(c.getDate().getTime());
If you still want to use the Date class, try this code, it will print the tomorrow day
private static final int DAY = 24 * 60 * 60 * 1000;
Date d = new Date(); d.setTime(d.getTime() + DAY);
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
public class myLibrary extends MIDlet {
Form f;
Button b;
public void startApp() {
com.sun.lwuit.Display.init(this);
private static final int DAY =86400000;
f = new com.sun.lwuit.Form();
b = new Button("Enter");
f.addComponent(b);
b.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent acv) {
java.util.Date d = new java.util.Date();
d.setTime(d.getTime() + DAY);
System.out.println(""+ d.toString());
}
});
f.show();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}

Categories