Results 1 to 5 of 5
  1. #1
    sessomw5098 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default FlowLayout Problem

    Hi,
    I am using several JPanels, which use FlowLayout. If I add several components to, say panelA, and then add panelA to a frame, any components that cannot fit on one line automatically go to the next. However, if I add components to panelA and then add panelA to panelB (a blank panel with FlowLayout), and then panelB to a frame, the components do not wrap around. Instead, the components are centered and the left & right components are off the screen. Is there anyway to fix this?

    Thanks,
    Matthew

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    yes. do not use FlowLayout :)

    seriously though, FlowLayout was not intended to be used by anybody to actually make UIs to show people. Consider BorderLayout - it's simple and predictable.

  3. #3
    sessomw5098 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the reply. If I have 100 checkboxes and I want them to be aligned in the center, and if they can't fit all on one line then they automatically wrap to the next line. But with BorderLayout, I can only add one component to each region. If I use a JPanel and then add the checkboxes to the panel, and then the panel to the BorderLayout panel, I'm right back to the same place I started. Am I missing something?

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    100 checkboxes huh. dont know what to suggest to you then, perhaps FlowLayout is indeed the way to go... Give BoxLayout a try as well, i think it knows how to center things too

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    FlowLayout was not intended to be used by anybody to actually make UIs to show people
    Every layout was designed for a specific use.

    Consider BorderLayout - it's simple and predictable.
    Now you can only add 5 components to the frame.

    The idea of using layout managers is to mix and match different layout managers to get your desired results.

    A FlowLayout is used to align components in a row. So yes you might create a panel using a FlowLayout and then add a bunch of buttons to it. Then you might add this panel to the South of another panel using a BorderLayout.

    Instead, the components are centered and the left & right components are off the screen. Is there anyway to fix this?
    By default the components of a FlowLayout will wrap to the next line when they are added to a container, the problem is that the preferred size of the panel is calculated as if all the components are displayed on a single row so the panel is not allocation enough vertical space to see the components wrap.

    The Wrap Layout may be what you are looking for.

Similar Threads

  1. A few question about FlowLayout
    By glhansen in forum New To Java
    Replies: 3
    Last Post: 03-23-2009, 10:19 PM
  2. New Line in FlowLayout
    By chris12295 in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 05:49 PM
  3. Help Using FlowLayout Manager
    By toad in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 12:43 AM

Posting Permissions

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