Layout managers and black voodoo magic

£$%^&* swing containers and their !"£$%^ layout managers >:(

I have (nested amongst many others) a JScrollPane, within which I set a JTree. Their parent JPanel happily fills the area i’ve allocated to it, yet the JScrollPane doesn’t want to play nice…

Regardless of almost any sizing I set for it, it dislikes resizing itself. By default it appears way too thin and slightly too tall. So I set the prefered size, which works but now it obviously doesn’t want to expand any larger when the window is maximised.

Fair enough, so i set the minimum size instead of the prefered… and it ignores it! *&^% thing.

Off hand I can’t think of a good way to fix this, surely it should pay attention to the minimum size? Other than listening to resize event and calculating this stuff manually (groan) I’m out of ideas.

Could you post some Code ? Without it, I can’t help you…

Very important Infos would be:

  • Which LayoutManager do you use?
  • What Components are in that JPanel?
  • How should it look like?

Troggan

I made some slight progress, but some weird side effects :o

The layout manager for the JPanel the JScrollPane was in was a FlowLayout. This is supposed to take into account min/max/prefered size, but wasn’t. Lots of trial and error later, and I switched it to a border layout on a whim. By adding the scroll pane at the center it now correctly expands to fill.

The scroll pane’s JPanel lives in a GridLayout of 1 collumn, 2 rows. The top cell’s JPanel is switched around as the user switches modes, but the bottom scroll pane remains there all the time. With the ‘fixed’ border layout in the bottom one, switching modes causes the whole app to grow by a sizable amount downwards (the new border layout expands downward, growing the app with it).

Once its grown (which it doesn’t need to, theres plenty of empty space) it will happly toggle as expected ???

gridbag pwnz j00 :wink:

If there was ever ONE thing I truly HATED about both the programmer certification test and Java itself, it’s this:

LAYOUT MANAGERS SUCK!

Okay, now that I’ve said that, I will amend it to say, they mostly suck. I really can only tolerate BoxLayout and the TableLayout class that isn’t part of Java (but should be)… because for the most part I think that the layout managers were designed by people who have never used a graphics console before. GridBagLayout is just WAAAAAAAAAY to annoying to use in normal development.

I have no wished to be ownzerd by GridBag. :smiley:

I loved gridbags but they do have a few shortcomings.
The new SpringLayout in 1.4 should be useful to those that never got on with gridbags.

Cas :slight_smile:

I wrote a custom Panel which extends JPanel contains another JPanel. The Parent Panel has a BorderLayout w/ the child panel added to center. This ensures the contents will allways fill the area available. Then the Childs layou is box, where the orientation is passed to the top level Panels contructor. Convienence methods are added to the mix to do things like add a nice titled border and separators.

Now you have a component in which you can place other components in an up-down or left right fashion.

For more complex layouts I nest these custom components. Man this Is getting more and more confusing.

package com.krypto.gui;

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;


public class KPanel extends JPanel
{

    private final JPanel group = new JPanel();

    private int    orientation = BoxLayout.Y_AXIS;


    public KPanel()
    {
        setup();
    } // End Constructor


    public KPanel(final int axis)
    {
        orientation = axis;
        setup();
    } // End Constructor

     
  
    private final void setup()
    {
        setOrientation(orientation);
        setLayout( new BorderLayout() );
        add( group, BorderLayout.CENTER );
    } // End setup method
    



    public final void setOrientation(final int axis)
    {
        orientation = axis;
        group.setLayout(new BoxLayout(group, orientation));
    } // End setOrientation method



   
    public final void add( final JComponent comp )
    {
        if( orientation == BoxLayout.X_AXIS )
        {
            comp.setAlignmentY( TOP_ALIGNMENT );
        }
        else
        {
            comp.setAlignmentX( LEFT_ALIGNMENT );
        }

        group.add( comp );

    } // End add method
    
    
    
    public final void addSeparator()
    {
      addSeparator( 10 );

    } // End addSeparator method


 
    public final void addSeparator( final int length )
    {
      final Dimension newDim = (orientation == BoxLayout.X_AXIS ?
                          new Dimension(length, 0) :
                          new Dimension(0, length));

      group.add(Box.createRigidArea(newDim));

    } // End addSeparator method
    

    
    public final void setBackgroundColor( final Color color )
    {
        group.setBackground( color );

    } // End setBackgroundColor method



   
    public final void setBorder( final String title )
    {
        setBorder( createTitledBorder(title) );      

    } // End setBorder method


   
    public final void setBorder( final int inset )
    {
        setBorder(BorderFactory.createEmptyBorder( inset,inset,inset,inset ) );

    } // End setBorder method


    public final void setBorder( final String title, final int inset )
    {
        setBorder( BorderFactory.createCompoundBorder
              ( createTitledBorder(title),
                BorderFactory.createEmptyBorder( inset,inset,inset,inset ) ) );
    } // End setBorder method



   
    public final void setSize( final int width, final int height )
    {
      final Dimension newDimension = new Dimension( width, height );
        setPreferredSize( newDimension );
        setMaximumSize( newDimension );
        setMinimumSize( newDimension );

    } // End setSize method


    protected javax.swing.border.Border createTitledBorder(final String title)
    {
      return (BorderFactory.createTitledBorder
            (BorderFactory.createEtchedBorder(), title));
      
    } // End createTitledBorder method




 
} 

Aww man! Finally a topic I can speak about and I come to the dance a month late.

Nested Layout managers will save you many headaches. Some days I just don’t care why something doesn’t fill it’s alotted space, so I throw it on a panel with a BorderLayout manager, place it in the center, add the panel where the original component was, and BINGO. My guis look great, but they sit on top of a stack of panels. I’ve never had a performance issue from this type of stacking.

SpringLayout is the truth! :wink: When it seems that only GridBagLayout will do I implore you to try SpringLayout. I found it much more intuitive and far less irritating.

[quote]If there was ever ONE thing I truly HATED about both the programmer certification test and Java itself, it’s this:

LAYOUT MANAGERS SUCK!

Okay, now that I’ve said that, I will amend it to say, they mostly suck. I really can only tolerate BoxLayout and the TableLayout class that isn’t part of Java (but should be)… because for the most part I think that the layout managers were designed by people who have never used a graphics console before. GridBagLayout is just WAAAAAAAAAY to annoying to use in normal development.

I have no wished to be ownzerd by GridBag. :smiley:
[/quote]
i agree , whenever i use awt the first lin in a constructor is setLayout(null);

i want control of my program, layoutmanager scare me

dont u like layouts? dont use them, so easy

Come on…just try it. If you used them for a while, you will see that it’s not that hard. And if you use layoutmanagers, your applikation behaves nice on resizes. I am using a lot of layout stuff (look at blacksheep.sf.net), and don’t have problems with it.

troggan

Its true layout managers suck for anything more complex. The GridBagLayout has known bugs and will screw if you change the layout later.

Except for the Spring layout (or whatever the name is). This is a forgotten layout manager but its by far the most flexible and powerful of them all. Also needs a lot of work to set up and is almost cryptic.

There is no need to resize my apps, all my apps are fixed size ;D

[quote]There is no need to resize my apps, all my apps are fixed size ;D
[/quote]
Ouch. I bet they suck on 1600x1200 and/or 640x480 then :stuck_out_tongue:

nah they do not suck, everybody likes em, enough ppl using them ;D

ever seen a game which is resizable ???

ok at least u supply some resolutions

like 640 x 480 and 800 x 600 ( look at UO the first and only MMORPG which makes money )