• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!