Link to home
Start Free TrialLog in
Avatar of Javin007
Javin007Flag for United States of America

asked on

Java Resize Event Trouble

So I've applied a ComponentListener to my window's ContentPane to catch the "resize" event.  Then during this event, I'm resizing/moving objects on the page to adjust for the new size.

The problem is that it seems as if the new size of the ContentPane isn't quite "caught up" at the point the resize event fires.  What happens is if I make the window larger, everything seems to be fine.  But if I make it shorter, one JPanel object is then too "long" by a pixel or two, and causes another JPanel object to drop to the next row.

Let me know if any code is needed.

Edit:  So apparently it's worse than I thought.  If I move the borders very quickly, there's a "gap" on either side of the panel components.  It's as if the resize event is giving me the size that the ContentPane WAS, not what it now IS.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Please post your code
Avatar of Javin007

ASKER

Which part?
	@Override protected void event_WindowResized(ComponentEvent e) {
		if (!Initialized) return;
		dimRight.height = myPane.getHeight();
		dimLeft.width = myPane.getWidth() - dimRight.width;
		dimLeft.height = dimRight.height;
	}

Open in new window


This is what is currently in the resize "event".
	@Override protected void buildComponents() {
		JPanel panLeft = new JPanel();
		JPanel panRight = new JPanel();

		panLeft.setBackground(Color.RED);
		panRight.setBackground(Color.BLUE);

		myPane = getContentPane();

		panLeft.setLayout(new BoxLayout(panLeft, BoxLayout.PAGE_AXIS));
		panRight.setLayout(new BoxLayout(panRight, BoxLayout.PAGE_AXIS));

		dimRight = new Dimension();
		dimRight.width = 150;
		dimRight.height = myPane.getHeight();
		panRight.setPreferredSize(dimRight);
		panRight.setMinimumSize(dimRight);
		panRight.setMaximumSize(dimRight);

		dimLeft = new Dimension();
		dimLeft.width = myPane.getWidth() - dimRight.width;
		dimLeft.height = dimRight.height;
		panLeft.setPreferredSize(dimLeft);
		panLeft.setMinimumSize(dimLeft);
		panLeft.setMaximumSize(dimLeft);

		myPane.add(panLeft);
		myPane.add(panRight);
		
	}

Open in new window


This is where the two panels are being added to the form.
Is this your actual code that you are after? Or a simple example just to post on here?

The reason that I ask, is that if this IS the real code then the layout that you are after is quite simple. Therefore, instead of trying to implement the resizing behaviour manually in your code, you should be using the features of the various LayoutManagers in Swing to do the work for you. Now there are a number of ways to do what you want, but the BorderLayout may be the simplest.

Here is a self contained demo that shows using a BorderLayout to allow one of the JPanels to resize with the main window while keeping the other fixed. Note that there is no handling of the resize event at all, yet it still hopefully does what you are after...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class LayoutDemo extends JFrame {
    
    public LayoutDemo(String title) {
        super(title);
    }

    private static void createAndShowGUI() {
        new LayoutDemo("Layout Demo").buildComponents();
    }
    
    private void buildComponents() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        Container myPane = getContentPane();
        myPane.setPreferredSize(new Dimension(450, 300));
        myPane.setLayout(new BorderLayout());
        
        JPanel panLeft = new JPanel();
        JPanel panRight = new JPanel();

        panLeft.setBackground(Color.RED);
        panRight.setBackground(Color.BLUE);
        panRight.setPreferredSize(new Dimension(150, 300));

        panLeft.setLayout(new BoxLayout(panLeft, BoxLayout.PAGE_AXIS));
        panRight.setLayout(new BoxLayout(panRight, BoxLayout.PAGE_AXIS));


        myPane.add(panLeft, BorderLayout.CENTER);
        myPane.add(panRight, BorderLayout.LINE_END);
        
        
        pack();
        setVisible(true);
    }
    
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Open in new window

Important lines in the above code... Line 25 is where we set the contentPane to have the BorderLayout and then Lines 38,39 are where we add the two JPanels in two different areas of the BorderLayout. Check out some info on BorderLayout for more...

http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html
I don't follow... How does this solve the problem?
I know you are addressing mccarl with your last comment, but having read your question before his comment, I would have said, from experience, that :

I'm resizing/moving objects on the page to adjust for the new size.

is not a good route to take. Trying to pre-empt the JVM's layout managers is "asking for trouble".
Yeah, I assumed as much when I went down this road, but I couldn't seem to get the layout managers to play nice and do what I wanted.  

I've since set all layout managers to "null" and have gone about using setLocation and setSize to manually place/scale things.  Probably not the "best" way to do it (fortunately, this app has zero chance of ending up anywhere but on a Windows system) but it at least finally gets the results looking the way I wanted.
I've since set all layout managers to "null"
That's worse still ;) There's probably almost never a good reason to reinvent the layout manager wheel
but it at least finally gets the results looking the way I wanted.

Hope you don't propose closing this Q with that as the solution? ;)

GridbagLayout has the greatest resolution, but can be tricky. BorderLayout as has been said, would be preferable. The comment above this one is the watchword here though. :)
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial