Lampapos
Lampapos

Reputation: 1081

Proportional resizing of JFrame

I'm trying to make a JFrame re-sizable in an unusual way: the ratio of width to height of the frame must be constant. I have written this simple code; in this case, the ratio is 1/2:

public class Panel extends JFrame {
  public Panel() {
    addComponentListener(new ComponentAdapter() {
      @Override
      public void componentResized(ComponentEvent e) {
      System.out.println("Reseized");
      setSize(getSize().width, getSize().width * 2);
    }
  });
}

But this frame has problems with repainting, and it works only when the user drags the mouse. When dragging is finished, the right bottom corner of the frame is situated in the point where user released the mouse.

Maybe componentResized method is not applicable in this case? So what should I use? Thanks in advance.

Upvotes: 6

Views: 5436

Answers (2)

user735703
user735703

Reputation:

Window resizing is handled by the OS; you only get updates post facto. So while you can correct the size after the resize, you cannot constrain the ratio while resizing.

Upvotes: 4

Mike
Mike

Reputation: 1899

You can try

((JFrame)e.getTopLevelAncestor()).pack();

where e is you component. Let me know if that work

Upvotes: 0

Related Questions