This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error in JPanel - JComponent link - paintComponent not paining on the main class

 
Badea Robert
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well hello everyone. Since this is my first post here i'd like to start by saying hi to everyone. Now, lets talk about my issue:

Frame =


class1 =


and class2


the 2nd class is not printing anything, although it is called ( tested with println ). I expect this to be because the getParent function is returning null, however i cannot see the error in the code that is not allowing the classes to communicate with each other.
I appreciate if anyone could point out the error in code. Thanks !

edited: apparently the getParent is not returning null, and it is working properly. i tested it outside the constructor of the class. I still cannot understand where the issue is =)
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getParent() returns null because in the constructor the instance hasn't been added to its parent yet; that only occurs after the constructor ends.

The reason nothing is printed is because that's what you specified - the paint method is overridden to do nothing. That means no painting at all takes place. Just remove that method.
 
Badea Robert
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:getParent() returns null because in the constructor the instance hasn't been added to its parent yet; that only occurs after the constructor ends.

The reason nothing is printed is because that's what you specified - the paint method is overridden to do nothing. That means no painting at all takes place. Just remove that method.

That code is just an example, the paint from class1 is working properly and paint everything , and the paintcomponent from class2 is aswell called (tested with println). however,the actual painting (drawstring in my case) doesn't take place. ( i cannot see the string painted on the frame ).
apparently it is working if i am explicitly call the class ( class2 temp = new class2(); temp.paintComponent(g); within the class1 paint method ).however, this is not what i was hoping for. i expected the class2 paintcomponent to draw the string on the screen via the call from its parent (super.paintcomponent).
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm not sure, but i think you should be calling paintComponent() in the JPanel(class1) not paint()
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the size of the "class2" component when it's displayed? (By the way "class2" is a horrible name for a class.) Perhaps your drawing is taking place outside its bounds. Or perhaps its size is zero.
 
Badea Robert
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:What is the size of the "class2" component when it's displayed? (By the way "class2" is a horrible name for a class.) Perhaps your drawing is taking place outside its bounds. Or perhaps its size is zero.

Well, i overwrote the name of the class for this example. the classes have tons of code implemented in it so i just explained my issue in a simpler matter. (no,the class is not called class2, no,the paint function is not empty :-P )
However,thanks for pointing the size,i completely forgot about it,it wasn't set at all.however,that wasn't the issue.By directly calling the func inside my paint method ,the text is displayed ( class2 object .paintComponent(g); )


Actually this entire issue has been caused because i am trying to find a way to use a function to draw on the screen only when i am telling it to do so ( and without using the paint from a super class so i wont require to go through an if statement on each call until it reaches his purpose).However, the problem is with the Graphics argument,apparently ,because i require it in order to do the painting and the only place i can drag him as an argument is within the paint function..any ideas how i could do it without ?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Badea Robert wrote:Actually this entire issue has been caused because i am trying to find a way to use a function to draw on the screen only when i am telling it to do so ( and without using the paint from a super class so i wont require to go through an if statement on each call until it reaches his purpose).However, the problem is with the Graphics argument,apparently ,because i require it in order to do the painting and the only place i can drag him as an argument is within the paint function..any ideas how i could do it without ?


The normal way to do that would be like this:

When you want the view of a component to change on your command, you call its "repaint" method. That causes the Swing code to call the component's "paintComponent" method. So the code in that method would have to display whatever you wanted to display at that particular point in time.

This would involve the commanding code changing some shared data and then calling "repaint" on the component. The component's "paintComponent" method would then access that shared data and display it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic