Java - displaying a minimal JFrame window

If the JFrame window is minimized, is there a way to bring it back into focus?

I am trying to get him to click on a specific point, and then restore it.

while (isRunning) { start = System.currentTimeMillis(); frame.setState(Frame.ICONIFIED); robot.mouseMove(clickX, clickY); robot.mousePress(InputEvent.BUTTON1_MASK); frame.setState(Frame.NORMAL); Thread.sleep(clickMs - (System.currentTimeMillis() - start)); } 
+6
source share
2 answers

If you want to return it from iconified , you can simply set its state to normal :

 JFrame frame = new JFrame(...); // Show the frame frame.setVisible(true); // Sleep for 5 seconds, then minimize Thread.sleep(5000); frame.setState(java.awt.Frame.ICONIFIED); // Sleep for 5 seconds, then restore Thread.sleep(5000); frame.setState(java.awt.Frame.NORMAL); 

An example from here .

There are also WindowEvent s that fire every time the state changes, and the WindowListener that handles these triggers. In this case you can use:

 public class YourClass implements WindowListener { ... public void windowDeiconified(WindowEvent e) { // Do something when the window is restored } } 

If you want to check for another change in the state of the program, there is no โ€œpure Javaโ€ solution, but just an ID window.

+12
source

You can set the state to normal:

 frame.setState(NORMAL); 

Full example:

 public class FrameTest extends JFrame { public FrameTest() { final JFrame miniFrame = new JFrame(); final JButton miniButton = new JButton( new AbstractAction("Minimize me") { public void actionPerformed(ActionEvent e) { miniFrame.setState(ICONIFIED); } }); miniFrame.add(miniButton); miniFrame.pack(); miniFrame.setVisible(true); add(new JButton(new AbstractAction("Open") { public void actionPerformed(ActionEvent e) { miniFrame.setState(NORMAL); miniFrame.toFront(); miniButton.requestFocusInWindow(); } })); pack(); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { new FrameTest(); } } 
+5
source

Source: https://habr.com/ru/post/903679/


All Articles