File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes error in JPanel - JComponent link - paintComponent not paining on the main class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "error in JPanel - JComponent link - paintComponent not paining on the main class" Watch "error in JPanel - JComponent link - paintComponent not paining on the main class" New topic
Author

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

Badea Robert
Greenhorn

Joined: May 03, 2013
Posts: 3
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Badea Robert
Greenhorn

Joined: May 03, 2013
Posts: 3
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

i'm not sure, but i think you should be calling paintComponent() in the JPanel(class1) not paint()


SCJP
Visit my download page
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: May 03, 2013
Posts: 3
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error in JPanel - JComponent link - paintComponent not paining on the main class