Wrap FlowLayout

Discussion in 'Java' started by Jason Cavett, May 1, 2008.

  1. Jason Cavett

    Jason Cavett Guest

    I'm using a FlowLayout in a JFrame. However, when a user resizes the
    JFrame, the components (JCheckBoxes) do not wrap. Instead, they just
    disappear completely. Is it possible to have FlowLayout wrap? If so,
    what am I doing wrong?

    Here is the setup of the JPanel (which is inside my JFrame).

    FlowLayout flowLayout = new FlowLayout();
    flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
    southPropertiesPanel = new JPanel();
    southPropertiesPanel.setLayout(flowLayout);
    southPropertiesPanel.add(getFirstPropertyCheckBox(), null);
    southPropertiesPanel.add(getSecondPropertyCheckBox(), null);
    southPropertiesPanel.add(getThirdPropertyCheckBox(), null);
    southPropertiesPanel.add(getFourthPropertyCheckBox(), null);
    Jason Cavett, May 1, 2008
    #1
    1. Advertising

  2. Jason Cavett wrote:
    > I'm using a FlowLayout in a JFrame. However, when a user resizes the
    > JFrame, the components (JCheckBoxes) do not wrap. Instead, they just
    > disappear completely. Is it possible to have FlowLayout wrap? If so,
    > what am I doing wrong?
    >
    > Here is the setup of the JPanel (which is inside my JFrame).
    >
    > FlowLayout flowLayout = new FlowLayout();
    > flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
    > southPropertiesPanel = new JPanel();
    > southPropertiesPanel.setLayout(flowLayout);
    > southPropertiesPanel.add(getFirstPropertyCheckBox(), null);
    > southPropertiesPanel.add(getSecondPropertyCheckBox(), null);
    > southPropertiesPanel.add(getThirdPropertyCheckBox(), null);
    > southPropertiesPanel.add(getFourthPropertyCheckBox(), null);


    You must have the size of the JPanel constrained somehow so that you
    can't see the rest of the components. Just out of curiosity what is the
    null constraint for in the add?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class test {
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel(new FlowLayout());

    JButton b;
    for (int i=0; i<10; i++) {
    b = new JButton(Integer.toString(i));
    p.add(b);
    }
    f.add(p,BorderLayout.CENTER);
    f.pack();
    f.setVisible(true);
    }
    });
    }
    }

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDem
    Knute Johnson, May 1, 2008
    #2
    1. Advertising

  3. Jason Cavett

    Jason Cavett Guest

    On May 1, 12:20 pm, Knute Johnson <>
    wrote:
    > Jason Cavett wrote:
    > > I'm using a FlowLayout in a JFrame.  However, when a user resizes the
    > > JFrame, the components (JCheckBoxes) do not wrap.  Instead, they just
    > > disappear completely.  Is it possible to have FlowLayout wrap?  If so,
    > > what am I doing wrong?

    >
    > > Here is the setup of the JPanel (which is inside my JFrame).

    >
    > > FlowLayout flowLayout = new FlowLayout();
    > > flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
    > > southPropertiesPanel = new JPanel();
    > > southPropertiesPanel.setLayout(flowLayout);
    > > southPropertiesPanel.add(getFirstPropertyCheckBox(), null);
    > > southPropertiesPanel.add(getSecondPropertyCheckBox(), null);
    > > southPropertiesPanel.add(getThirdPropertyCheckBox(), null);
    > > southPropertiesPanel.add(getFourthPropertyCheckBox(), null);

    >
    > You must have the size of the JPanel constrained somehow so that you
    > can't see the rest of the components.  Just out of curiosity what is the
    > null constraint for in the add?
    >
    > import java.awt.*;
    > import java.awt.event.*;
    > import javax.swing.*;
    >
    > public class test {
    >      public static void main(String[] args) {
    >          EventQueue.invokeLater(new Runnable() {
    >              public void run() {
    >                  JFrame f = new JFrame();
    >                  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    >                  JPanel p = new JPanel(new FlowLayout());
    >
    >                  JButton b;
    >                  for (int i=0; i<10; i++) {
    >                      b = new JButton(Integer.toString(i));
    >                      p.add(b);
    >                  }
    >                  f.add(p,BorderLayout.CENTER);
    >                  f.pack();
    >                  f.setVisible(true);
    >              }
    >          });
    >      }
    >
    > }
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/linux/
    >
    > --
    > Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    >       ------->>>>>>http://www.NewsDem


    Actually, after a bit of testing, it may be because of the following
    reason:

    1. I have a JFrame with a BorderLayout.
    2. Inside the JFrame, I have three panels. northPanel, centerPanel
    and southPanel. The southPanel is the one that contains the
    JCheckBoxes (and the FlowLayout).
    3. When I resize the JFrame, it appears that the centerPanel receives
    the benefit of the resize, but the south and north panels appear to
    stay statically sized.

    That's at least what I am seeing. Not sure if there is any way to
    change/fix this.
    Jason Cavett, May 1, 2008
    #3
  4. Jason Cavett wrote:
    > On May 1, 12:20 pm, Knute Johnson <>
    > wrote:
    >> Jason Cavett wrote:
    >>> I'm using a FlowLayout in a JFrame. However, when a user resizes the
    >>> JFrame, the components (JCheckBoxes) do not wrap. Instead, they just
    >>> disappear completely. Is it possible to have FlowLayout wrap? If so,
    >>> what am I doing wrong?
    >>> Here is the setup of the JPanel (which is inside my JFrame).
    >>> FlowLayout flowLayout = new FlowLayout();
    >>> flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
    >>> southPropertiesPanel = new JPanel();
    >>> southPropertiesPanel.setLayout(flowLayout);
    >>> southPropertiesPanel.add(getFirstPropertyCheckBox(), null);
    >>> southPropertiesPanel.add(getSecondPropertyCheckBox(), null);
    >>> southPropertiesPanel.add(getThirdPropertyCheckBox(), null);
    >>> southPropertiesPanel.add(getFourthPropertyCheckBox(), null);

    >> You must have the size of the JPanel constrained somehow so that you
    >> can't see the rest of the components. Just out of curiosity what is the
    >> null constraint for in the add?
    >>
    >> import java.awt.*;
    >> import java.awt.event.*;
    >> import javax.swing.*;
    >>
    >> public class test {
    >> public static void main(String[] args) {
    >> EventQueue.invokeLater(new Runnable() {
    >> public void run() {
    >> JFrame f = new JFrame();
    >> f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    >> JPanel p = new JPanel(new FlowLayout());
    >>
    >> JButton b;
    >> for (int i=0; i<10; i++) {
    >> b = new JButton(Integer.toString(i));
    >> p.add(b);
    >> }
    >> f.add(p,BorderLayout.CENTER);
    >> f.pack();
    >> f.setVisible(true);
    >> }
    >> });
    >> }
    >>
    >> }
    >>
    >> --
    >>
    >> Knute Johnson
    >> email s/nospam/linux/
    >>
    >> --
    >> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    >> ------->>>>>>http://www.NewsDem

    >
    > Actually, after a bit of testing, it may be because of the following
    > reason:
    >
    > 1. I have a JFrame with a BorderLayout.
    > 2. Inside the JFrame, I have three panels. northPanel, centerPanel
    > and southPanel. The southPanel is the one that contains the
    > JCheckBoxes (and the FlowLayout).
    > 3. When I resize the JFrame, it appears that the centerPanel receives
    > the benefit of the resize, but the south and north panels appear to
    > stay statically sized.
    >
    > That's at least what I am seeing. Not sure if there is any way to
    > change/fix this.


    BorderLayout for the JFrame will be problematic, the edge areas in BL do
    not follow the same rules as the center. I would try GridBagLayout,
    it's always more complicated but you can usually get it to do almost
    anything.

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDem
    Knute Johnson, May 1, 2008
    #4
  5. Jason Cavett wrote:
    > On May 1, 12:20 pm, Knute Johnson <>
    > wrote:
    >> Jason Cavett wrote:
    >>> I'm using a FlowLayout in a JFrame. However, when a user resizes the
    >>> JFrame, the components (JCheckBoxes) do not wrap. Instead, they just
    >>> disappear completely. Is it possible to have FlowLayout wrap? If so,
    >>> what am I doing wrong?
    >>> Here is the setup of the JPanel (which is inside my JFrame).
    >>> FlowLayout flowLayout = new FlowLayout();
    >>> flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
    >>> southPropertiesPanel = new JPanel();
    >>> southPropertiesPanel.setLayout(flowLayout);
    >>> southPropertiesPanel.add(getFirstPropertyCheckBox(), null);
    >>> southPropertiesPanel.add(getSecondPropertyCheckBox(), null);
    >>> southPropertiesPanel.add(getThirdPropertyCheckBox(), null);
    >>> southPropertiesPanel.add(getFourthPropertyCheckBox(), null);

    >> You must have the size of the JPanel constrained somehow so that you
    >> can't see the rest of the components. Just out of curiosity what is the
    >> null constraint for in the add?
    >>
    >> import java.awt.*;
    >> import java.awt.event.*;
    >> import javax.swing.*;
    >>
    >> public class test {
    >> public static void main(String[] args) {
    >> EventQueue.invokeLater(new Runnable() {
    >> public void run() {
    >> JFrame f = new JFrame();
    >> f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    >> JPanel p = new JPanel(new FlowLayout());
    >>
    >> JButton b;
    >> for (int i=0; i<10; i++) {
    >> b = new JButton(Integer.toString(i));
    >> p.add(b);
    >> }
    >> f.add(p,BorderLayout.CENTER);
    >> f.pack();
    >> f.setVisible(true);
    >> }
    >> });
    >> }
    >>
    >> }
    >>
    >> --
    >>
    >> Knute Johnson
    >> email s/nospam/linux/
    >>
    >> --
    >> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    >> ------->>>>>>http://www.NewsDem

    >
    > Actually, after a bit of testing, it may be because of the following
    > reason:
    >
    > 1. I have a JFrame with a BorderLayout.
    > 2. Inside the JFrame, I have three panels. northPanel, centerPanel
    > and southPanel. The southPanel is the one that contains the
    > JCheckBoxes (and the FlowLayout).
    > 3. When I resize the JFrame, it appears that the centerPanel receives
    > the benefit of the resize, but the south and north panels appear to
    > stay statically sized.
    >
    > That's at least what I am seeing. Not sure if there is any way to
    > change/fix this.


    Jason:

    Here's a simple example with three components, the bottom one containing
    8 JButtons in a FlowLayout. Each of the three components have the same
    weights so they should take approximately the same space. The buttons
    will reorganize as the JFrame is resized. You can do a lot here with
    setting some preferred/minimum sizes.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class test {
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    c.weightx = c.weighty = 1.0;

    c.gridy = 0;
    JPanel north = new JPanel();
    north.setBackground(Color.RED);
    f.add(north,c);

    c.gridy = 1;
    JPanel center = new JPanel();
    center.setBackground(Color.BLUE);
    f.add(center,c);

    c.gridy = 2;
    JPanel p = new JPanel(new FlowLayout());
    JButton b;
    for (int i=0; i<10; i++) {
    b = new JButton(Integer.toString(i));
    p.add(b);
    }
    f.add(p,c);
    f.pack();
    f.setVisible(true);
    }
    });
    }
    }

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDem
    Knute Johnson, May 1, 2008
    #5
  6. Not quite sure, but if the flow layout doesn't have enough room then
    it can't wrap. Also try using the add method that only takes a
    component.
    Chase Preuninger, May 2, 2008
    #6
  7. I always design the component that sits in the center to be able to
    work at any size and I also assume that NORTH,SOUTH,EAST,WEST will
    always be the same size and only the needed amount in (NORTH/SOUTH
    height; EAST/WEST width)
    Chase Preuninger, May 2, 2008
    #7
  8. Jason Cavett

    Jason Cavett Guest

    On May 1, 9:25 pm, Chase Preuninger <> wrote:
    > Not quite sure, but if the flow layout doesn't have enough room then
    > it can't wrap.  Also try using the add method that only takes a
    > component.


    Yeah. The reason the "null" was in place was because, originally, a
    visual editor had been used to create the GUI (man...those things make
    horrible code, even if they are easy to use). I've since rectified
    that.
    Jason Cavett, May 2, 2008
    #8
  9. Jason Cavett

    Jason Cavett Guest

    On May 1, 12:52 pm, Knute Johnson <>
    wrote:
    > Jason Cavett wrote:
    > > On May 1, 12:20 pm, Knute Johnson <>
    > > wrote:
    > >> Jason Cavett wrote:
    > >>> I'm using a FlowLayout in a JFrame.  However, when a user resizes the
    > >>> JFrame, the components (JCheckBoxes) do not wrap.  Instead, they just
    > >>> disappear completely.  Is it possible to have FlowLayout wrap?  If so,
    > >>> what am I doing wrong?
    > >>> Here is the setup of the JPanel (which is inside my JFrame).
    > >>> FlowLayout flowLayout = new FlowLayout();
    > >>> flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
    > >>> southPropertiesPanel = new JPanel();
    > >>> southPropertiesPanel.setLayout(flowLayout);
    > >>> southPropertiesPanel.add(getFirstPropertyCheckBox(), null);
    > >>> southPropertiesPanel.add(getSecondPropertyCheckBox(), null);
    > >>> southPropertiesPanel.add(getThirdPropertyCheckBox(), null);
    > >>> southPropertiesPanel.add(getFourthPropertyCheckBox(), null);
    > >> You must have the size of the JPanel constrained somehow so that you
    > >> can't see the rest of the components.  Just out of curiosity what is the
    > >> null constraint for in the add?

    >
    > >> import java.awt.*;
    > >> import java.awt.event.*;
    > >> import javax.swing.*;

    >
    > >> public class test {
    > >>      public static void main(String[] args) {
    > >>          EventQueue.invokeLater(new Runnable() {
    > >>              public void run() {
    > >>                  JFrame f = new JFrame();
    > >>                  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > >>                  JPanel p = new JPanel(new FlowLayout());

    >
    > >>                  JButton b;
    > >>                  for (int i=0; i<10; i++) {
    > >>                      b = new JButton(Integer.toString(i));
    > >>                      p.add(b);
    > >>                  }
    > >>                  f.add(p,BorderLayout.CENTER);
    > >>                  f.pack();
    > >>                  f.setVisible(true);
    > >>              }
    > >>          });
    > >>      }

    >
    > >> }

    >
    > >> --

    >
    > >> Knute Johnson
    > >> email s/nospam/linux/

    >
    > >> --
    > >> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    > >>       ------->>>>>>http://www.NewsDem

    >
    > > Actually, after a bit of testing, it may be because of the following
    > > reason:

    >
    > > 1. I have a JFrame with a BorderLayout.
    > > 2. Inside the JFrame, I have three panels.  northPanel, centerPanel
    > > and southPanel.  The southPanel is the one that contains the
    > > JCheckBoxes (and the FlowLayout).
    > > 3. When I resize the JFrame, it appears that the centerPanel receives
    > > the benefit of the resize, but the south and north panels appear to
    > > stay statically sized.

    >
    > > That's at least what I am seeing.  Not sure if there is any way to
    > > change/fix this.

    >
    > BorderLayout for the JFrame will be problematic, the edge areas in BL do
    > not follow the same rules as the center.  I would try GridBagLayout,
    > it's always more complicated but you can usually get it to do almost
    > anything.
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/linux/
    >
    > --
    > Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    >       ------->>>>>>http://www.NewsDem- Hide quoted text -
    >
    > - Show quoted text -


    Alright. At least I don't feel like I'm going crazy anymore. Thanks
    for the response and thank for the code example below.
    Jason Cavett, May 2, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen

    flowlayout vs. gridlayout

    Stephen, Dec 5, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    5,913
    mikeb
    Dec 5, 2003
  2. dee
    Replies:
    2
    Views:
    2,906
  3. Aaron Fude

    To wrap or not to wrap?

    Aaron Fude, May 8, 2008, in forum: Java
    Replies:
    12
    Views:
    672
    Chronic Philharmonic
    May 10, 2008
  4. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    206
    Art Werschulz
    Sep 22, 2003
  5. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    226
    Anno Siegel
    Sep 25, 2003
Loading...

Share This Page