I have the following problem: I am working on a Java Swing application that show me a JFrame.

What I have to do is that when the user click on the X button the window have to be iconified and not close (it is a requirement requested by the client).

So I have a MainFrame class that extends a clssic JFram of Swing.

The application run on Linux Ubuntu and another requirement of the client is the following one: when the user do right click on the application icon in the Unity menu bar, and then click on the quit voice the application must shut down

My problem is that I can iconize the application when the user click on the X button changing this property:

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

in this way:

mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

Then I add a WindoListener that say that my mainFrame object (my window) have to be iconified when the user click on the X button.

    mainFrame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            logger.info("Minimize the MainFrameWindows instead close it");
            ((MainFrame)e.getSource()).setState(MainFrame.ICONIFIED);
            logger.info("EVENTO ICONIZZAZIONE: " + e.getSource().toString());
        }
    });

Ok, this work pretty well...the problem is that the same method will be called also if I click on the quit button on the application icon in the Ubuntu Unity main menu. So I obtain the same behavior.

My problem is that I would 2 different behavior:

1) Minimize the application in the menu if the user click on the X button in the application

2) Shut down the application if the user click on the quit button of the application contextual menu in the Ubuntu Unity main menu

Do you have some ideas about how do it?

Tnx

Andrea

1

There are 1 best solutions below

0
On

Maybe something like:

 @Override 
  public void windowClosing(WindowEvent e) {
       JFrame f = (JFrame) e.getWindow();
       if (f.getState() != JFrame.ICONIFIED)
           f.setState(JFrame.ICONIFIED);
       else 
           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }