Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Position a JPanel with code

Status
Not open for further replies.

murrayja

Programmer
May 6, 2000
90
US
I am using NetBeans to convert an app from Visual Basic to Java. I have a JFrame on which I want to place two panels each taking half of the available JFrame space. I am using the absolute layout manager on the JFrame. When I run the app with one panel it refuses to occupy the left half of the JFrame space. here is my initialization code.....

public Titlebar() {
initComponents();
// -- set class to full screen --
Dimension d = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setLocation(0,0);
setSize(d.width, d.height);
// -- set panel to fill left half of frame --
pnlFields.setLocation(0,50);
pnlFields.setSize(d.width / 2, d.height - 50);
}

thanks
jim murray
 
..so where exactly does it get drawn? Does it appear at all? What is happening in the initComponents() method? Have you added the pnlFields object to the Frame? For example (from memory, don't pick me up on incorrect signatures)...
Code:
frame.getContentPane.add(pnlFields, null);

Tim
 
At design (NetBeans) I position the JPanel immediately below the menubar of the JFrame with W=H=500.

At run time it is in the same place, ie the width and height have not been changed to fill 1/2 the JFrame area.

InitComponents has this relating to the JPanel ...

pnlFields.setMinimumSize(new java.awt.Dimension(250, 250));
pnlFields.setPreferredSize(new java.awt.Dimension(500, 500));
getContentPane().add(pnlFields, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 250, 320));
 
Okay, try doing
Code:
this.getContentPane().validate();
at the end of your TitleBar constructor method.

Tim
 
Strange.. I knocked up a quick program using your code and it works fine. The difference I had is that I'm not using NetBeans, so when you set the layoutmanager for the frame, do
Code:
this.getContentPane().setLayoutManager(null);
and add the panel with
Code:
this.getContentPane().add(pnlFields, null)

Tim
 
I had just tried setting the layout manager of the JFrame to null and it works ok now. I guess the lesson is to avoid a layout manager.
thanks
jim
 
Well, you do get benefits from using one of the out-of-the-box Java LayoutManagers. See thread269-988099. The need to cling to null layouts is a hang-over from VB development IMO. LayoutManagers are more work but the results are worth it when developing application to run on machines with a variety of different screen resolutions.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top