File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes calling paint from another 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 "calling paint from another class" Watch "calling paint from another class" New topic

calling paint from another class

Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
hi ,
simple problem but cant rack my brains enuf :-)
Ok, now I have 2 classes which extends from JPanel each, one of them ( p1) has a button draw, once I click on draw I should be able to draw a line in the other panel ( p2) but without creating an object of p2, how do I do it?
for eg.
class p1 extends JPanel
class p2 extends JPanel
//draw line in p1 ..

- Vinod<br />-------<br />SCJP2
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
On Tony Alicea's Java Page you'll find a demonstration of Inter-Applet Communication.
Good Luck.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Vinod,
If you have common parents the problem is quite easy. You can just get the parent and then use the sibling object array to get a handle on the other panel. The code below shows an example.

If you don't have a common parent, you will need to pass a reference to the first panel into the constructor of the second in order to have a handle on the component you want to paint into.

Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
Thanx guys for the feedback, ..Manfred, mine is something similar to the commonparent types except that commonparent is a frame itself, also I just dont need to repaint, based on values obtained from some table based operation in p1 I have to drawrect in p2..& this is dynamic..that means the Graphics context object needs to be active all the time..
Just doing drawrect was creating problems so I tried with adding JLabels with borders & empty text so it looks like rectangles, works out somewhat ok, but it would be gr8 to use graphics object & tinker with drawRect.
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
Oops sorry , I would like to correct the message above, commponparent is a Panel like MyPanel...
It is sorta covered in the JavaRanch Style Guide.
subject: calling paint from another class
Similar Threads
load Jpanels from seperate classes
Can anybody help??
Lines disappear when scrolling
flicker in my program
Marcus Green - Exam 2 - Question 9