Drag undecorated jDialog - java

So, I have a jFrame in which I'm building the main interface window of a chat. This window/jFrame has several buttons , each of which show a jDialog (Which I created previously in Netbeans dragging a jDialog onto the parent(?) jFrame).
My problem is that both windows are set to undecorated = true and so I wish to let the user drag and move all windows at will by clicking and dragging a portion of the windows (Which emulate the title bar when not undecorated)
In all the jFrames I have accomplished this by the following code just after initComponents():
final Point point = new Point(0,0); // Why 'final' and not simply Point point?
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(!e.isMetaDown()){
point.x = e.getX();
point.y = e.getY();
System.out.println("Ratón pulsado: " + point.x + "," + point.y);
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if(!e.isMetaDown() && point.y <= 17){ //Coordinates of title bar, any X and up to 17px from the top border
Point p = getLocation();
setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
System.out.println("Ratón movido: " + (p.x + e.getX() - point.x) + "," + (p.y + e.getY() - point.y));
}
}
});
However, I don't know how to use this code in the jDialog. When I right click it in the Navigator, and select Customize code, then I can't paste it in there because the whole jFrame stops working. I'm new in this thing of jDialogs children of jFrames, so please help me with some guidelines :) Thanks

Well, as #mKorbel suggested, I headed to here where I found a nice class called ComponentMover which helped me to do this. I'll need 2 more reputation so I saved the link to get back and upvote when I'm able to do it.
I'll have to ensure it works perfect and exactly in the way I want, but looks great! Thanks!

Related

MouseListener called multiple times

I am using this code to get the X and Y coordinates of an image placed as icon of a jLable.
This method to get the coordinates was suggested by an answer to this question.
private void lblMapMouseClicked(java.awt.event.MouseEvent evt) {
lblMap.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
double X = e.getX();
double Y = e.getY();
System.out.println("X: " + X + "Y: " + Y );
}
});
}
When I run this public void mouseClicked(MouseEvent e) { } gets called multiple times.
Exactly the amount of times I click on the image.
Eg: If I'm clicking on it for the 3rd time ,
X and Y values from the System.out.println line , gets printed 3 times.
And it increases as the number of times I click increases.
Can any of you explain why this happens? And how can I fix it? :)
The problem is that you are adding a new listener again and again when click happens, here.
private void lblMapMouseClicked(MouseEvent evt)
{
lblMap.addMouseListener(new MouseAdapter()
{
...
Instead, change your code to this.
private void lblMapMouseClicked(MouseEvent e)
{
double X = e.getX();
double Y = e.getY();
System.out.println("X: " + X + "Y: " + Y);
}
And it should fix the problem.
Hope this helps.
it looks for me that every time image is clicked new mouse listener is added.. do also
System.out.println(this)
to check from which instance of mouse listener it is actually printed
The problem with above code was you are creating new Mouse event with every click on the image.
// Create a Mouse pressed Event
mouseLis = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
actionMenthod(e);
}
};
Here am attaching the my event to lblMap.
lblMap.addMouseListener(mouseLis);
After this event happens you have to remove this event from the lblmap.
lblMap.removeMouseListener(mouseLis);
After when I click again only one event will be there then it prints only once.

Java unlimited relative mouse drag events

Is there a way to offer the user unlimited relative mouse drag events in one of the Java GUI frameworks?
I would like to have a GUI element that the user can click and drag, but that element does not move: rather, some parameter (say color, volume, zoom level, etc.) gets adjusted in proportion to how far the user moves the mouse.
The trouble with Swing and SWT is that they want to report mouse coordinates, and those are bounded by the dimensions of the screen. If the mouse pointer hits the edge of the screen during the drag operation, I'm afraid my GUI element will suddenly stop adjusting to further mouse movements by the user.
Is there a way to make this work in Java?
You will probably want to look into the Robot class, specifically Robot#mouseMove(int, int).
You can listen for mouse drag events on the UI component which is being dragged via a MouseMotionListener, find the difference in the drag and reposition the mouse back onto the component.
This is a quick and dirty test to show that the idea works. Note: you will probably want to clean up the positioning of the mouse to stay at the same location; currently it just repositions the mouse to the centre of the JLabel
public static void main(String[] args) throws AWTException {
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(400, 400));
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
final Robot r = new Robot();
JLabel label = new JLabel("Hello World");
label.setBackground(Color.ORANGE);
label.setOpaque(true);
label.addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
JLabel label = (JLabel) e.getSource();
Point point = label.getLocationOnScreen();
point.x += (label.getWidth() / 2);
point.y += (label.getHeight() / 2);
r.mouseMove(point.x, point.y);
Point movedPoint = e.getLocationOnScreen();
int diffX = point.x - movedPoint.x;
int diffY = point.y - movedPoint.y;
System.out.println("Dragged: " + diffX + ", " + diffY);
}
});
panel.add(label);
frame.add(panel);
frame.setVisible(true);
}

Jobjects on top of existing object

so I would like to know if it's possible to put things like buttons, text boxes, words, progress bars, ect, ect, on top of an already existing, in this example, JLabel.
Here is the image of the undercoated frame I made, followed by the snippet of code that is associated with this undercoated frame.
(I dont have 10 reputation, so here is a link to a photo)
http://prntscr.com/15516f
Map.setTitle("Map");
Map.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Map.setUndecorated(true);
Map.setBackground(new Color(0,0,0,0));
Map.setLayout(new FlowLayout());
JLabel Background = new JLabel(new ImageIcon(getClass().getResource("Map.png")));
Background.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
initialClick = e.getPoint();
getComponentAt(initialClick);
}
});
Background.addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
// get location of Window
int thisX = Map.getLocation().x;
int thisY = Map.getLocation().y;
// Determine how much the mouse moved since the initial click
int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);
// Move window to this position
int X = thisX + xMoved;
int Y = thisY + yMoved;
Map.setLocation(X, Y);
}
});
Map.add(Background);
Map.setSize(507,512);
Map.setLocation(0, 100);
Map.setResizable(false);
Map.setVisible(false);
on a side note, and I KNOW this is FlowLayout(), but when I try to add something else, it'll just put itself above, or below my map.
I'd just like to know if I could put things on top of this Map.
Maybe I should put the image in in another way besides the JLabel?
Look into JLayeredPane and similar strategies. See How to Use Layered Panes for more details.

Can't read calls to the ACM mouseMoved method

I'm working through the Stanford CS106A lectures and have hit a snag in the breakout project.
After I addMouseListeners(), I cannot detect mouseMoved calls.
However if I rename mouseMoved() to mouseDragged() or mouseClicked() or mousePressed() etc, it all updates correctly. Only the moved method does not work.
No errors or alerts, just doesn't detect the mouse being moved.
Any idea why that would happen?
public void run() {
/* Add a listener for the mouse */
addMouseListeners();
label = new GLabel("Mouse x & y");
add(label, 50, 50);
/* Load the method to create the brick pattern */
createBricks();
}
/** Detect a mouse move and update something */
public void mouseMoved(MouseEvent e){
label.setLabel("Mouse: " + e.getX() + " , " + e.getY());
paddle.setLocation(e.getX(), getHeight()-PADDLE_Y_OFFSET);
}
You need to use addMouseMotionListeners();. addMouseListeners is only for the functions that you were using.

JDialog Mouse Listener called as not expected

In my swing application, I have used JDialog and added a JPanel to the JDialog. I want the mouse listener of JDialog called when the mouse exits the JDialog. Here is how I did it.
myJDialog.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseExited(java.awt.event.MouseEvent evt) {
System.out.println("Mouse has exited the Dialog");
}
});
Actually it is called when the mouse exits, but when the mouse goes into a JTextField which is in the Jpanel that added to JDialog, this listener is called as the mouse exited even though the mouse is still inside the JDialog. I want to get the listener called when completely the mouse exited and not called when the mouse goes to its child component which is in itself. How can I solve this?
This is not an easy question to answer.
Lets start with the mouseListener. Try and think of your container (in this case the dialog) as a 3D plan, on top of that plan, we add components. Each component exists on one or more layers above the container (casting a nice shadow).
Now, think of the mouse as a laser pointing down onto the top of that plan. So long as the laser light can reach your container, you will receive notification about mouse events. However, if the mouse is moved so that another component blocks it (the laser can no longer reach your component), you stop receiving notification, as the the events are blocked.
Now, the notification system is kind enough to let you know when the mouse enters and exists you domain of influence.
This is how the system works. Other then using a Global Event Handler (which will at least let you know where the mouse context has moved to), the only "other" solution I can think of is to check the coordinates of the mouse exit event and check them against your content pane's bounds.
public void mouseExit(MouseEvent evt) {
Point p = evt.getPoint();
Rectangle bounds = getBounds();
bounds.x += 4;
bounds.y += 4;
bounds.width -= 4;
bounds.height -= 4;
if ((p.x < bounds.x || p.x > bounds.x + bounds.width) && (p.y < bounds.y || p.y > bounds.y + bounds.height) {
System.out.println("Elvis has left the building");
}
}
This of course is probably going to need some tweaking in order to get just right.

Categories

Resources