0

I tried to do this in Java:

public class Fram extends JFrame{
     public void init(){

        addWindowStateListener(new java.awt.event.WindowStateListener() {
            public void windowStateChanged(java.awt.event.WindowEvent evt) {
                wsc(evt);
            }
        });

    }
    private void wsc(java.awt.event.WindowEvent evt) {                     

        System.out.println(evt.getNewState() == Frame.MAXIMIZED_BOTH);
        System.out.println(this.getWidth());
    }                    
}

And the output is late.

When I maximize it the value is 360, but actual value is 1260. How do I the get width after maximizing?

Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
user965347
  • 186
  • 2
  • 16

3 Answers3

1

It's a bit confusing. You actually have to implement ComponentListener. Then componentResized() is called. This will give you the correct width.

markbernard
  • 1,412
  • 9
  • 18
1

when i maximize it the value is 360 but actual value is 1260 hot to get width after maximize?

Try wrapping your code in a SwingUtiltities.invokeLater(). This will place the code at the end of the Event Dispatch Thread (EDT) so it should execute after the resizing code has been completed.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

ok i found the solution this post is not at all right.

How to detect JFrame window minimize and maximize events?

this code, do the trick

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JFrame;

public class Main extends JFrame implements ComponentListener {
  public Main() {
    addComponentListener(this);
  }

  public void componentHidden(ComponentEvent e) {
    System.out.println("componentHidden");
  }

  public void componentMoved(ComponentEvent e) {
    System.out.println("componentMoved");
  }

  public void componentResized(ComponentEvent e) {
    System.out.println("componentResized");

    if (getState() == Frame.ICONIFIED) {
      System.out.println("RESIZED TO ICONIFIED");
    } else if (getState() == Frame.NORMAL) {
      System.out.println("RESIZED TO NORMAL");
    } else {
      System.out.println("RESIZED TO MAXIMIZED");
    }
  }

  public void componentShown(ComponentEvent e) {
  }

  public static void main(String[] arg) {
    Main m = new Main();

    m.setVisible(true);
    m.setSize(new Dimension(300, 100));
    m.setLocation(50, 50);
  }
}
Community
  • 1
  • 1
user965347
  • 186
  • 2
  • 16