This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Change Swing Component Subclass  RSS feed

 
brad enddie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Dunn
Ranch Hand
Posts: 4632
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And in both forums the OP hasn't figured out how to use the "Code" button to post readable code.
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
brad enddie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!