2

i'm making a java application for mac. the application must have the hability of "Auto Hide" equals to "Command+H" shortcut. i'm trying to do it using setVisible(False) in the JFrame. but it doesn't work. how can i do it?

this is may code:

void hide(){
   setNormalScreen(); //disable fullscreen mode
   //this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
   setVisible(false);
   this.setState(JFrame.ICONIFIED);
}

and this is what i'm getting: The main window

moscoquera
  • 278
  • 2
  • 16
  • Wrap your JAR in an Mac OS X application bundle, discussed [here](http://stackoverflow.com/a/14917990/230513) and [here](http://stackoverflow.com/a/8956715/230513). – trashgod Feb 13 '14 at 20:38

1 Answers1

0

See the below example. You can hide it via Java code using setVisible(false) as you suggested, then appReOpened() event will be called when the user clicks the app in the dock. When this happens, you can just call setVisible(true). This should mimic the Command-H behavior.

See the commented code below also for an uglier solution.

public class Test extends JFrame implements ActionListener, com.apple.eawt.AppReOpenedListener {

    public static void main(String[] args) {
        Test frame = new Test();
        JButton test = new JButton("test");
        test.addActionListener(frame);

        com.apple.eawt.Application app = com.apple.eawt.Application.getApplication();
        app.addAppEventListener(frame);

        frame.getContentPane().add(test);
        frame.pack();
        frame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        setVisible(false);

        //      try {
        //          Robot robot = new Robot();
        //          robot.keyPress(KeyEvent.VK_META);
        //          robot.keyPress(KeyEvent.VK_H);
        //          robot.keyRelease(KeyEvent.VK_H);
        //          robot.keyRelease(KeyEvent.VK_META);
        //      } catch (AWTException ex) {
        //          // TODO Auto-generated catch block
        //          ex.printStackTrace();
        //      }
    }

    @Override
    public void appReOpened(AppReOpenedEvent arg0) {
        setVisible(true);
    }
}
martinez314
  • 12,162
  • 5
  • 36
  • 63
  • Thank you. after a little more research, I realized that setVisible does not work if the application is in full screen, just so I have to wait until the application returns to normal and then I can do the trick robot – moscoquera Feb 13 '14 at 23:29
  • Is it possible when it hide to dock and a timer implements here during minimize will work perfectly, or timer does not get stuck. – Dheeraj Upadhyay Jul 26 '16 at 09:23