Solved

Java Resize Event Trouble

Posted on 2014-01-30
11
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 36

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 17

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 17

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 36

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

696 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