Solved

Java Resize Event Trouble

Posted on 2014-01-30
11
163 Views
Last Modified: 2014-10-02
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.
0
Comment
Question by:Javin007
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Please post your code
0
 
LVL 4

Author Comment

by:Javin007
Comment Utility
Which part?
0
 
LVL 4

Author Comment

by:Javin007
Comment Utility
	@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
 
LVL 4

Author Comment

by:Javin007
Comment Utility
	@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
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 4

Author Comment

by:Javin007
Comment Utility
I don't follow... How does this solve the problem?
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
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
 
LVL 4

Author Comment

by:Javin007
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
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
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now