How To Keep JFrame Always On Top

Always On Top : It’s something that keeps the window on top, you might have seen in some programs they have a menu strip item called Always On Top when you enable it, it will keep the window on top most. You might have seen in Visual Basic or Visual C-Sharp they have a property for that in Form properties so you don’t need to write codes for that but in Java you all know that they neither have a official IDE nor a well advanced 3rd party IDE such as Visual Studio.


To keep your window always on top you may use this code :
setAlwaysOnTop(true); 

You should keep that code where you define your JFrame properties(I think it’s the same when you use JDialog etc) :

import javax.swing.*;
public class GUI_ extends JFrame
{
public GUI_()
{
setSize(400,500);
setDefaultCloseOperation(3);
setVisible(true);
 setAlwaysOnTop(true);
}

public static void main(String args[])
{
  new GUI_();
}
}

I hope you found this little thing helpful.

Post a Comment