tags:

views:

187

answers:

2

hey guys
I had made one application in java-swing,
Now what i am getting problem is,
i want to minimize my jframe when it is deactivate and then to maximize i want to activate that window.

So for maximize, i want to activate any jframe using java code.
So how to activate and deactivate any jframe, so that i can do something on window listeners?

thanks in advance.

+1  A: 

The following works:

import java.awt.Frame;

import javax.swing.*;

public class FrameTest {
    public static void main(String[] args) throws InterruptedException {

        // Create a test frame
        JFrame frame = new JFrame("Hello");
        frame.add(new JLabel("Minimize demo"));
        frame.pack();

        // Show the frame
        frame.setVisible(true);

        // Sleep for 5 seconds, then minimize
        Thread.sleep(5000);
        frame.setState(Frame.ICONIFIED);

        // Sleep for 5 seconds, then restore
        Thread.sleep(5000);
        frame.setState(Frame.NORMAL);

        // Sleep for 5 seconds, then kill window
        Thread.sleep(5000);
        frame.setVisible(false);
        frame.dispose();

        // Terminate test
        System.exit(0);
    }
}

Modified from http://www.javacoffeebreak.com/faq/faq0055.html


To focus the window you can do frame.requestFocus();.

aioobe
@aioobe,i know how can i minimize and maximize the jframe but i want to know,how set any frame to activate mode and deactivate mode.?
Nitz
What is activate / deactivate mode?!
aioobe
sorry guys,i was writing that code on some other function.so thank u 4 replay.
Nitz
+2  A: 

You'll need to add a WindowListener to your JFrame, and add the following logic to your listener:

public class Demo extends JFrame implements WindowListener {

    public Demo() {
        addWindowListener(this);
    }

    public void windowActivated(WindowEvent e) {
        setExtendedState(getExtendedState() | Frame.ICONIFIED);
    }

    public void windowDeactivated(WindowEvent e) {
        setExtendedState(getExtendedState() | Frame.MAXIMIZED_BOTH);
    }

    ....
}
msakr