• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

setBounds() method doubt in swings

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just run a program from a book,which creates 6 buttons on a frame.


The code that creates a frame,adding components to it,is OK with me.

But what confused me lot is


bt1.setBounds(30,10,80,60);

bt2.setBounds(130,10,80,60);

bt3.setBounds(30,70,80,60);

bt4.setBounds(130,70,80,40);

bt5.setBounds(30,130,80,40);
bt6.setBounds(130,130,80,40);

here the variables bt1,bt2,bt3,bt4,bt5,bt6 are Button Reference variables.

The first two arguments in setBounds() method are X and Y cooridinates respectively right?

If so,the buttons in the frame should be displayed in this fashioin


Button5 Button6


Button3 Button4


Button1 Button2



Because "Y" coordinate for Button1 and 2 is 10,so they should be at the bottom.


For Buttons 3 and 4 it is 70,so they should be above Buttons 1 and 2.


For buttons 5 and 6 Y coordinate is 130,so they should be on top.





But The frame displayed is as Follows



Button1 Button2

Button3 Button4


Button5 Button6




Actually thought twice and thrice before posting this,questioning myself "Did I forget my basic graph pointing?"

tried on paper too.

But could not get why the displayed frame is different from my expectations?


Hope somebody clears.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
0,0 location of any container in Swing is the top left corner and not the bottom left.

Any specific reason why you are calling a setBounds() instead of using some layout manager? The setBounds() defines absolute positions where as the layout managers use relative positions. This is especially useful when the UI is resized.

In spite of this, if you really need to use setBounds(), you will have to give appropriate x,y locations starting from the top left corner as 0,0.
Also, you might want to take a look at the translate() method in the Graphics class.
 
vatsalya rao
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Maneesh.



Since yesterday I staretd programming GUI in my machine.

Till now I never wrote a siingle program abt GUIs.Just gone through tutorials in the net.


I used Flow Layout Manager,GridLayout and BorderLayout.Just to experiment I set setLayout to null and set bounds.


Two more issues are bothering me.


1) What ever the layout manager I use, If I resize the frame I created,I cant view the resized frame.it is oon the top of my command prompt window but just as a thin liine showing Frame title and with maximize,minimize and close buttons.

Why it is so?

2) If I wont write System.exit(0) in my code Iam left with an open Dos prompt if I write the newly created frame is just disappearing after creation.


Hope you will clear these too.

I had gone through translate(int x,int y) in Graphics class.

it will change the origin to the position indicated in x,y fields right?

Why did you ask me to go through this method?


My code is as follows



import java.awt.*;

import javax.swing.*;

import java.awt.event.*;


public class createFrame extends JFrame

{


public static void main(String[] args)

{

createFrame f1 = new createFrame();

}



public createFrame()
{

setTitle("frame 1");
setVisible(true);

Container c = getContentPane();


c.setLayout(null);




JButton bt1 =new JButton("Button1");

JButton bt2= new JButton("Button2");
JButton bt3= new JButton("Button3");
JButton bt4= new JButton("Button4");
JButton bt5= new JButton("Button5");
JButton bt6= new JButton("Button6");

JLabel lb1= new JLabel("Label1");
JLabel lb2=new JLabel("Label2");


JTextField txt1=new JTextField();
JTextField txt2=new JTextField();

c.add(bt1);
c.add(bt2);
c.add(bt3);
c.add(bt4);
c.add(bt5);
c.add(bt6);


c.add(lb1);
c.add(lb2);


c.add(txt1);
c.add(txt2);



bt1.setBounds(50,30,80,60);

bt2.setBounds(140,30,80,60);

bt3.setBounds(50,120,80,60);

bt4.setBounds(140,120,80,60);

bt5.setBounds(50,190,80,60);
bt6.setBounds(140,190,80,60);


lb1.setBounds(90,250,80,60);

lb2.setBounds(170,250,80,60);


txt1.setBounds(300,30,80,40);
txt2.setBounds(300,120,80,40);

// System.exit(0);
}
}



The last thing is is it ok to start programming GUI with swings rather than AWT classes?
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) What ever the layout manager I use, If I resize the frame I created,I cant view the resized frame.it is oon the top of my command prompt window but just as a thin liine showing Frame title and with maximize,minimize and close buttons.



You need to set the size of the frame explicitly by calling frame.setSize() or pack(). The frame defaults to 0,0 size (excluding the title bar).

2) If I wont write System.exit(0) in my code Iam left with an open Dos prompt if I write the newly created frame is just disappearing after creation.



You need to call the frame.setVisible(true) on the last line in your createFrame().

The last thing is is it ok to start programming GUI with swings rather than AWT classes?


Swing are light weight components where as AWT are heavy weight. You might want to google to find out what it exactly means.

Why did you ask me to go through this method?


If you wished, you could translate your origin point from top left (default) to bottom left (where you desired). This way you would not need to change any of your co-ordinates.

In generic terms, I code in the following manner:
1) Main class which has the main method. This method creates a new MyFrame() object and then I call a setVisible(true) on that object
2) MyFrame class which extends JFrame. The constructor will do stuff like, size, location, title, icon. It will also have 2 methods. initComponents() is where I create all the components which I am going to use in the UI and buildUI() where I actually define the layout and add the components to their respective parents. This way I segregate both functionalities which are extremly helpful later on as I know which code to look at if there is some data problem or some layout problem.

Best way to learn swing is to design some UI on some paper(which you are already doing) and then try to code it. At a beginner stage I would advise you to not go in for drag and drop style UI builders. Get a hang of how the UI works and then you can move to such builders if required.

Hope this helps.
Best of luck
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vatsalya rao:
2) If I wont write System.exit(0) in my code Iam left with an open Dos prompt if I write the newly created frame is just disappearing after creation.



You would expect the open DOS prompt while the frame is up and running.

If the problem is that you aren't getting a prompt back when you close the frame, then add the following line:

public createFrame()
{
setDefaultCloseOperation(EXIT_ON_CLOSE); // <--- add this line
setTitle("frame 1");
setVisible(true); // <--- see note below
Container c = getContentPane();

If you want a prompt even while the app is running, then launch with javaw createFrame instead of java createFrame (but add the setDefaultCloseOperation line anyway).


A couple of notes while I'm here:

(1) It is better, I think, not to call setVisible(true) until after you've added all the components and such. I would remove it from the beginning of the createFrame() constructor and place it (A) at the end of the createFrame() constructor, or (B) in main() like this:

public static void main(String[] args)
{
createFrame f1 = new createFrame();
f1.setSize(400, 400); // <--- added
f1.setVisible(true); // <--- added
}

(2) To follow convention you would begin the name of your class with a capital letter, so createFrame -> CreateFrame.

(3) I realize that you are using the null layout manager and setting the bounds manually as an exercise in exploration, and that's fine. But for real code a null layout manager should almost never be used.
[ January 23, 2008: Message edited by: Brian Cole ]
 
vatsalya rao
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome answers.Thank you Maneesh and Brian for your clear cut explainations.


Iam out of my issues now and started programming in EVENT HANDLING.

Thanks once again.
 
reply
    Bookmark Topic Watch Topic
  • New Topic