• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help in java GUI

 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For button Back and button Delete, I have setBounds to (130, 120, 195,30); and (10, 190, 195,30); , but they still doesn't move to bottom.

What's wrong here ?

Untitled.png
[Thumbnail for Untitled.png]
Image GUI
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should use the Layout manager which corresponds to your component. What are you adding those buttons to? Is it a panel? Remember that panels default to flow layout, so your buttons will congregate near the top. Remember the layout manager can override any other location and size information.
Suggest:-
  • 1: Go through the Java™ Tutorials section about layouts: here it is.
  • 2: Don't set the content pane on your frame. Leave its default value. Call add() rather than getContentPane().add()
  • 3: Call the set preferred size method on both buttons. Make sure to give them reasonable values.
  • 4: Add a panel to the frame with border layout.SOUTH or PAGE_END. Remember a j frame defaults to border layout.
  • 5: Add your buttons to that panel, which defaults to flow layout and (I think) the components distribute around its midline. You may have to call pack() on the panel. Not sure about that last point.
  • You may have to do similar things with the check boxes to get them where you want them.
     
    Rancher
    Posts: 3324
    32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Crossposted: http://stackoverflow.com/questions/36957438/need-help-in-gui
     
    John Joe
    Ranch Hand
    Posts: 570
    3
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:You should use the Layout manager which corresponds to your component. What are you adding those buttons to? Is it a panel? Remember that panels default to flow layout, so your buttons will congregate near the top. Remember the layout manager can override any other location and size information.
    Suggest:-

  • 1: Go through the Java™ Tutorials section about layouts: here it is.
  • 2: Don't set the content pane on your frame. Leave its default value. Call add() rather than getContentPane().add()
  • 3: Call the set preferred size method on both buttons. Make sure to give them reasonable values.
  • 4: Add a panel to the frame with border layout.SOUTH or PAGE_END. Remember a j frame defaults to border layout.
  • 5: Add your buttons to that panel, which defaults to flow layout and (I think) the components distribute around its midline. You may have to call pack() on the panel. Not sure about that last point.
  • You may have to do similar things with the check boxes to get them where you want them.



    Thanks for your suggestions. Appreciated
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're welcome
    In future make sure to tell people on both fora if you post in two locations.
     
    John Joe
    Ranch Hand
    Posts: 570
    3
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:You're welcome
    In future make sure to tell people on both fora if you post in two locations.



    Noted,with thanks
     
    Bartender
    Posts: 732
    10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:
    •3: Call the set preferred size method on both buttons. Make sure to give them reasonable values.
    •5: You may have to call pack() on the panel. Not sure about that last point.



    Why set the preferred size of a button? You should let the buttons determine their own preferred size. If you try to do it yourself and later use a different font, you will have to do a lot of trial-and-error experimentation to figure out "reasonable values." If you want all of the buttons to be the same size, use the appropriate layout manager that will do it for you.

    You call pack() on a Window (JFrame), not a panel.
     
    Author
    Posts: 986
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Fred Kleinschmidt wrote:Why set the preferred size of a button? You should let the buttons determine their own preferred size.


    This is a valid point, but the larger point (in my opinion) is that it's ok to set the preferredSize of a component, but it's not ok to set the bounds (or the size, which is part of the bounds) of a component. It's the layout manager's job to set the bounds, not yours. [The exception is when you have set up the code to explicitly not use a layout manager, which I'm reluctant to even mention because only in very rare and unusual circumstances should this be done.]

    Campbell Ritchie wrote:2: Don't set the content pane on your frame. Leave its default value. Call add() rather than getContentPane().add()


    FYI, reasonable people can disagree on this.

    Personally, I think it's fine to set the content pane and/or to call getContentPane().add(). [What I usually do, though, is add() stuff directly to the panel before setting the panel as the content pane. It's just a personal preference.] The reason I mention it is not to start a debate (no need for that) but simply to clarify that what's going wrong with your code is not related to this.
     
    Rob Camick
    Rancher
    Posts: 3324
    32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Brian Cole wrote: it's ok to set the preferredSize of a component, but it's not ok to set the bounds (or the size, which is part of the bounds) of a component. It's the layout manager's job to set the bounds, not yours.



    Yes it is the layout managers job to set the bounds of a component. If you do use the setBounds() method it will be ignored because the layout manager will override the values based on the rules of the layout manager. So coding that statement is wasted code and should not be coded.

    You should not be using the setPreferredSize() method since it is the responsibility of each component to determine its own size so the layout manager can do its job.

    If you do use the setPreferredSize() then you are in effect setting the "size" of the component since many layout manager respect the preferred size of the component. So this will diminish the effectiveness of the layout manager.

     
    Brian Cole
    Author
    Posts: 986
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Camick wrote:You should not be using the setPreferredSize() method since it is the responsibility of each component to determine its own size so the layout manager can do its job.

    If you do use the setPreferredSize() then you are in effect setting the "size" of the component since many layout manager respect the preferred size of the component. So this will diminish the effectiveness of the layout manager.


    Again, I essentially agree with this. But there can be exceptions.

    For example, I think it's ok to do something like if you're using a layout manager that respects preferredSize (such as FlowLayout) and for aesthetic reasons you want the two buttons to be the same size as each other.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Brian Cole wrote:. . . is add() stuff directly to the panel before setting the panel as the content pane. It's just a personal preference. . . .

    It is simply quicker to add things directly to a JFrame than to change its content pane.That sort of code allows you to use several panels with different layouts.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic