• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

How can I repaint during resizing a Frame?

I'm completely new to Java ... never touched the stuff before, but I got an itch to play around in it. Now I'm trying to figure out how to repaint while the resizing of a Frame or JFrame is happening.

I'm running a Windows system with the "Show window contents while dragging" effect turned on. This means that when I drag a window around the screen, or when I resize a window, I get to see the window change as that happens. The problem I'm having is that my ComponentListener is not firing the componentResized event while I'm actually resizing the window. It only fires once I let go of the mouse button, thus completing the resize.

It appears that it is possible to handle repainting while resizing. I'm using the Eclipse editor and it seems to handle it just fine. So what am I missing?

0
bspotswood
Asked:
bspotswood
  • 3
  • 3
1 Solution
 
aozarovCommented:
In the resize process the JFrame does get events (PaintEvent). you can capture those events and apply further processing.
See the following example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestEventButton
{
      public static void main(String st[])
      {
            final JFrame frame = new JFrame("test");
            final JButton test = new JButton("test");

            frame.add(test);
            frame.pack();
            frame.show();

            Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener()
                        {
                              public void eventDispatched(AWTEvent event)
                              {
                                    System.out.println("event dispatched: " +event);
                                    
                                    if (event instanceof PaintEvent)
                                    {
                                          PaintEvent e = (PaintEvent) event;
                                          Rectangle pRec = e.getUpdateRect();
                                          System.out.println("Paint rec: " + pRec);
                                          Rectangle fRec = frame.getBounds();
                                          System.out.println("JFrame rec: " + fRec);
                                          frame.validate();
                                          frame.doLayout();
                                    }
                              }
                        }, 0xffffffffffffffffl);
      }
}
0
 
bspotswoodAuthor Commented:
Well, that example certainly gets me a lot closer, but its not quite right.

Using that example, the Frame repaints as I expand it larger and larger. Its not doing the same though when I shrink it. If I grab the edge of it and drag it smaller and smaller, it doesn't repaint until I move one of the borders larger or until I release the mouse button.

I'm increasing the points for the question +75 points in hopes someone can complete the shrinking part of the puzzle.
0
 
aozarovCommented:
The program above gives you the answer.
If you look at the output generated from "System.out.println("event dispatched: " +event);" you can see that
you get MouseEvents while you are shrinking. So if you capture those and do the same (frame.validate(); frame.doLayout())
you will see the updates while you shrink.
e.g.:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestEventButton
{
      public static void main(String st[]) throws Exception
      {
            final JFrame frame = new JFrame("test");
            final JButton test = new JButton("test");

            frame.add(test);
            frame.pack();
            frame.show();

            Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener()
                        {
                              public void eventDispatched(AWTEvent event)
                              {
                                    System.out.println("event dispatched: " +event);
                                    
                                    if (event instanceof PaintEvent || event instanceof MouseEvent)
                                    {
                                          if (event instanceof PaintEvent)
                                          {
                                          PaintEvent e = (PaintEvent) event;
                                          Rectangle pRec = e.getUpdateRect();
                                          System.out.println("Paint rec: " + pRec);
                                          Rectangle fRec = frame.getBounds();
                                          System.out.println("JFrame rec: " + fRec);
                                          }
                                          frame.validate();
                                          frame.doLayout();
                                    }
                              }
                        }, 0xffffffffffffffffl);

            if (false)
            {
            Runtime.getRuntime().exec("rundll32 url.dll FileProtocolHandler http://www.whatever.com/");
            Runtime.getRuntime().exec("cmd /c start /b notepad");
            System.out.println("after");
            }
      }
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
bspotswoodAuthor Commented:
Actually, I don't get mouse events unless the mouse happens to move over the form as I'm shrinking it, which is very unreliable. If I shrink it real fast then sometimes I will get a MOUSE_ENTERED and MOUSE_EXIT events, but not always.

Luckily, I just stumbled across my own answer, and its a lot more simple.


frame.getToolkit().setDynamicLayout(true);

That right there does the trick I was looking for all along. See: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Toolkit.html#setDynamicLayout(boolean)

To quote it:

"Parameters:
dynamic - If true, Containers should re-layout their components as the Container is being resized. If false, the layout will be validated after resizing is finished. "

Yay. I'll still give you credit though for dedication and excellent examples. =)
0
 
bspotswoodAuthor Commented:
Actually aozarov, can you post the line I mentioned above ...

frame.getToolkit().setDynamicLayout(true);

...and I'll accept that posting. I don't want to accept an answer that doesn't exactly correspond to my question. 8)
0
 
aozarovCommented:
Yes, Toolkit.getDefaultToolkit().setDynamicLayout(true); definetly does the job ;-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now