• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initializing a JPanel's Variables

 
Tom Josephits
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a modified JPanel in my program. Mostly just a minor change to the paint method and a few variables. I have an array of strings which I need to be set according to an XML document. Is there some method I can use that activates once at the beginning of the JPanel to initialize it?
 
Paul Clapham
Sheriff
Posts: 21882
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could write a method to do that, sure. Just call it right after you create the JPanel. Or you could just put the code in the constructor.
 
Tom Josephits
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You could write a method to do that, sure. Just call it right after you create the JPanel. Or you could just put the code in the constructor.


That's perfect. Sorry about the stupid post, I just never imagined overriding the constructor, but it makes perfect sense in hindsight.

Edit: Spoke too soon. Turns out you can't override a constructor, nor can you access local variables or methods outside the decoration. Here's an idea of what i have so far:
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul C thought that modified JPanel meant a subclass of JPanel. That would of course have its own constructors.
 
Paul Clapham
Sheriff
Posts: 21882
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the posted code IS a subclass of JPanel. Just an anonymous one, that's all.

This is just another example of how Swing mercilessly exposes any deficiencies in Java programming ability. It's like getting into a Formula 1 race just after getting your driver's licence.

Anyway there are two ways to put initialization code into that posted code: (1) Make it a named subclass, so you can have a constructor, or (2) Use an instance initializer.
 
Kocha Sapam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subclass it, thats better IMHO like this :
class YourPanel extends JPanel {

String[] Paragraphs;
public YourPanel ()
{
super();
//do your initialization here
}
@Override
public void paintComponent(Graphics g)
{

super.paintComponent(g);
}
}


Edit : Now I have followed the convention I hope :p
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kocha Sapam wrote:Subclass it, thats better IMHO like this :

But, follow the standard Java coding conventions: class names start with an uppercase letter.

Also, UseCodeTags <= link
 
Kocha Sapam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the nice correction. I'm just a beginner, so please bear with me
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!