FiroKun
FiroKun

Reputation: 345

.requestFocusInWindow() doesn't work when build the jar

I have a GUI Java application. The app minimizes to the system tray. I put a frame.requestFocusInWindow() for when the tray icon is clicked, so that the restored JFrame can gain the user attention.

The code as follow.

 trayicon.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            try {
                if (e.getButton()==MouseEvent.BUTTON1) frame.setVisible(true);
                frame.requestFocusInWindow();
            } catch (Exception ex) {
                Logger.getLogger(TrayControl.class.getName()).log(Level.SEVERE, null, ex);
            }
        } 
});

This works perfectly if I execute the app in NetBeans IDE, but when I build the .jar file and execute it, don't seems to work the .requestFocusInWindow() because the app window don't gain the user attention and is restored behind other windows that I had open. So what's happening here?

Upvotes: 2

Views: 217

Answers (1)

FiroKun
FiroKun

Reputation: 345

Problem solved. I used .setExtendedState() method of the JFrame in the MouseListener as follows:

trayicon.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        try {
            frame.setExtendedState(JFrame.NORMAL);
            frame.setVisible(true);
        } catch (Exception ex) {
            Logger.getLogger(TrayControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 
});

Upvotes: 0

Related Questions