Show only months and years with JDatePicker - java

I am trying to use JDatePicker to display a calendar. However, I only want to display months and years, not the days.
I tried poking around the model and JDatePickerImpl objects, without luck.
Here is the code I have to show the JDatePicker, from documentation:
UtilCalendarModel model = new UtilCalendarModel();
Properties p = new Properties();
p.put("text.today", "Today");
p.put("text.month", "Month");
p.put("text.year", "Year");
JDatePanelImpl datePanel = new JDatePanelImpl(model, p);
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateComponentFormatter());
Thanks in advance!
Note: Here is an image of what I mean.

After looking at the code from the JDatePicker project, I think this is possible by making a custom version of the JDatePanelImpl class (and perhaps some other classes). The functionality you want is not yet configurable in the standard classes, but it could be implemented as an enhancement and sent to the JDatePicker developers as a proposal (pull request).
Just to be sure of what you need for your application: you want to use a calendar similar to the example below? The user could change the selected month & year by clicking the next/previous month buttons, selecting a month from the month popup menu, or selecting a different year (using the year spinner):
Edit: added example with adapted versions of JDatePicker classes
I have added a modified example of your code and two adapted versions of JDatePicker classes. The normal component closes the popup when the user clicks a specific day, which is not possible in this case (since the days are hidden). I have added a small OK button to make it possible to close the date picker (see screenshot above). This is clearly a proof of concept only; the code really needs more work.
(Note: when I tried to add the two modified classes, my answer became to big. Therefore I forked the JDatePicker project on GitHub, rewrote the customizations from JDatePicker version 1.3.4 to version 1.3.4.1, and added links for these two files instead of all the code.)
// ExampleDatePickerWithoutDay class:
import java.text.*;
import javax.swing.*;
import org.jdatepicker.*;
public class ExampleDatePickerWithoutDay {
public static void main(String[] arguments) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ExampleDatePickerWithoutDay().createAndShowGui();
}
});
}
private void createAndShowGui() {
JFrame frame = new JFrame("Stack Overflow");
frame.setBounds(100, 100, 800, 200);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
// Set two date formats and a text label.
DateFormat dateFormat = new SimpleDateFormat("MMMM yyyy");
ComponentFormatDefaults.Key formatKey;
formatKey = ComponentFormatDefaults.Key.SELECTED_DATE_FIELD;
ComponentFormatDefaults.getInstance().setFormat(formatKey, dateFormat);
formatKey = ComponentFormatDefaults.Key.TODAY_SELECTOR;
ComponentFormatDefaults.getInstance().setFormat(formatKey, dateFormat);
ComponentTextDefaults.Key textKey = ComponentTextDefaults.Key.TODAY;
ComponentTextDefaults.getInstance().setText(textKey, "Current month");
// Create the date picker.
UtilCalendarModel calendarModel = new UtilCalendarModel();
CustomDatePanel datePanel = new CustomDatePanel(calendarModel);
CustomDatePicker datePicker = new CustomDatePicker(datePanel);
panel.add(datePicker);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
CustomDatePanel class: CustomDatePanel.java (on GitHub)
CustomDatePicker class: CustomDatePicker.java (on GitHub)

Related

How to create new event in Google Calendar API with java in NetBeans

I followed the quickstart guide that Google provides on Calendar API https://developers.google.com/google-apps/calendar/quickstart/java but they dont explain how to create a new event. I found this snippet of code online
public void createEvent(Calendar cal){
Event event = new Event();
event.setSummary("Event name here");
event.setLocation("event place here");
Date startDate = new Date();
Date endDate = new Date(startDate.getTime() + 3600000);
DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
event.setStart(new EventDateTime().setDateTime(start));
DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
event.setEnd(new EventDateTime().setDateTime(end));
Event createdEvent = cal.events().insert("primary", event).execute();
System.out.println("Created event id: " + createdEvent.getId());
}
But it didn't help me, i got an error in the Event createdEvent = cal.events() section as events() doesn't exist. Any help is much appreciated, thank you.
At the bottom of your link to the documentation there is a link to Create Events. I won't duplicate the entire page here, but the gist is that you need to Create an Event object (perhaps called MyNewEvent), populate it, and then call:
MyNewEvent = service.events().insert("Some Calendar Id", MyNewEvent).execute();

Function works only in Debug Mode MATLAB GUI

I'm trying to create a calendar using MATLAB GUI.
I found this tutorial and created calendar:
com.mathworks.mwswing.MJUtilities.initJIDE;
% Put calendar to my figure
jPanel = com.jidesoft.combobox.DateChooserPanel;
[hPanel,hContainer] = javacomponent(jPanel,[500,130,200,200],gcf);
set(handles.hPanel, 'MousePressedCallback', ...
#(src, evnt)CellSelectionCallback(src, evnt, handles));
set(handles.hPanel, 'KeyPressedCallback', ...
#(src, evnt)CellSelectionCallback(src, evnt, handles));
Also I have Edit Text object. Lets try to put Selected Value to it!
I use excaza's code:
function CellSelectionCallback(hObject, evnt, handles)
hModel = handle(hObject.getSelectionModel, 'CallbackProperties');
selectedDate = hModel.getSelectedDate();
dayNumber = get(selectedDate,'Date');
handles.edit_start.String = num2str(dayNumber);
handles.newNote(3) = {dayNumber};
guidata(handles.figure1, handles);
So I can read selected date and put it anywhere.
Problem
Code works only in Debug Mode. In normal mode my Edit Text Object (edit_start) stays empty!

SWT DateTime doesn't take into account the locale

Is it possible to change to change the format in which DataTime widget diplays date? The matter is that even though I set the locale to one that uses European format (dd/mm/yyyy) I still have DateTime widget in mm/dd/yyyy format.
Edit: There have been similat questions on SO along the lines "How to change the format of DateTime" and they were answered by something like "change the locale and DateTime will adapt". My question is about the situation when changing the locale doesn't have any effect on DateTime widget. What might be wrong? Is there any other solution to force format change?
After running some tests on Linux, I can confirm that the DateTime widget does not appear to be using the OS's locale. This seems to be a bug and you should report it.
What you can do in the meantime is use Nebula's CDateTime which supports Java Locales:
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
Locale.setDefault(Locale.GERMAN);
CDateTime cdt = new CDateTime(shell, CDT.DATE_SHORT);
cdt.setSelection(new Date());
Locale.setDefault(Locale.ENGLISH);
cdt = new CDateTime(shell, CDT.DATE_SHORT);
cdt.setSelection(new Date());
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

JXDatePicker make arrows in the JXMonthView bigger

I want to use this JXDatePicker component in a application that will work on a touch display. Because the default component is small, all the dates and the buttons are hard to click using a bad touch screen I wanted to make them bigger. So far I successfully made the result text field bigger (the one showing the selected date, by changing the font), make the pop-up bigger (the JXMonthView, also by changing its font), change the picture of the JXDatePicker with a bigger image, set the default date to be the current date, set the date format, etc. This is my code:
private void touch_screen_datepicker(JXDatePicker date_picker) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
JXMonthView monthView = date_picker.getMonthView();
date_picker.setDate(new Date());
date_picker.setFont(new Font(Font.DIALOG, Font.PLAIN, 50));
JButton btn_pick = (JButton) date_picker.getComponent(1);
btn_pick.setBackground(new Color(66, 147, 223));
Image image = toolkit.getImage("/home/adrrian/Image/calendar/" + "calendar image 4.png"); //Земање на сликата за мк знаме
ImageIcon icon = new ImageIcon(image); //Правење на икона
btn_pick.setIcon(icon); //Поставување на иконата
SimpleDateFormat longFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat shortFormat = new SimpleDateFormat("yy-MM-dd");
Date startDate = new Date(0);//01.01.1970
shortFormat.set2DigitYearStart(startDate);
DatePickerFormatter formatter = new DatePickerFormatter(
// invers sequence for parsing to satisfy the year parsing rules
new DateFormat[]{shortFormat, longFormat}) {
#Override
public String valueToString(Object value) throws ParseException {
if (value == null) {
return null;
}
return getFormats()[1].format(value);
}
};
DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);
date_picker.getEditor().setFormatterFactory(factory);
monthView.setFont(new Font(Font.DIALOG, Font.PLAIN, 50));
monthView.setFirstDayOfWeek(Calendar.MONDAY);
}
and this is an image of my final work:
My main problem is how to make the arrow that are changing the months (for example if I go back from this image to show September). I tried listing all of the components, like I did for the button, but still I didn't found anything. Also for better GUI I like to find that dark blue color (where the month is displayed), to make my button the same.
Hope someone can help me. Thanks in advance.
Toolkit toolkit = Toolkit.getDefaultToolkit();
JXMonthView monthView = date_picker.getMonthView();
/*EDITED*/
//1
date_picker.putClientProperty("JComponent.sizeVariant", "large");
monthView.putClientProperty("JComponent.sizeVariant", "large");
//2
date_picker.putClientProperty("JXComponent.sizeVariant", "large");
monthView.putClientProperty("JXComponent.sizeVariant", "large");
//3
date_picker.putClientProperty("JXDatePicker.sizeVariant", "large");
monthView.putClientProperty("JXMonthView.sizeVariant", "large");
//
date_picker.putClientProperty("JDatePicker.sizeVariant", "large");
monthView.putClientProperty("JMonthView.sizeVariant", "large");
SwingUtilities.updateComponentTreeUI(this);
SwingUtilities.updateComponentTreeUI(date_picker);
SwingUtilities.updateComponentTreeUI(monthView);
date_picker.updateUI();
monthView.updateUI();
/*EDITED*/
As #Vighanesh Gursale suggested I insterdet this lines and also did the frame.pack() before setVisible(true), but nothing changes.
I have made some code using nimbus look and feel, i know it is not exact that you want but it is pretty much helpful. Check this code. To perform same you need to find the key of your next and previous buttons key in my case it is Button.margin. Try to use the same key in your code if you are lucky it would work.
import javax.swing.*;
import java.awt.*;
public class Demo {
JFrame frame = new JFrame("");
JButton btn = new JButton("Example");
public Demo() {
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(btn);
frame.setVisible(true);
}
public static void main(String[] args) {
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
Insets insets = new Insets(50, 20, 50, 20); //change the size of button
UIManager.put("Button.margin", insets);
}
catch(Exception e)
{
e.printStackTrace();
}
Demo d = new Demo();
}
}
Also keep in mind to use default look and feel or any other look and feel.
This question is quite old but for everyone who is looking how it can be done
UIManager.put("JXMonthView.monthDownFileName", <PATH_TO_IMAGE>);
UIManager.put("JXMonthView.monthUpFileName", <PATH_TO_IMAGE>);
monthDownFileName refers to previous month
monthUpFileName refers to next month
this will change the image with other of your choice (and size)

Choose Non-default googlecalendar with google-java-client-api

I want to get all the Calendars, which are in my GoogleAccount, using the google java client API.
In my application I want that a user can choose in wich calendar his events will be saved (not only in the default). But therefore I need their CalendarIDs. I don't want that the users have to search their calendar ids to write them by hand into the app.
Would it be possible to create a new Calendar in his account, to write all the events in this new one.
Sorry for my bad English.
Yes of course it is possible.You only have to know the calendarId in which you want to save the new event, and use them with the event insert function.
For example :
Event event = new Event();
event.setSummary("This is my Event");
event.setLocation("127.0.0.1 -- Home sweet Home!!");
ArrayList<EventAttendee> participants = new ArrayList<EventAttendee>();
participants .add(new EventAttendee().setEmail("member#domain.com"));
event.setAttendees(participants);
DateTime start = new DateTime(new Date(), TimeZone.getTimeZone("UTC"));
event.setStart(new EventDateTime().setDateTime(start));
DateTime end = new DateTime(new Date(startDate.getTime() + 3600000), TimeZone.getTimeZone("UTC"));
event.setEnd(new EventDateTime().setDateTime(end));
Event createdEvent = service.events().insert("YourCalendarID", event).execute();
Hope this could help you!

Categories

Resources