• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Change Swing Component Subclass

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://forums.sun.com/thread.jspa?threadID=5430018&tstart=0
 
Rancher
Posts: 3325
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22895
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22895
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
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
reply
    Bookmark Topic Watch Topic
  • New Topic