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 / GUI / April 2006

Tip: Looking for answers? Try searching our database.

when to call setBounds(), setLocation(), ...

Thread view: 
Kova - 27 Apr 2006 12:31 GMT
Hello.
I'm using BoxLayout and I can't get components to show as I want. I
create components, do setBounds on them, make everything visible and it
dosen't work! It only works if I set bounds after I show them (make them
visible).. why? Of course I want to set where the components will be
displayed first and then display them, not display them and then move
them around... help? Below is example of the problem.

-------------------------------------------------------
import java.awt.Dimension;
import javax.swing.*;

public class TFrame extends JFrame {
    JTextField tf1 = new JTextField("Test 1");
   
    public TFrame() {
        super("TEST");
    setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    tf1.setBounds(50,50,200,75); // dosen't work
    tf1.setMaximumSize(new Dimension(200,75));
    add(tf1);
    setBounds(0,0,400,200);
    setVisible(true);
//    tf1.setBounds(175,50,200,75); // this works
    }
   
    public static void main(String[] args) {
    new TFrame();
        System.out.println("Tnx a lot comp.lang.java.gui! :)");
    }

} // end of class
------------------------------------------------
moumita - 27 Apr 2006 14:35 GMT
instead of using BoxLayout,u can set the Layout as null.
I think it will work..
Kova - 27 Apr 2006 14:46 GMT
> instead of using BoxLayout,u can set the Layout as null.
> I think it will work..

no, dosen't work. I choose BoxLayout becouse it (should) respects
component's proparties. Was worth a shot.

--
Kova
moumita - 27 Apr 2006 15:02 GMT
try this...

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

public class TFrame extends JFrame {
    JTextField tf1 = new JTextField("Test 1");

    public TFrame() {
        super("TEST");
      getContentPane().setLayout(null);//(new
BoxLayout(getContentPane(), BoxLayout.X_AXIS));
        setDefaultCloseOperation(EXIT_ON_CLOSE);

  /***Working now****/     tf1.setBounds(50,50,200,75); // dosen't
work
       tf1.setMaximumSize(new Dimension(200,75));
        getContentPane().add(tf1);
       setBounds(0,0,400,200);
       setVisible(true);
//      tf1.setBounds(175,50,200,75); // this works
    }

    public static void main(String[] args) {
       new TFrame();
        System.out.println("Tnx a lot comp.lang.java.gui! :)");
    }

} // end of class

it's working...:o)
Kova - 27 Apr 2006 16:07 GMT
> try this...
> it's working...:o)

yeah strangely enough this works as expected in this example, but when I
tested it, it dosen't work on my actual application :( I think I'll
listen Thomas and try to do the whole layout properly.
Tnx again.

--
Kova
Thomas Weidenfeller - 27 Apr 2006 14:52 GMT
> I'm using BoxLayout and I can't get components to show as I want. I
> create components, do setBounds on them, make everything visible and it
> dosen't work!

When you use layout managers you should not use setBounds() or
setSize(). You leave the calculation of the sizes and positions to the
layout manager.

If things don't look as you like, you have

- Chosen the wrong combination of layout managers, and/or

- Not configured one or more of the used layout managers as desired.

Work through
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
from start to end. Twice.

/Thomas

PS: If you get the "great hint" to use no layout manager or a null
layout manager, ignore it. Layout managers are an essential part of
AWT/Swing GUIs and you e.g. badly need them to compensate for
cross-platform differences, dynamic resizing, internationalization, etc.
Time spent to learn layout managers is time well spent.

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Kova - 27 Apr 2006 16:12 GMT
thank you for your advice, I'll try to configure layout manager to do
what I need.

--
Kova


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.