Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 13 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

SwingX: Introduction to the JXDatePicker

At 11:56 PM on Sep 14, 2005, R.J. Lorimer wrote:

The JXDatePicker from the SwingLab's SwingX project is a quickly maturing, and generally useful 'calendar' component for Swing. To give you a quick idea of what I mean by calendar component, here is what the JXDatePicker looks like using the Ocean look and feel and my System look and feel for Windows XP:

To quickly show you the hoops of working with the JXDatePicker I have written a quick example program that responds to changes in the JXDatePicker. Note that I simply print the user's selected date in a JLabel, which, for all intents and purposes is pointless because the date picker already comes with a pre-formatted text field for that same purpose, but, I needed to do *something* with the date, and I'm not finding the creativity to come up with a better example.

final JLabel label = new JLabel();
label.setText("Choose Date by selecting below.");
 
final JXDatePicker datePicker = new JXDatePicker(System.currentTimeMillis());
datePicker.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		label.setText(datePicker.getDate().toString());
	}
});
 
 
frame.getContentPane().add(label, BorderLayout.NORTH);
frame.getContentPane().add(datePicker, BorderLayout.CENTER);

Once you've integrated the date picker in to your application, you need to learn what customization options you have available to you.

User-Provided Format Processing

One of the keys to the JXDatePicker is the embedded text field. This allows keyboard-savvy users to bypass the calendar component altogether, and simply enter the date manually. The expectation is that once they are done, the date picker will still fire events that your application can respond to; where yoru application code isn't concerned as to whether they used the calendar drop down, or simply used the text field. The trick is that every user has a potentially different idea of how to input a date. Sure, you can provide them with some documentation as to what format you expect, but wouldn't it be nice if you could understand multiple commonly provided formats. By using either the JXDatePicker.setFormats(String[]) or JXDatePicker.setFormats(DateFormat[]) methods, you can. Each entry in the provided array is a possible acceptable format. The String[] in the former method is simply composed of String patterns that apply for the java.text.SimpleDateFormat class - so read the documentation there to learn what options are available to you.

In reality, however, the JXDatePicker already has multiple common formats available. The only remaining piece of the puzzle is what format will be used in the text field if the user selects a value in the calendar, rather than entering it manually. The answer is: the format in the first index of the array provided (or the long format of the built-in JXDatePicker formats), or the format the user provided if they have ever manually entered a value - once they have entered a value manually, it remembers what format they used, and outputs any further selections in that format.

The Link Panel

At the bottom of the calendar view, there is a little panel with a link with today's date:

This panel can be customized freely by you. First, you can specify the date that should be used, and the message format that should be used with that date:

datePicker.setLinkDate(System.currentTimeMillis(), new MessageFormat("The time is now {0}"));

Alternatively, you can completely replace the link panel with your own implementation (with whatever you want in reality) by using JXDatePicker.setLinkPanel(JPanel) .

The Month View

If the date picker is too coarsely defined for you, and you don't want to have all of the popup, button, and text field thrown together, you can consider the JXMonthView. This view operates identically to the JXDatePicker calendar window, but it doesn't have the associated text field or button, and can simply be embedded into a panel. Here is some example code using that:

final JLabel label = new JLabel();
label.setText("Choose Date by selecting below.");
 
final JXMonthView monthView = new JXMonthView(System.currentTimeMillis());
monthView.setSelectionMode(JXMonthView.MULTIPLE_SELECTION);
monthView.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		label.setText(monthView.getSelectedDateSpan().getEndAsDate().toString());
	}
});
 
 
frame.getContentPane().add(label, BorderLayout.NORTH);
frame.getContentPane().add(monthView, BorderLayout.CENTER);

Here is what it looks like:

You'll notice that in this case, my code is working a little differently to get the selected value. That is because the month view supports multiple selection types - you can technically set these on the full date picker as well by calling JXDatePicker.getMonthView() and setting them on the month view, but it really doesn't apply in that case. The month view allows for single selection, multi-selection (via mouse dragging), week-level selection (simply via clicking), and no selection, which for the most part makes the month view a read-only component.

Happy coding, and stay tuned for more journeys into the SwingX world in the near future.

Until next time,

R.J. Lorimer
Contributing Editor - rj -at- javalobby.org
Author              - http://www.coffee-bytes.com
Software Consultant - http://www.crosslogic.com

1 . At 8:14 AM on Sep 15, 2005, keke wrote:
  Click to reply to this thread Reply

Displaying week numbers?

I just wish it could display calendar week numbers (the ordinal number of the week: 1..52) on the left hand side. -Keke
2 . At 9:38 AM on Sep 15, 2005, R.J. Lorimer wrote:
  Click to reply to this thread Reply

Re: Displaying week numbers?

Sounds like a great request for enhancement for the SwingX team. I'd recommend posting your thoughts here:

https://jdnc.dev.java.net/servlets/ProjectMailingListList

Regards,
Best, R.J. Lorimer
3 . At 1:18 PM on Sep 15, 2005, Richard Bair DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Displaying week numbers?

This is the forum link:

http://www.javadesktop.org/forums/forum.jspa?forumID=53&start=0
4 . At 6:29 PM on Sep 15, 2005, Sumit Kishore wrote:
  Click to reply to this thread Reply

Re: SwingX: Introduction to the JXDatePicker

(1) SwingX has JXComboBox which allows complex components in the combo popup. Is this also targeted for inclusion in that widget? The use case is very rarely to display the whole calendar view at all times, but rather to show a representation (the date) of it and pop up the picker on demand.

(2) I saw that the JXDatePicker had no notion of boundedness (allowing date selection between a start and an end date). Please consider this seriously. The other bounded date selection widget in Swing, JSlider, has grave problems, both with its bugginess and usability.
5 . At 7:56 PM on Sep 15, 2005, R.J. Lorimer wrote:
  Click to reply to this thread Reply

Re: SwingX: Introduction to the JXDatePicker

Sumit,

Let me see if I can respond to your points in turn:

1.) The base date picker I showed at the top, while not being represented very well, is in it's default case just a formatted text field that allows the user to input whatever they want - if they hit the expansion button next to that text field, they are given the calendar widget, at which point they can select their time via the widget.

2.) JXDatePicker doesn't carry the bounding rules, instead the underlying JXMonthView does - which I have described in the latter part of my tip. You can manage bounding with the JXDatePicker through that control. Try it out!

Regards,
Best, R.J. Lorimer
6 . At 5:46 AM on Sep 16, 2005, Christopher Brown DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Displaying week numbers?

> This is the forum link:
>
> http://www.javadesktop.org/forums/forum.jspa?forumID=5
> 3&start=0

And this is a posting I made on the forum way back in July :-) :
http://www.javadesktop.org/forums/thread.jspa?threadID=14843&messageID=97928#97928

I also found a bug at that time (haven't tried it since to see if it has been fixed...):
http://www.javadesktop.org/forums/thread.jspa?threadID=14840&messageID=98012#98012

I filed some issues but can't find them again in their awful bugtracker...

- Chris
7 . At 10:41 AM on Sep 16, 2005, Richard Bair DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Displaying week numbers?

> I also found a bug at that time (haven't tried it
> since to see if it has been fixed...):
> http://www.javadesktop.org/forums/thread.jspa?threadID
> =14840&messageID=98012#98012
>
> I filed some issues but can't find them again in
> their awful bugtracker...

I know, the bug tracker is horrible. I hate it. I really hate it :)
8 . At 4:52 AM on Apr 14, 2006, Erik van Oosten wrote:
  Click to reply to this thread Reply

Re: SwingX: Introduction to the JXDatePicker

Re 2.) I did not find methods to restrict the date range that can be selected from. The only thing that comes close is the ability to disable the month forward/backward buttons.

Another thing I noticed is that there are no events whatsoever when the date is entered manually.

It seems quite hard to make a feature-complete date picker :)

Regards,
Erik.
9 . At 7:46 AM on Mar 19, 2007, Israr Ahmed wrote:
  Click to reply to this thread Reply

Re: SwingX: Introduction to the JXDatePicker

JDatePicker performes the same role as JSDatePicker. I used it in my application.
There certainly have been performance issues with Java. We've been working really hard on them. The primary way we've attacked the problem is with advanced virtual machines. The performance has been getting very nice. --James Gosling, 1999.
10 . At 4:56 AM on Apr 5, 2007, Sergueï wrote:
  Click to reply to this thread Reply

Re: SwingX: Introduction to the JXDatePicker

Hi!I'm using JXDatePicker in my multilanguage application. I would like days to be changed when a user change his preferences option via MenuItem. But it doesn't work with JXDatePicker neither with JXMonthView. Even when I try to hard code like
.setDefaultLocal(Locale.FRENCH) or
.setDefaultLocal(Locale.FRENCH)

nothing changes. I always have days according to the user System Locale preferences. Is it possible to change it anyhow?
Thank you.
11 . At 7:10 AM on Apr 5, 2007, Sergueï wrote:
  Click to reply to this thread Reply

Re: SwingX: Introduction to the JXDatePicker

I've resolved the problem. It's OK now.
12 . At 3:51 AM on Jul 9, 2007, Andreas Lövgren wrote:
  Click to reply to this thread Reply

Re: SwingX: Introduction to the JXDatePicker

Hi, does anybody know if it's possible to change the calenders initial selection date ?

Im using two calenders(from date and tom date). And I would like the tom dates initial selection to be same as the first calender(from date), instead of todays date.
Just a tip how to change the intial date during runtime would help me alot.

Thanks in advance
Andreas
13 . At 5:31 AM on Jul 20, 2007, Jeanette Winzenburg wrote:
  Click to reply to this thread Reply

Re: SwingX: Introduction to the JXDatePicker

> Just a tip how to change the intial date during
> runtime would help me alot.

not sure what you mean with "initial" - you can always call

myDatePicker.setDate(initialDate);

If you want an additional constructor, please file an enhancement issue in SwingX issue tracker.

Jeanette

thread.rss_message