Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: JDialog

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default JDialog

    Hi.
    This is my code:
    JDialog dialog = new  JDialog();
    	dialog.setSize(400, 150);
    	dialog.setTitle("Input dialog");
    	dialog.add( new JLabel("simtime(min)") );
    	dialog.add( new JLabel("interval(sec)") );
    	dialog.setVisible(true);
    The problem is that the two lables replace each other.How to position them where we want?

    ?Thanks in advance


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    411
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: JDialog

    By default, the first label is going to be the same location as the second label.
    You need to set the x/y coordinates so the computer knows where to position each
    label. If you have no coordinates, you will only see the most recent label as this is
    placed on top of the other labels of the creative stack.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    hooshdar3 (July 14th, 2014)

  4. #3
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: JDialog

    Quote Originally Posted by Ada Lovelace View Post
    By default, the first label is going to be the same location as the second label.
    You need to set the x/y coordinates so the computer knows where to position each
    label. If you have no coordinates, you will only see the most recent label as this is
    placed on top of the other labels of the creative stack.

    Wishes Ada xx
    How to specifythe coordinates?

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,457
    My Mood
    Hungover
    Thanks
    144
    Thanked 638 Times in 546 Posts

    Default Re: JDialog

    You shouldn't specify the location or size manually. You should use a layout manager instead. Recommended reading: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. The Following User Says Thank You to KevinWorkman For This Useful Post:

    hooshdar3 (July 14th, 2014)

  7. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,334
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: JDialog

    I presume Ada is referring to the setLocation method of JComponent - this will not work with most layout managers. The default LayoutManager of the JDialog content pane is BorderLayout - google this for how it works. For instance, adding the second JLabel to BorderLayout.NORTH will allow both labels to be seen, however this may not be the behavior you wish so I recommend playing with layout's linked in Kevin's post

  8. The Following User Says Thank You to copeg For This Useful Post:

    hooshdar3 (July 14th, 2014)

  9. #6
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: JDialog

    Well, I used the method setBounds(). Thank you guys

  10. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,457
    My Mood
    Hungover
    Thanks
    144
    Thanked 638 Times in 546 Posts

    Default Re: JDialog

    Quote Originally Posted by hooshdar3 View Post
    Well, I used the method setBounds(). Thank you guys
    Like we said, that is almost definitely not the way to go. You should get into the habit of using a layout manager, otherwise you're in for a world of headaches down the road.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. which is better jdialog or joptionpane?
    By tonu in forum AWT / Java Swing
    Replies: 1
    Last Post: January 6th, 2014, 06:01 AM
  2. Is a modal JDialog possible in this scenario?
    By JAPA1972 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 17th, 2013, 04:01 AM
  3. Need some help with this JFrame, or JDialog.
    By MR bruto in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 10th, 2013, 08:14 AM
  4. JDialog Failing to Render...
    By snowguy13 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 7th, 2012, 11:55 AM
  5. [SOLVED] sending a parameter to a new JDialog
    By luisp88 in forum AWT / Java Swing
    Replies: 4
    Last Post: October 5th, 2011, 09:31 AM