CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2006
    Location
    Baltimore, Maryland, USA
    Posts
    104

    Using Swing and Placing Components

    ..how difficult can this really be.

    I'm having so much trouble placing my components on my "frame" that it is making my head. I don't understand all the different layouts, and I heard GridBagLayout is the better one to use. Using GridBag, I can't put a component to the top, others near the middle of the frame, and my button on the bottom right. The coordinate system is just not working the way I want.

    Is there another layout where I can say "put this button at x=780, y=800" while I have the bulk of my components near a x=400, y=300 coordinate area?

    I think I need some basic help, or some helpful examples, or something... I would like to do this without a visual editor plugin, but I didn't except all this trouble I'm having.

    Can you help/suggest anything?
    -= the best is yet to come =-

  2. #2
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Re: Using Swing and Placing Components

    why don't you do null..you can put everything where you want..

    Please use code tags like this :
    [SIGPIC][/SIGPIC]
    Code Your Dreams

  3. #3
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Using Swing and Placing Components

    Have you read this:

    http://java.sun.com/docs/books/tutor...ybigindex.html

    ...specifically the section 'Creating a GUI With JFC/Swing -> Laying Out Components Within a Container'?

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Using Swing and Placing Components

    As Holestary said, if you set a null layout, you can place components by co-ordinate, but this is generally considered to be a Bad Idea, because it doesn't scale, takes a lot of work, and is a nightmare to maintain or change.

    It's well worth learning to select and use layouts. See Using Layout Managers

    I hear and I forget; I see and I remember; I do and I understand...
    Confucius
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Jan 2006
    Location
    Baltimore, Maryland, USA
    Posts
    104

    Re: Using Swing and Placing Components

    ... the text at Sun for me isn't that well explained. I did look throughout and still didn't get a good feeling after reading/trying it. Like, is the document saying I shouldn't manually code by hand when using a Layout Manager instead use a tool ie. NetBeans IDE?

    I would like to use GridBagLayout since it is so flexible (despite it being so complex), but I can't get it the exact way I want. I want my grid's origin to start at the top-left on my panel; how do I get it there? The borderlayout doesn't have a setting for that.
    -= the best is yet to come =-

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Using Swing and Placing Components

    Quote Originally Posted by capitolc View Post
    Can you help/suggest anything?
    Have you checked out the Swing pages at Sun?

    http://java.sun.com/docs/books/tutor...ing/index.html

    Maybe you're rushing it? You need to start small and go from there.
    Last edited by nuzzle; October 12th, 2009 at 10:25 PM.

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Using Swing and Placing Components

    Quote Originally Posted by capitolc View Post
    I would like to use GridBagLayout since it is so flexible (despite it being so complex), but I can't get it the exact way I want. I want my grid's origin to start at the top-left on my panel; how do I get it there? The borderlayout doesn't have a setting for that.
    If you can explain exactly how you want to layout your panel (a picture would help), maybe we can suggest some ways to do it.

    Layout managers take a while to become familiar with, but it pays off in the long run. Don't forget that you can put panels inside panels and use different layout managers for each.

    Plan to throw one away; you will anyhow...
    F. Brooks
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured