Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing Beginner Question  RSS feed

 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code, whichever of the two components I add second, is the only one that shows up, even if the JFrame is expanded to the max. I don't understand why two adds produces only 1 visible component.

i
[ May 25, 2008: Message edited by: Red Smith ]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use a LayoutManager that determines which component goes where. The default is a BorderLayout, for which the add method places the component at the center, thus placing them on top of one another.

Try a BoxLayout or GridLayout instead (using something like "one.setLayout (new GridLayout(2, 1))").
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf! I changed the layout and now I see both of them.

Does Java have a LayoutManager where you give it the size you want the component to be, and the x,y coordinate where you want the component to have it's upper left corner displayed at? There is a C++ toolkit called FLTK that does it that way and it is super easy to use.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While it's possible not to use a layout and place components directly, that's rarely done and I would advise against it. Using Box- and/or GridLayouts within a hierarchy of JPanels allows pretty nice control of the layout. Direct positioning also has its own problems across platforms; what looks good on one, may not not look so good on some other OS or JVM.

Component size can be controlled with the setMinimumSize, setPreferredSize and setMaximumSize methods.
[ May 25, 2008: Message edited by: Ulf Dittmer ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Component size can be controlled with the setMinimumSize, setPreferredSize and setMaximumSize methods.


Sometimes, depending on the layout manager used. Some layout managers ignore these properties. For example, setPreferredSize is ignored by BorderLayout for everthing except BorderLayout.CENTER.

I highly recommend using MigLayout rather than the built in Swing layout managers. It is super simple and very coder friendly.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens for quite a few layout managers is that it uses the preferred size for several components to use some size that is "acceptible" for all. For instance, GridLayout will use the maximum of all preferred widths and the maximum of all preferred heights as its cell size. It therefore does not ignore the preferred size completely, but it warps it a bit.

Originally posted by Gregg Bolinger:
For example, setPreferredSize is ignored by BorderLayout for everthing except BorderLayout.CENTER.

Don't you mean the other way around? It uses the preferred size for everything BUT the center component, and therefore ignores it ONLY for the center component.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!