Change Swing Component Subclass
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I have an abstract "GridCell" class that extends JLabel and two subclasses of this class: "EmptyGridCell" and "NonEmptyGridCell" that are the same except for the background and the border colors. Basically, when I click an "EmptyGridCell" it has to become a "NonEmptyGridCell" and vice versa which means the component has to change its color and text. The program runs but the component aspect doesn't change. Could you please tell me what I need to change or add to make the program work, here's the code and thanks in advance:
Edited by: braddie on Feb 28, 2010 11:37 AM
Edited by: braddie on Feb 28, 2010 11:38 AM
Edited by: braddie on Feb 28, 2010 11:37 AM
Edited by: braddie on Feb 28, 2010 11:38 AM
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
And in both forums the OP hasn't figured out how to use the "Code" button to post readable code.
posted 15 years ago
In other words, Be Forthright When Cross Posting To Other Sites.
You can't change an object's class, so you have two options:
- remove your old component and add a new one
- don't make sub classes for just the border and color but use properties for them (hint: they already exist)
The latter is the easier of course. If you still want to group the empty / non-empty properties together you can create a custom class. For instance:
Instead of sub classing GridCell, it gets a new field with getters and setters. The setter method would set the color and border as well:
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Michael Dunn wrote:http://forums.sun.com/thread.jspa?threadID=5430018&tstart=0
In other words, Be Forthright When Cross Posting To Other Sites.
You can't change an object's class, so you have two options:
- remove your old component and add a new one
- don't make sub classes for just the border and color but use properties for them (hint: they already exist)
The latter is the easier of course. If you still want to group the empty / non-empty properties together you can create a custom class. For instance:
Instead of sub classing GridCell, it gets a new field with getters and setters. The setter method would set the color and border as well:
brad enddie
Greenhorn
Posts: 3
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Rob. I am not using subclasses just because I need different border and background colors, those are just so that I can tell if the component has been changed. I would have gone with the second solution if I could and if it was just the border and the color but the use of the 3 classes is a requirement. Moreover that's not the actual code it's just to demonstrate what I want to do, if I can make the "example" code work and in a way refresh the frame then I can make the original much longer code work.
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Then you have to remove the EmptyGridCell instance, add a new NonEmptyGridCell (at the same index so the layout manager will treat it in the same way), then call repaint() and revalidate() to make sure the container (JPanel) sees the changes.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
brad enddie
Greenhorn
Posts: 3
posted 15 years ago
Thanks, that's what I ended up doing. I had also forgotten to add the mouseListener to the GridCells. Here's the modified code:
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Rob Prime wrote:Then you have to remove the EmptyGridCell instance, add a new NonEmptyGridCell (at the same index so the layout manager will treat it in the same way), then call repaint() and revalidate() to make sure the container (JPanel) sees the changes.
Thanks, that's what I ended up doing. I had also forgotten to add the mouseListener to the GridCells. Here's the modified code:
| That new kid is a freak. Show him this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









