views:

825

answers:

1

I need a simple way to implement a Calendar (similar to google calendar) in java. The calendar must display Monday - Sunday at the top and each hour as a row.

      Monday | Tuesday | Wednesday
08:00
09:00
10:00
11:00

How would one create a calendar in Swing like that? I'm using Netbeans IDE. Each column should be able to hold some text.

Could I just use the table component in swing? If so, can i modify it to meet my needs?

+1  A: 

You would probably want to think about using JTable. You can use an AbstractTableModel to define the dimensions of the table, as well as some other properties, then you can fill in the top row and first column with the desired values. You can also set certain parts of the table editable by your users, in the event that you wanted to be able to input data into the calendar.

For more info, take a look at the JTable documentation.

Tim
Don't do it! JTable sucks, particularly for rich content. I'd recommend using a JPanel with a GridLayout with JPanels for each panel. JTables approach of using renderers for each cell and switching to editors will be wholly innappropriate for a calendar component.
Tom Martin
"Rich content"? The OP specified that each cell had to hold text, not images or anything else. I think using a JTable would be the simpler solution for this problem, given that there's no need to do the hard work of instantiating bunches of JPanels in a GridLayout then dealing with making them editable somehow.For rich content, yes, a bunch of JPanels might beat out a JTable. But for a plain text calendar, I don't see why a JTable is inappropriate.
Tim
True but they also mentioned that it should be similar to Google Calendar which goes way beyond JTable's functionality. Also JTable still sucks. What happens when the OP needs to change the cursor for a particular cell, add a mouseover effect, use wrapped text or have a complex editor containing more than one Component? They'll run into JTable's myriad of shortcomings.
Tom Martin
Okey I will try them both and see which one fits my needs. I will need a mouse over effect and also actually a drag n drop functionallity to move events. It doesn't need to look good in any way, just work :)
Baversjo