import javax.swing.JFrame; import javax.swing.JLabel; public class Annoying { public static void main(String[] args) { JFrame frame = new JFrame("Hello!!"); // Set's the window to be "always on top" frame.setAlwaysOnTop( true ); frame.setLocationByPlatform( true ); frame.add( new JLabel(" Isn't this annoying?") ); frame.pack(); frame.setVisible( true ); } }
w.setAlwaysOnTop(true); w.setBounds(w.getGraphicsConfiguration().getBounds()); w.setVisible(true);
Window w=new Window(null) { @Override public void paint(Graphics g) { final Font font = getFont().deriveFont(48f); g.setFont(font); g.setColor(Color.RED); final String message = "Hello"; FontMetrics metrics = g.getFontMetrics(); g.drawString(message, (getWidth()-metrics.stringWidth(message))/2, (getHeight()-metrics.getHeight())/2); } @Override public void update(Graphics g) { paint(g); } }; w.setAlwaysOnTop(true); w.setBounds(w.getGraphicsConfiguration().getBounds()); w.setBackground(new Color(0, true)); w.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); this.setLocationRelativeTo(owner); this.setAlwaysOnTop(true); this.addWindowFocusListener(new WindowFocusListener() {
DragPanel.class, DataFlavor.javaJVMLocalObjectMimeType, "JLabel"); window.add(label); window.setAlwaysOnTop(true); window.setBackground(new Color(0,true)); DragSource.getDefaultDragSource().addDragSourceMotionListener(
dialog.add(btn1); dialog.setVisible(false); dialog.setAlwaysOnTop(true); dialog.setModal(true); dialog.setLayout(new GridLayout(2, 0, 10, 10));
import java.io.File; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; public class GFileChooserDialog extends JDialog { private JFileChooser fileChooser; private File file; public GFileChooserDialog(JFrame relativeTo) { super(relativeTo, true); this.setAlwaysOnTop(true); this.fileChooser = new JFileChooser(); this.getContentPane().setSize(450, 300); int returnVal = fileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); } } public File getFile() { return file; } public void setFile(File file) { this.file = file; } }
private MainPanel() { super(new BorderLayout()); JCheckBox check = new JCheckBox("Always On Top", true); check.addActionListener(e -> { JCheckBox c = (JCheckBox) e.getSource(); Container w = c.getTopLevelAncestor(); if (w instanceof Window) { ((Window) w).setAlwaysOnTop(c.isSelected()); } }); JPanel p = new JPanel(); p.add(check); p.setBorder(BorderFactory.createTitledBorder("JFrame#setAlwaysOnTop(boolean)")); add(p, BorderLayout.NORTH); setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); setPreferredSize(new Dimension(320, 240)); }
import java.awt.Frame; import javax.swing.*; class TestFrameSize { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame f = new JFrame("Test Screen Size"); f.setAlwaysOnTop(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); System.out.println(f.getToolkit().getScreenSize()); f.setExtendedState(Frame.MAXIMIZED_BOTH); f.setVisible(true); System.out.println(f.getSize()); } }); } }
import javax.swing.*; public class AfterJFrameClose { public static void main(String[] args) { JFrame frame = new JFrame("My frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setAlwaysOnTop(true); frame.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent windowEvent) { System.out.println("Frame closing"); } }); } }
import javax.swing.*; import java.awt.*; public class test extends JWindow{ public test(){ super(); this.setSize(500, 400); this.setLocation(50, 50); setBackground(new Color(0,0,0,0)); // this is the critical line - that fourth 0 represents alpha (or opacity) setAlwaysOnTop( true ); // keeps it in the foreground so you don't click away from it - note that clicks on the transparent part DO pass through to the desktop, at least on Lion JLabel testLabel = new JLabel("Floating text hah"); this.add(testLabel); } public static void main(String[] args){ System.out.println("Sup"); test t = new test(); t.setVisible(true); } }
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setVisible(true); frame1.setAlwaysOnTop(true); JFrame frame2 = new JFrame("Second frame"); frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame2.setVisible(true); frame2.setAlwaysOnTop(true); showFrameOnScreen(frame1, 1); showFrameOnScreen(frame2, 2);
import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import net.miginfocom.swing.MigLayout; public class MainClass { public static void main(String[] args) { JFrame frame = new JFrame (); frame.setVisible(true); frame.setAlwaysOnTop(true); final JDesktopPane desktopPane = new JDesktopPane(); desktopPane.setLayout(new MigLayout("center panel", "[100px:100px:1366px,grow,shrink 50,center]", "[100px:100px:768px,grow,shrink 50,center]")); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JInternalFrame form = new JInternalFrame(); form.setResizable(true); form.setVisible(true); desktopPane.add(form); frame.add(desktopPane); } }
private void showJPopupMenu(MouseEvent e) { try { if (e.isPopupTrigger() && menu != null) { if (window == null) { if (isWindows) { window = new JDialog((Frame) null); ((JDialog) window).setUndecorated(true); } else { window = new JWindow((Frame) null); } window.setAlwaysOnTop(true); Dimension size = menu.getPreferredSize(); Point centerPoint = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); if (e.getY() > centerPoint.getY()) window.setLocation(e.getX(), e.getY() - size.height); else window.setLocation(e.getX(), e.getY()); window.setVisible(true); menu.show(((RootPaneContainer) window).getContentPane(), 0, 0); // popup works only for focused windows window.toFront(); } } } catch (Exception ignored) { } }
import java.awt.Dimension; import javax.swing.*; public class testDialog { public static void main(String []args) { JDialog d=new JDialog(); JInternalFrame i=new JInternalFrame("HI",false,false,false,false); i.setPreferredSize(new Dimension(100,100)); d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); d.setTitle("Wait dialog"); d.add(i); d.pack(); d.setPreferredSize(new Dimension(100,100)); d.setLocation(300,300); d.setAlwaysOnTop(true); d.setVisible(true); } }
frame.setAlwaysOnTop(true);
/** * Called by the KnowledgeFlow application once the enclosing JFrame is * visible */ @Override public void nowVisible() { m_parent.setSize(600, 180); ((JFrame) m_parent).setResizable(false); m_parent.setAlwaysOnTop(true); m_parent.validate(); int iwidth = m_plotPanel.getWidth(); int iheight = m_plotPanel.getHeight(); m_osi = m_plotPanel.createImage(iwidth, iheight); Graphics m = m_osi.getGraphics(); m.setColor(m_BackgroundColor); m.fillRect(0, 0, iwidth, iheight); m_previousY[0] = -1; setRefreshWidth(); }
/** * Called by the KnowledgeFlow application once the enclosing JFrame is * visible */ @Override public void nowVisible() { m_parent.setSize(600, 180); ((JFrame) m_parent).setResizable(false); m_parent.setAlwaysOnTop(true); m_parent.validate(); int iwidth = m_plotPanel.getWidth(); int iheight = m_plotPanel.getHeight(); m_osi = m_plotPanel.createImage(iwidth, iheight); Graphics m = m_osi.getGraphics(); m.setColor(m_BackgroundColor); m.fillRect(0, 0, iwidth, iheight); m_previousY[0] = -1; setRefreshWidth(); }
setAlwaysOnTop(true); setResizable(false); setMinimumSize(new Dimension(300, 300));
setUndecorated(true); setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); setAlwaysOnTop(true); setOpacity(0.8f); setSize(200, 200);