Results 1 to 10 of 10
  1. #1
    SantaFlan is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default Moving Components Around

    I used to be a VisualBASIC programmer and it was pretty easy to move a command button around. All you had to do was specify a left and a top value in the code.

    Ex:
    cmdButton.Left = 50
    cmdButton.Top = 100

    Is there a way to do this with AWT buttons & other components as well?

    P.S. Sorry for defiling this forum with VisualBASIC codes... LOL.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    In Java using AWT, or Swing UI components, components such as Buttons are laid out within a container, such as a Panel. The containers use a LayoutManager to have the button width and height and positional offset inside the container set for us. Most of the built-in to the Java VM layout managers don't let you tune the button individual X,Y coordinates that is.

    Probably the most useful for fine tuning, and also the most compicated, is to use a GridBagLayout.

  3. #3
    SantaFlan is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Gawd dammit. Why does it have to be difficult?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by travishein View Post
    Probably the most useful for fine tuning, and also the most compicated, is to use a GridBagLayout.
    I've seen good results with nested layouts and with the MIgLayout (though this needs to be downloaded first).

  5. #5
    Tanshaydar is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default

    For JButtons, you have to set layout (if not CardLayout or similar function) to null and specify bounds for button like:
    Java Code:
    JButton button = new JButton("This is a button")
    button.setBounds(topX, topY, buttonWidth, buttonHeight);
    But if your container is resizeable, then button place stays same and container look degrades.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Tanshaydar View Post
    For JButtons, you have to set layout (if not CardLayout or similar function) to null and specify bounds for button like:
    I very strongly disagree with the statements above. Why do you recommend this over use of layout managers? With what experience do you base these recommendation on? What will happen if/when a GUI created with null layout is run in a different OS with different screen resolutions?


    But if your container is resizeable, then button place stays same and container look degrades.
    Agree, and not just in this situation, but in many other situations as well.
    Last edited by Fubarable; 12-12-2010 at 09:11 PM.

  7. #7
    SantaFlan is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Tanshaydar View Post
    For JButtons, you have to set layout (if not CardLayout or similar function) to null and specify bounds for button like:
    Java Code:
    JButton button = new JButton("This is a button")
    button.setBounds(topX, topY, buttonWidth, buttonHeight);
    But if your container is resizeable, then button place stays same and container look degrades.
    That is exactly what I was looking for! It works perfectly thanks!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    OP, again, while transhaydar means well, this recommendations will not work in the long run. I strongly urge you to read up on how to use layout managers if you want to learn to create flexible scalable Swing GUI's.

  9. #9
    SantaFlan is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    I don't think you understand what I'm trying to make here. It's never going to be a scaleable window. It's like a dialogue. It's only meant to be minimized and closed. Not scalable, not maximizable.

  10. #10
    Tanshaydar is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default

    Of course, more professional coding needs to think every situation for a general application. So I can see Fubarable's concern here and he is right.
    However for a short term non-resizeable container it doesn't have any problem.

Similar Threads

  1. moving a pic, another pic disappears
    By benjamin in forum New To Java
    Replies: 4
    Last Post: 06-16-2010, 06:18 PM
  2. Moving objects
    By rsjava24 in forum AWT / Swing
    Replies: 1
    Last Post: 02-26-2010, 10:47 AM
  3. moving square
    By blindfolded in forum New To Java
    Replies: 5
    Last Post: 01-22-2010, 05:58 PM
  4. moving a file
    By swati.jyoti in forum New To Java
    Replies: 8
    Last Post: 11-23-2009, 08:44 AM
  5. Moving Box
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-30-2009, 12:29 PM

Posting Permissions

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