Results 1 to 8 of 8
- 01-12-2009, 04:15 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 35
- Rep Power
- 0
setLocation on a JDialog is ignored
Hello-
I'm trying to set the location of a JDialog.
For some reason my setLocation() call seems to be ignored, and the JDialog just remains at 0,0 all the time.
Does anyone know what I'm doing wrong?
I've created a simple class below which illustrates the issue:
import javax.swing.*;
import java.awt.event.*;
public class JDialogTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setLocation(300,300);
frame.setSize(300,300);
JDialog dialog = new JDialog(frame, true /*modal*/);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
dialog.add(new JLabel("Test"));
dialog.pack();
dialog.setVisible(true);
dialog.setLocation(200,200);
}
}
My setLocation(200,200) call is ignored, and the dialog remains at 0,0 (upper left corner of the screen).
Interestingly, the frame.setLocation(300,300) is accepted, and you can see the frame is at 300,300.
What do you think?
-scott
- 01-12-2009, 04:49 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
First of all, please use code tags when you posting code segments here in the forum. Unformated codes are really hard to read.
Before set the visible, you must set the location. Swing components cannot dynamically re-generated after visibility. Simple set the location before set visible to true.
- 01-12-2009, 05:02 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 35
- Rep Power
- 0
Yes, that was it. Thanks! -scott
- 01-12-2009, 05:08 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Nice, if you have solve the problem please mark it using thread tools.
- 01-12-2009, 07:23 PM #5
The solution is correct, but the reasoning is wrong. Try the same code with a non-modal dialog and you'll see that the location is set correctly.
The correct reasoning, which applies to all GUI programming platforms, and not just Java/Swing, is that when a modal window is set to visible, the thread of code execution is halted till the modal window is dismissed.
In this case, setLocation is actually executed after the dialog is closed. Which of course doesn't help.
dbLast edited by DarrylBurke; 01-12-2009 at 07:27 PM.
- 01-13-2009, 03:12 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What did you say is, code execution thread prevent the execution of GUI?
What I'm trying to say is, setVisble() calls before set the location. So it's place in default location. Once the dialog is located in default locations not possible to move into another location. What I know is, once the window is located in a place the UI thread exist. Darryl correct me if I'm wrong.
- 01-13-2009, 07:28 AM #7
Illustrative SSCCE:
Code execution continues when a non modal dialog is setVisible(true), but is suspended when a modal dialog is shown. This is true for all visual platforms (VB, VFP, VC++ etc)Java Code:import java.awt.Frame; import javax.swing.JDialog; import javax.swing.SwingUtilities; public class LocateDialog { JDialog dialog; public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new LocateDialog().makeUI(); } }); } public void makeUI() { dialog = new JDialog((Frame) null, "One - non modal", false); dialog.setSize(200, 200); System.out.println("showing One"); dialog.setVisible(true); System.out.println("One shown"); dialog.setLocation(100, 100); dialog = new JDialog((Frame) null, "Two - non modal", false); dialog.setSize(200, 200); System.out.println("showing Two"); dialog.setVisible(true); System.out.println("Two shown"); dialog.setLocation(400, 100); dialog = new JDialog((Frame) null, "Three - modal", true); dialog.setSize(200, 200); System.out.println("showing Three"); dialog.setVisible(true); System.out.println("Three shown (and closed!)"); dialog.setLocation(100, 400); } }
db
- 01-13-2009, 07:35 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Thanks a lot db. I got the point what you saying. Seems I've learn it in wrong way.
Similar Threads
-
Returning data from a JFrame/JDialog?!
By Joe2003 in forum AWT / SwingReplies: 6Last Post: 01-08-2009, 12:14 AM -
help with jdialog
By leonard in forum AWT / SwingReplies: 1Last Post: 08-05-2007, 05:37 AM -
Close a JDialog Programmatically
By Marcus in forum Advanced JavaReplies: 1Last Post: 07-06-2007, 04:06 PM -
problems with jDialog in a JFrame
By bbq in forum AWT / SwingReplies: 1Last Post: 07-05-2007, 04:14 AM -
How to get rid of minimize and close icon on a jdialog?
By sandor in forum AWT / SwingReplies: 1Last Post: 04-09-2007, 12:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks