Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Undecorated JFrame on Multi Screen

843805Mar 7 2006 — edited Jul 17 2007
Dear all,

java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode, sharing)

on Linux/Debian.

I would want create undecorated frame but i have a problem in setting frame location.
Indeed I create differents components on JFrame and when I drag them I would want to move the frame (such as decorated JFrame when presses mouse over titlebar and drag title bar = the frame changes her location..).
But my frame moves with a jerked behaviour and turns around all the screen.I must stop mouse moving in order to stabilize the frame location in screen.The phenomen is more important if I cross through first screen to second screen with my frame.

source code example :

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class UndecoratedFrame {

/**
* Press on label and move mouse,the window shall be able to move correctly.
* But the frame move in differents directions and the phenomen is more important if
* you have multi screen.When you perform a drag from first screen into second screen
* the frame movement jerked.
*/
private static void createAndShowGUI() {

//Create and set up the window.
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());

JLabel label = new JLabel("Drag Me");
label.setBorder(BorderFactory.createEtchedBorder());
mainPanel.add(label, BorderLayout.NORTH);

label.addMouseListener(new MouseDragAdapter(frame));
label.addMouseMotionListener(new MouseDragAdapter(frame));

//Display the window.
frame.setContentPane(mainPanel);
frame.setPreferredSize(new Dimension(200,200));

frame.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2){
System.exit(0);
}
}
});

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

private static class MouseDragAdapter implements MouseListener,MouseMotionListener{

private final Point origin = new Point();
private JFrame frame;

public MouseDragAdapter(JFrame frame){
this.frame = frame;
}

public void mousePressed(MouseEvent e) {
origin.x = e.getX();
origin.y = e.getY();
}

public void mouseDragged(MouseEvent e) {
Point p = frame.getLocationOnScreen();
frame.setLocation(p.x + e.getX() - origin.x,p.y + e.getY() - origin.y);
}

public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}

}

}

Thanks you in advance for all assistances.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 14 2007
Added on Mar 7 2006
14 comments
949 views