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);
}
}