How to display a swing window (GUI) or JFrame always on top?

Java has introduced a new method since it’s 1.5 version release. If you are working with GUI and you would like your Frame or Window to stay on the top of other applications, you can do so by doing just a method call.

This method is: setAlwaysOnTop on JFrame Class

Sample Code:

/**
 * @Author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modified On 2009-09-18
 *
 * Demonstrates how to write a GUI that
 * always stays on Top.
 */
package com.kushal.gui;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class WindowOnTop {

	public static void main(String[] args) {
		JFrame frame = new JFrame("Sanjaal Corps - Windows On Top Demo!");
		frame.setSize(400, 100);
		frame.setLocation(100, 150);

		/**
		 * Since JDK 1.5, there is a method called 'setAlwaysOnTop' which will
		 * put the frame on top, even if inactive.
		 */
		frame.setAlwaysOnTop(true);

		frame.add(new JLabel(
				" No Matter Where You Click, This Frame Always Stays On Top"));
		frame.setVisible(true);
	}
        
/*
	 * SANJAAL CORPS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF 
	 * THE SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
	 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
	 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SANJAAL CORPS SHALL NOT BE LIABLE FOR 
	 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR 
	 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. 
	 * 
	 * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE 
	 * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE 
	 * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT 
	 * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE 
	 * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE 
	 * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE 
	 * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SANJAAL CORPS 
	 * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR 
	 * HIGH RISK ACTIVITIES. 
	 */
}

The following is the information about this feature from the Java Documentation of this API:

setAlwaysOnTop - Changes the always-on-top window state. An always-on-top window is a window that stays above all other windows except maybe other always-on-top windows. If there are several always-on-top windows the order in which they stay relative to each other is not specified and is platform dependent.

If some other window already is always-on-top then the relative order between these windows is unspecified (depends on platform). No window can be brought to be over always-on-top window except maybe another always-on-top window.

All owned windows of an always-on-top window automatically become always-on-top windows. If a window ceases to be always-on-top its owned windows cease to be always-on-top.

When an always-on-top window is sent toBack its always-on-top state is set to false.

This method makes the window always-on-top if alwaysOnTop is true. If the window is visible, this includes bringing window toFront, then “sticking” it to the top-most position. If the window is not visible it does nothing other than setting the always-on-top property. If later the window is shown, it will be always-on-top. If the Window is already always-on-top, this call does nothing.

If alwaysOnTop is false this method changes the state from always-on-top to normal. The window remains top-most but its z-order can be changed in the normal way as for any other window. Does nothing if this Window is not always-on-top. Has no effect on relative z-order of windows if there are no other always-on-top windows.

Note: some platforms might not support always-on-top windows. There is no public API to detect if the platform supports always-on-top at runtime.

If a SecurityManager is installed, the calling thread must be granted the AWTPermission “setWindowAlwaysOnTop” in order to set the value of this property. If this permission is not granted, this method will throw a SecurityException, and the current value of the property will be left unchanged.

Parameters: alwaysOnTop – new value of always-on-top state of the window
Throws: SecurityException – if the calling thread does not have permission to set the value of always-on-top property
Since: 1.5

Java Tutorial – Swing Text Drag And Drop – Sample Example

In computer graphical user interfaces, drag-and-drop or DnD is the action of (or support for the action of) clicking on a virtual object and dragging it to a different location or onto another virtual object. In general, it can be used to invoke many kinds of actions, or create various types of associations between two abstract objects. As a feature, support for drag-and-drop is not found in all software, though it is sometimes a fast and easy-to-learn technique for users to perform tasks. However, the lack of affordances in drag-and-drop implementations means that it is not always obvious that an item can be dragged.

In this tutorial, I will demonstrate the ability to do a drag and drop between a text field and a text area or vice versa in Java Swing GUI development. I will also show how dragging of a text from a text area or text field can be disabled, preventing the text from that field from getting dragged and drop. This is a very powerful feature of Java, and thanks to those who thought of simplifying developer’s life by implementing the feature in API. All we have to do now is to sit back and use the feature which has already been developed.

package com.kushal.gui;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modified On 07-14-2009
 *
 * This class demonstrates the ability to do
 * drag and drop of selected text between a text field
 * and a text area.
 *
 */
public class DragAndDropDemo extends JFrame {
	private static final long serialVersionUID = 1L;

	public DragAndDropDemo()
	{
		super("www.sanjaal.com/java - Drag and Drop Demo");

		Box verticalBox = Box.createVerticalBox();
		/**
		 * Defining a text field.
		 * Setting setDragEnabled() to true.
		 * You can drag on this field, or drag text from
		 * this field to any other field.
		 */
		JTextField textField = new JTextField(15);
		textField.setText("Select me and drag me to other fields.");
		textField.setDragEnabled(true);
		verticalBox.add(textField);

		/**
		 * Defining a text area.
		 * Setting setDragEnabled() to true.
		 * You can drag on this field, or drag text from
		 * this field to any other field.
		 */

		JTextArea textArea = new JTextArea(4, 25);
		textArea.setText("www.sanjaal.com/java is great.");
		textArea.setDragEnabled(true);
		verticalBox.add(new JScrollPane(textArea));

		/**
		 * Adding a text field by setting setDragEnabled() to false.
		 * You can drag over this text field, but cannot drag
		 * the text on this field to any other fields.
		 */
		JTextField textField1 = new JTextField(15);
		textField1.setText("You cannot drag me.");
		textField1.setDragEnabled(false);
		verticalBox.add(textField1);

		JPanel panel=new JPanel();
		panel.add(verticalBox);
		getContentPane().add(panel);

		/**
		 * Setting some common GUI Features
		 */
		pack();
		setVisible(true);
		setLocation(200,200);
		setSize(400,200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	/**
	 * Testing the DragAndDrop GUI program.
	 */
	public static void main(String[] args) {
		new DragAndDropDemo();

	}
}

—————————-
Here is the output: