Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Trying to extend and add a panel to the frame created in the parent 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 "Trying to extend and add a panel to the frame created in the parent class" Watch "Trying to extend and add a panel to the frame created in the parent class" New topic
Author

Trying to extend and add a panel to the frame created in the parent class

david mohr
Greenhorn

Joined: Jan 13, 2002
Posts: 12
I am trying to have a base class that will create a standard frame. Then have subclasses add a panel to the frame created. This is the code I have come up with so far. I don't know if this is the best way to do this. If anybody has any suggestions please feel free to comment.
Thanks for all your help.
//this is the parent class
import javax.swing.*;
import java.awt.*;

class parentcls
{
public int width = 0;
public int height = 0;
public String practice = null;
JButton b = new JButton("parentbutton");
public parentcls (int wid, int hit, String practice){
width = wid;
height = hit;

JFrame fr = new JFrame(practice);
Dimension dim = fr.getToolkit().getScreenSize();
int screenWidth = dim.width;
int screenHeight = dim.height;
int frameWidth = width;
int frameHeight = height;
fr.setSize(frameWidth,frameHeight);
fr.setLocation((screenWidth-width)/2,(screenHeight-height)/2);
Container mainpanel = fr.getContentPane();
mainpanel.add(b);
fr.setVisible(true);
}
public static void main(String s[]){

new parentcls (300,300,"parent");
}
}
//This is the child class
import javax.swing.*;
import java.awt.*;
public class childcls extends parentcls {
public childcls(){
super(200,200,"child");
}
public static void main(String a[]){

new childcls();
}
}
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
I am not seeing what coding it that way is buying you. Could you provide a little more information on what you are trying to do. It is difficult to comment on design when you don't give much info. It is like asing us to describe a house by looking through a keyhole.
david mohr
Greenhorn

Joined: Jan 13, 2002
Posts: 12
I am trying to have the base class create a standard frame and use that frame in several lower level classes. What I want to do is in the child class just call the constructor of the parent class to create my frame then in the extended class add the panels and components in each extended class. I guess I am just being lazy and do not want to type the same code to create a standard frame over and over approximately 10 times or more.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi David,
It is a valid approach. However, I would remove the setVisible call because the children should be doing that after they have inserted all the components. SetVisible calls the Frame layout method and since you haven't placed any components yet it will need to be called later anyway ...
You might also want to add a windowListener into your parent frame because that also gets tedious.
Regards,
Manfred.
david mohr
Greenhorn

Joined: Jan 13, 2002
Posts: 12
Thanks Manfred for your advice. Unfortunately,
I am having a hard time coming up with the code that will make it work.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
My problem with the way it is coded is, he creates the JFrame within the constructor. He can't really change the frame in any way. With the way he has it coded he would do just as well calling a method that creates a frame and returns it. Now if he extended JFrame and put some common methods in then I could see more use.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi David,
Paul is correct. You probably would do better to extend the JFrame and then add the centering stuff onto it.
Look at the code below and see how it works.

Enjoy,
Manfred.
david mohr
Greenhorn

Joined: Jan 13, 2002
Posts: 12
Thanks Manfred for the code, and Paul for the helpful advice. This code should be very helpful to anybody needing to create a bunch of windows. I think it is a great way to take advantage of the OO concept.
[ January 15, 2002: Message edited by: david mohr ]
Alex Zhang
Ranch Hand

Joined: Apr 19, 2001
Posts: 68
Hi Is there any additional features that the child frame would provide instead of the parent. As I cannot see why you need to have extends the parent class. I think if U are just creating a new instance of JFrame with no additional features, instantiating the parent class for the other Frame is already enough?
Do U agree? :roll:
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
He was just trying to get the concept down. You are right as is isn't very helpful. You could also couple it with an interface to force things like, updating a db, refreshing the screen (for when you only want to set the frame invisible), and things like that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to extend and add a panel to the frame created in the parent class
 
Similar Threads
Dispose
GUI Issues. Ghosting when moving JLabel via Mouse
JComponent problem ?
Rotating a JWindow with multiple JPanels
Display Frame on Center of Screen