Solved

Java Resize Event Trouble

Posted on 2014-01-30
11
174 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
ID: 39821763
Please post your code
0
 
LVL 4

Author Comment

by:Javin007
ID: 39821767
Which part?
0
 
LVL 4

Author Comment

by:Javin007
ID: 39821771
	@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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 4

Author Comment

by:Javin007
ID: 39821775
	@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
ID: 39822812
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
 
LVL 4

Author Comment

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

Expert Comment

by:krakatoa
ID: 39825011
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
ID: 39825131
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
ID: 39825154
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
ID: 39825197
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
ID: 39825774
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

856 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