-2

I know there are already lot of thread available for this topic. I have already visited almost all of em, this one, this one, this one, also this one and this one.But didn't solve my issue.
My problem is different over here when I try to restore the JFrame it blinks, and didn't come on top of everything. I have already run this code in Ubuntu and it worked like a charm on ubuntu. frame.setAlwaysOnTop(true); works absolutely fine on ubuntu.
To solve this issue in windows I tried to use WindowsListener, But in windows 7 it blinks and didn't come on top of every windows. What I think is that it's trying to come on top of everything but may be other application has higher priority than this it goes away. How can I resolve this issue ?
EDIT :
I have two thread over here one thread is authenticating and if it's authenticated it minimized. If not authenticated it should always be on top for authenticating. Even if user switches window by pressing Alt key tab it should again come on top after 2 seconds.
Code for authentication :

public class ScreenLockAndUnlock implements Runnable{
    public static JFrame frame;
    public static boolean working = false;
    private JTextField punch;

    public void stop(){
        working = false;
    }

    public void run(){
        try{
            frame = new JFrame("Protected");
            frame.setContentPane(new JLabel(new ImageIcon("C:\\Users\\four.four-PC\\eclipse-workspace\\optimization\\src\\main\\java\\com\\greycode\\optimization\\finger_PNG6297.png")));
            frame.setVisible(true);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            gs.setFullScreenWindow(frame);
            frame.validate();
            frame.setLayout(new BorderLayout());
            punch = new JTextField();
            frame.add(punch,BorderLayout.SOUTH);
            punch.requestFocus();
            punch.addActionListener(action);
            }finally{
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    private void onTop() throws AWTException{
        // TODO Auto-generated method stub
        AlwaysOnTop top = new AlwaysOnTop();
        new Thread(top).start();
        while(true){
            try{
                frame.setState(Frame.NORMAL);
                if(punch.getText().trim()!= null && punch.getText().trim().toLowerCase().equals("true")){
                    working = true;
                    top.cancel();
                    frame.setState(JFrame.ICONIFIED);
                    Thread.sleep(10000);
                    top.star();
                    top = new AlwaysOnTop();
                    new Thread(top).start();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    @SuppressWarnings("serial")
    Action action = new AbstractAction(){
        public void actionPerformed(ActionEvent e){
            try{
                onTop();
            } catch (AWTException e1){
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    };
}

This code always look for whether JFrame is on top or not if not authenticated

public class AlwaysOnTop implements Runnable{
    boolean cancelled = false;
    public void run(){
        while(!cancelled){
            try{
                lookForMinimised();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public void cancel(){
        this.cancelled  = true;
    }
    public void star(){
        this.cancelled = false;
    }
    public void lookForMinimised() throws InterruptedException{
        // TODO Auto-generated method stub
        ScreenLockAndUnlock.frame.addWindowStateListener(new WindowStateListener(){
            public void windowStateChanged(WindowEvent e){
                // TODO Auto-generated method stub
                int newState = e.getNewState();
                if((newState & Frame.ICONIFIED) == Frame.ICONIFIED){
                    System.out.println("Frame is minimised");
                    ScreenLockAndUnlock.frame.setAlwaysOnTop(false);
                    ScreenLockAndUnlock.frame.setAlwaysOnTop(true);
                    ScreenLockAndUnlock.frame.setVisible(true);
                    ScreenLockAndUnlock.frame.toFront();
                    ScreenLockAndUnlock.frame.requestFocus();
                    ScreenLockAndUnlock.frame.validate();
                    ScreenLockAndUnlock.frame.setState(Frame.NORMAL);
                }
                else if ((newState & Frame.NORMAL) == Frame.NORMAL){
                      System.out.println("Waiting for authentication ...");
                }
            }
        });
        Thread.sleep(2000);
    }
}

Main method:

public class Authenticate{
    public static void main(String[] args){
        Thread displayScreen = new Thread(new ScreenLockAndUnlock());
        displayScreen.start();
    }
}
Lokesh Pandey
  • 1,739
  • 23
  • 50

1 Answers1

1

Please find a code which depicts the logical functionality that you want.
Also note that this code just depicts the functionality only which are frame restore-minimize, thread and their inter-working.
At the end, it will be you, who have to use the same at appropriate locations as per your need.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JTextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

public class TestClass2 extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    static boolean isAuthenticationStarted = false;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestClass2 frame = new TestClass2();
                    frame.setVisible(true);

                    frame.addWindowStateListener(new WindowStateListener() {
                           public void windowStateChanged(WindowEvent e) {
                               // minimized
                               if ((e.getNewState() & Frame.ICONIFIED) == Frame.ICONIFIED){
                                   if (!isAuthenticationStarted)
                                   {
                                       // Authentication not started yet and window minimized
                                       frame.setState(Frame.NORMAL);
                                   }
                               }
//                             // maximized
//                             else if ((e.getNewState() & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH){
//
//                             }
                           }
                        });

                    frame.setAlwaysOnTop(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestClass2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        textField = new JTextField();
        textField.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode()==KeyEvent.VK_ENTER)
                {
                    new Thread()
                    {
                        public void run()
                        {
                            // Start authentication here
                            isAuthenticationStarted = true;

                            // if authentication is success show next jframe
                            // else restore window
                            // reset the flag only when authentication is successful 
                            // isAuthenticationStarted = false;

                            // Minimizing frame
                            setState(Frame.ICONIFIED);

                            try {
                                Thread.sleep(3000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }



                            // restoring frame
                            setState(Frame.NORMAL);

                        }
                    }.start();
                }
            }
        });// End listener


        contentPane.add(textField, BorderLayout.CENTER);
        textField.setColumns(10);
    }
}

Hope this will help you. :-)

Aman
  • 735
  • 1
  • 6
  • 19
  • Your code works!! but can you tell me where i was doing wrong looking at my code ? – Lokesh Pandey Sep 19 '17 at 09:41
  • 1
    Thanks for the same. And for the problem, what I can think is the cross-calling of events between threads, which is leading to restore-minimize quickly, causing blinking effect. – Aman Sep 19 '17 at 09:51