Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing Inner Classes  RSS feed

 
Jon Parise
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Everyone,

So I am attempting to do some Swing programming today. I'll be honest I am not that great at Swing. I'm pretty good at Java in general, but not at making UIs in Swing.

What I am trying to do is make a nice little Date chooser popup to allow the user to easily select a date. I know Java doesn't have one built in, but it really doesn't seem that hard to build.

The plan is to create a Class called DateChooserPanel that extends JPanel. From there I am create a method called updatePanel(int month, int year). When called it will generate a JLabel for each day of that month year combo.

The problem I am having is adding the click event to the labels. I know I need to use an Anonymous Inner Class since I am dynamically creating the UI each time the next/prev button is clicked. I also know how to add the inner class. The problem I have is making it usable really.

I figured I could make it hold a reference to the parent frame that called it and use that to tell the parent which day label is selected. I also though a slightly better solution might be to overload JLabel and make it hold a boolean value indicating whether it is selected or not. This way when the DateChooseJPanel needs to know which label is selected it could simply look through it's ArrayList of them to see which is selected.

Basically, I am slightly confused and want to make a nice little pluggable UI component. I know people have created similar things online and all, but I want to make it myself to learn a little more about swing.

Sorry for the long windedness. Here's the code so far:

DateChooseJPanel:


DayLabel:



DateLabelMouseAdapter:


I think I am on the right track...am I totally off base?
 
Jon Parise
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could this work for the Anonymous Class?

 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jon Parise:
The problem I am having is adding the click event to the labels. I know I need to use an Anonymous Inner Class since I am dynamically creating the UI each time the next/prev button is clicked. I also know how to add the inner class. The problem I have is making it usable really.

I figured I could make it hold a reference to the parent frame that called it and use that to tell the parent which day label is selected. I also though a slightly better solution might be to overload JLabel and make it hold a boolean value indicating whether it is selected or not.


Swing already has a component that handles click events and keeps
a boolean value indicating whether it is selected or not. It's called
JButton. (Or perhaps JToggleButton would suit your needs better.)

If you're interested, there have been a couple threads (one, two)
about making JButtons look less like buttons and more like labels.
[ April 25, 2008: Message edited by: Brian Cole ]
 
Jon Parise
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought about using a toggle button and trying to make it look like a label.

Instead I thought it might just be easier to use a label with a click event
I think I have something that should work now. I'm not sure it is a great solution, but I think it will work. I was mainly asking because I want to know how to do this right. I can certainly hack something together, but that won't necessarily make me a better programmer. I figured I would check and see if I was on the right track or out in left field somewhere.

I simply altered the DateChooserPanel like so:



I have not yet tested it out of course, so I can't verify it works. Is there anything glaring or even subtly wrong with that approach?

The rest of the DateChooser should be fairly easy once I can dynamically create the day labels.
 
Jon Parise
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I just figured out the key to this Anonymous Inner Class stuff.

The inner class can call methods of the outer class right? If thats the case all I need to do is make an outer method that takes and integer to represent the selected day. Then I won't need to overload the mouse adapter or store an arraylist of labels.

I think that would be a much better solution right?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!