1

I need solution similar to GridLayout but without resizing components in JPanel.

Everything works great with JFrame, but I need to put those components into JPanel instead JFrame.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • 2
    *"Everything works great with JFrame, but i need to put those components into JPanel instead JFrame"* Since a frame and panel might have the same layout manager, I do not see the relevance of that statement. – Andrew Thompson Apr 18 '12 at 13:27
  • If your question has been answered, or if it is no longer valid, please 'tick' to choose the most appropriate answer so everyone knows that the problem has been resolved. Thanks – wattostudios May 21 '12 at 14:51

4 Answers4

5

I've seen two approaches that may suit your requirement:

  • Nest each component in a JPanel having FlowLayout, which respects the component's preferred size, as shown here.

  • Use the HORIZONTAL_WRAP or VERTICAL_WRAP orientation of JList, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

The following link might help you to choose the most appropriate layout for your needs. Its the Java Tutorial called "A Visual Guide to Layout Manager", which shows nice pictures of each layout and what they look like...

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Otherwise, if you say GridLayout is similar to what you need, you could always write your own MyGridLayout class (that extends GridLayout) and overwrite the method that does the autoresizing.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
1

I stumbled upon this question myself and even though I think using z JList might be a good solution, there's an even simpler way of doing it with a customized FLowLayout, called WrapLayout, available here: https://tips4java.wordpress.com/2008/11/06/wrap-layout/

I ended up using it in my project and it works very well. The only issue I run into is when I set my window to the full screen mode the layout did not update correctly. I used a simple workaround, which was this:

        //int targetWidth = target.getSize().width;
        int targetWidth = target.getParent().getSize().width; // FIXME: this is a hack for getting the correct size when switching between full screen modes on Mac

With that small hack it works perfectly.

b005t3r
  • 716
  • 5
  • 8
0

Just Override preferredLayoutSize() in flowlayout and set Maximum size to it. set Alignment as LEADING and set it to your JPanel. You'll get what you want

private FlowLayout getFlowLayout(int maximumSize)
      {
        if (flowLayout == null)
        {
          flowLayout = new FlowLayout()
          {
            @Override 
            public Dimension preferredLayoutSize(Container target)
            {
              Dimension dimension = super.preferredLayoutSize(target);
              dimension.width = Math.min(maximumSize, dimension.width);
              return dimension;
            }
          };
          flowLayout.setAlignment(FlowLayout.LEADING);
        }
        return flowLayout;
      }
Ravinda Lakshan
  • 1,006
  • 14
  • 14
  • I downvoted it because it doesn't work - won't give you a multi row FlowLayout. – b005t3r Oct 03 '20 at 18:01
  • Also there's a typo in the code - you're putting width where height should be used, but even with this fixed, it hardly makes any sense to try to do the rest of it as it won't fix the issue. – b005t3r Oct 03 '20 at 18:08
  • @b005t3r "Also there's a typo in the code - you're putting width where height should be used" can you point out where I use width instead of height? – Ravinda Lakshan Oct 05 '20 at 13:34