1

I am quite new to Java, but familiar with native Android dev so bear with me xD. I created an application that creates a JFrame. Then I set the closeOperation to: setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);.

It performs as expected, the frame is hidden and this is what I want (when I close). I need the application to keep on running (only once instance), because I am running a thread in the background that is performing an operation.

My actionListener on my button in my JFrame currently does this: setVisible(false);

My question is this, how can I maximize the JFrame again after it has been hidden? Would it be possible to display the frame when the user clicks on the minimized application in the task bar? Is there some type of listener that I need to implement?

Thanks in advance, any advice will be appreciated

UPDATE

For this solution to work correctly you need to do the following. Also have a look at XtremeBaumer's answer for this to make sense.

On JFrame creation setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);. When you want to minimize the app (on click possibly) frame.setState(Frame.ICONIFIED);. When you want to maximize the app again frame.setState(Frame.NORMAL); in windowDeiconified event.

One last thing, if you want to also minimize your app when the user clicks on the exit button (red x) add this to the windowClosing event frame.setState(Frame.ICONIFIED);.

Lunchbox
  • 1,538
  • 2
  • 16
  • 42

1 Answers1

2
    this.addWindowListener(new WindowListener(){

        @Override
        public void windowActivated(WindowEvent e) {
        }

        @Override
        public void windowClosed(WindowEvent e) {
        }

        @Override
        public void windowClosing(WindowEvent e) {
            setState(Frame.ICONIFIED)
        }

        @Override
        public void windowDeactivated(WindowEvent e) {
        }

        @Override
        public void windowDeiconified(WindowEvent e) {
            this.setVisible(true);
            //this should be what you want
        }

        @Override
        public void windowIconified(WindowEvent e) {
        }

        @Override
        public void windowOpened(WindowEvent e) {
        }

    });

i hope this solves your question. add it to your JFrame

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • Thank you, maybe also add this to your answer (worked when I implemented this answer) - http://stackoverflow.com/questions/3965336/how-to-minimize-a-jframe-window-from-java . Otherwise the `windowDeiconified` event is never called – Lunchbox Dec 16 '16 at 11:08
  • so i found the problem, that once you setVisible(false) or hide() the window, the user cannot show it again by clicking the icon, because there is no icon. how did you solve that? – XtremeBaumer Dec 16 '16 at 11:39
  • When the JFrame is created, do this instead (I will edit to my question today). `setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);`. Then you need to handle the closing of the JFrame on your own by using `frame.setState(Frame.ICONIFIED);` and `frame.setState(Frame.NORMAL);`. Iconified minimizes and normal reverts. User should not be able to close my app, yep dodgy, but with good reason. Clients orders. – Lunchbox Dec 19 '16 at 07:51