Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Adding a text field to JDialog with JTextField  RSS feed

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to add a text field to a dialog created using JDialog.
Basically I'm trying to get the same effect given by
JOptionPane.showInputDialog but have to use JDialog since the application
I'm customizing (Pro/Engineer CAD software) always places JOptionPane
related windows behind the main application window.

Here is a snippet of what I'm trying to do:

JTextField new_text = new JTextField("Test text");
JDialog new_dialog = new JDialog(parentFrame, "Test");
new_dialog.pack();
new_dialog.setVisible(true);
new_dialog.toFront();
new_dialog.setSize(500,150);
new_dialog.setLocation(500,500);
new_dialog.add(new_text);

The dialog box shows up, but no text field. Disclaimer: I'm something of
a noob at Java (if you couldn't tell).
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTextField new_text = new JTextField("Test text");
JDialog new_dialog = new JDialog(parentFrame, "Test");
new_dialog.pack();
new_dialog.setVisible(true);
new_dialog.toFront();
new_dialog.setSize(500,150);
new_dialog.setLocation(500,500);
new_dialog.add(new_text);

any reason why you are doing pack first? set the size, location, etc and then finally do the pack()
 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sam. Since my last post I've modified my code as follows:

JFrame parentFrame = new JFrame();
JLabel new_label = new JLabel("Is this really the test label?");
JTextField new_text = new JTextField();
JPanel new_panel = new JPanel();
new_label.setLabelFor(new_text);
new_panel.add(new_label);

Object[] new_object = {new_label};
JOptionPane new_pane = new JOptionPane(new_text);
new_pane.setWantsInput(true);

JDialog new_dialog = new_pane.createDialog(parentFrame, "Test");
new_dialog.setModal(false);
new_dialog.setVisible(true);
new_dialog.toFront();
new_name = new_text.getText();

JOptionPane.showMessageDialog(parentFrame, new_name);

This adds the text field to the dialog and everything works until I try to retrieve the string from the text field. The getText() method doesn't seem to work in this instance.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!