Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

setBounds() does x/y position; ignores w/h sizing

Thread view: 
xkred27 - 09 Apr 2004 00:46 GMT
Hello.

I'm fairly new to Java and this is my first post.  In the following app, I
display some buttons inside a JPanel inside a JFrame.  The last command in
the constructor is a call to setBounds(), with which I try to position the
JFrame more toward the center of the screen, and size the JFrame at 100w x
400h pixels.  I've also tried moving the setBounds() call to the bottom of
main().  In either case, the JFrame is positioned correctly (at 0,0 when
setBounds()  gone; at 100,250 when setBounds() used) , but the width/height
numbers are ignored, resulting in a JFrame maybe 500w x 50h--emphatically
not 100w x 400h.

Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Rebate extends JFrame implements ActionListener {
JButton create, update, delete, exit;

public Rebate() {
 super ("Process Rebates");
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 create = new JButton("Add New Rebate");
 update = new JButton("Update Existing Rebate");
 delete = new JButton("Delete a Completed Rebate");
 exit = new JButton("Quit");
 create.addActionListener(this);
 update.addActionListener(this);
 delete.addActionListener(this);
 exit.addActionListener(this);
 JPanel pane = new JPanel();
 pane.setLayout( new GridLayout() );
 pane.add(create);
 pane.add(update);
 pane.add(delete);
 pane.add(exit);
 setContentPane(pane);
 setBounds(100,250,100,400);
}

public static void main (String[] args) {
 Rebate r = new Rebate();
 r.pack();
 r.show();
}

public void actionPerformed (ActionEvent e) {
 Object o = e.getSource();
 *
 * bunch of code which displays JOptionPane dialog boxes showing
 * what button was clicked
 *
 System.exit(0);
}

}

Thanks.

Peter

Anti-spam = remove work dot x
Andrew Thompson - 09 Apr 2004 02:10 GMT
> ...numbers are ignored, resulting in a JFrame
> maybe 500w x 50h--emphatically not 100w x 400h.
.....
>  public static void main (String[] args) {
>   Rebate r = new Rebate();
>   r.pack();

This is what is doing it..
Check the JDocs for Window.pack()
"Causes this Window to be sized to fit the
_preferred_ size and layouts of its subcomponents."

I suspect you'll find your components
_prefer_ to be the size that pack
results in.

It is important to understand that setting
the size of things is _generally_ a bad thing
to do, it destroys cross-platform UI's for
starters.  It is besst to use the appropriate
layout manager(s) and let the UI find it's own
size.

If there _is_ a component with no definite size
(a drawing canvas, for instance) set it's minimum
size and rely on that to fill the GUI out.

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Peter - 09 Apr 2004 04:06 GMT
Andrew Thompson <SeeMySites@www.invalid> wrote in news:1syee76pjo2lq
$.1ne0o82h5r174.dlg@40tude.net:

>>   r.pack();
>
> This is what is doing it..

Thank you very much.  I cloned some of this program from a Teach Yourself
Java in 21 Days.  I've been pretty diligent about making sure I know what
cloned code was doing, but pack() slipped through the cracks.

Signature

Peter

Anti-spam =
  a) Remove NS_
  b) Change 3 zeroes to alpha O's



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.