I have a JDialog
being displayed on screen and I want to simulate its movement (Drag from one location to another) based on a condition. Is there any way this can be done ?
Asked
Active
Viewed 123 times
0

Dan D.
- 32,246
- 5
- 63
- 79

DarkKnight
- 243
- 1
- 4
- 10
-
2Please clarify -- what do you mean by `enterDragAndLeave()`? What have you tried? Please post a small compilable program that demonstrates your problem, an [sscce](http://sscce.org). In fact you were asked for this in your last question, and in fact I showed you an example of this in your last question. This will require a bit of effort on your part, but it's worth it for both you and us. – Hovercraft Full Of Eels Aug 24 '12 at 20:22
-
I tried to use setLocation() but it is not changing the location and all components inside JDialog became invisible. with enterDragAndLeave() i want to simulate the effect similar when a user clicks on JDialog and drags it to a particular location – DarkKnight Aug 24 '12 at 20:27
-
1Without showing your code, we have no idea what you're doing wrong. Again, I suggest that you create and post an SSCCE (certainly we don't want to see the whole program). Yes this will mean some effort on your part, but no more than the effort I expended to create the SSCCE for your previous question. – Hovercraft Full Of Eels Aug 24 '12 at 20:30
-
are you want to moving with JDialog programatically – mKorbel Aug 24 '12 at 20:57
-
@mKorbel : yes. I want the JDialog to move programatically. – DarkKnight Aug 24 '12 at 21:04
-
21) get screen resolution from GraphicsEnviroment, 2) create JDialog then show it, 3) start Swing Timer on some period, 4) if period from Timer is odd then move with JDialog on odd pixels or to same with even number :-) 5) good period is 33 - 50milisecond, 6) then move to the next tw0 pixels somewhere 7) something must happens in the case that JDialog is close to screens bounds 8) for better help sooner post an SSCCE – mKorbel Aug 24 '12 at 21:10
-
2@Dark: where is your SSCCE? How are we supposed to guess what is not working for you if you won't show code? – Hovercraft Full Of Eels Aug 24 '12 at 21:20
1 Answers
3
See this piece of code below. I have just tested it and it works fine. It is just a proof of concept.
private void startDialog() {
final JDialog d = new JDialog(this, "Test", true);
d.getContentPane().add(new JLabel("Something"));
d.setBounds(100, 100, 400, 300);
Thread t = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 50; i++) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Point p = d.getLocation();
d.setLocation(p.x + 10, p.y + 10);
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// ignore
}
}
}
});
t.start();
d.setVisible(true);
}
You can improve the code yourself:
- use a
Timer
instead of a regularThread
- tweak the sleep times and the location jumps and so on
Just call this method from any Swing application and it will work.

Dan D.
- 32,246
- 5
- 63
- 79
-
@ Dan: this only moves the panels inside JDialog. JDialog remains at its position – DarkKnight Aug 24 '12 at 21:05
-
Are you kidding or something? Which are the panels that are moved? I tested the code before posting it here. – Dan D. Aug 24 '12 at 21:07
-
What components do the moving panels contain? How do they move (left to right,...)? – Dan D. Aug 24 '12 at 21:12
-
The thing you see moving is the JDialog. It is nothing else moving. I started to get downvotes trying to help you. I will stop now. – Dan D. Aug 24 '12 at 21:15
-
Thanks. I think we are all curious to have the asker explain us what is a JDialog and what a panel inside a JDialog is. – Dan D. Aug 24 '12 at 21:21
-