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.
LVL 4
Javin007Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
How does this solve the problem?
The code that I gave you is a standalone program that is ready to run. If you give it a try and resize the window you will see that the JPanel resizes too. Is this what you are looking for? If not, can you explain in more detail what it is that you are after and we can help further.
0
 
CEHJCommented:
Please post your code
0
 
Javin007Author Commented:
Which part?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Javin007Author Commented:
	@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".
0
 
Javin007Author Commented:
	@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.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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
0
 
Javin007Author Commented:
I don't follow... How does this solve the problem?
0
 
krakatoaCommented:
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".
0
 
Javin007Author Commented:
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.
0
 
CEHJCommented:
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
0
 
krakatoaCommented:
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. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.