GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes getText() doesn't work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "getText() doesn Watch "getText() doesn New topic
Author

getText() doesn't work

Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
Hello,

I'm trying to use getText(); to retrieve a string from a dialog box with a text field in it. It doesn't seem to work. Has anyone else had trouble with getText? I'm using JDK 1.4. Thanks.

See the last two lines for use of the getText() method:

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);
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
Its generally not wise to assume something is wrong with something widely released in use by tons of people, the correct question is what am I doing wrong.

The answer to that question appears to be you call getText() before any text could have been set by the user. Your input dialog pops up and immediately the code keeps executing and calls get getText() and pops up the message dialog.

A simple fix for this test code is to make the new_dialog modal. In the long run though your code needs to be event driven and respond to user events.
Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
Obviously I'm doing something wrong. Question needs rephrasing. Dialog cannot be modal. Doesn't work with Pro/E.
Jessica Dancel
Greenhorn

Joined: Dec 07, 2006
Posts: 10
Can you show your declaration of new_name, as well as what specifically "doesn't work" (error message, null value returned, etc.)?
Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
Here is my code. Please note that the J_Link application in Pro/E requires a start and stop method rather than a main method:

import javax.swing.*;
import java.io.*;
import java.awt.*;

public class Jerry_Test
{
//public static String new_name;

public static void start()
{

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

JOptionPane new_pane = new JOptionPane(new_panel);
new_pane.setWantsInput(true);

JDialog new_dialog = new_pane.createDialog(parentFrame, "Test dialog");
new_dialog.setModal(false);
new_dialog.setVisible(true);
new_dialog.toFront();
String new_name = new_text.getText();
JOptionPane.showMessageDialog(parentFrame, "New name is: "+new_name);

}

public static void stop()
{
}

}

The message dialog pops up but does not display the new_name string.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Jerry,

You've added a JLabel to a JPanel, but otherwise, you have nothing but a bunch of disassembled components; and in particular, the JTextField you create is never added to any container, let alone a visible one, so it's impossible for the user to ever enter any text into it -- it never appears on the screen. And I don't know why you've created a JFrame at all.

Here is some code which uses JOptionPane to get a String from the user:

String newName = JOptionPane.showInputDialog(null, "Enter new name");

That's it. If I were you I'd spend some time with Sun's tutorial on Swing.


[Jess in Action][AskingGoodQuestions]
Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
Hi Ernest,

Thanks for the reply to my question. If only I could use JOptionPane.showInputDialog()! I would make my life so much easier! The trouble is, I am implementing Java using J-Link with Pro/Engineer CAD software. The nice showInputDialog window always appears behind the Pro/Engineer window whether I user toFront() or a number of other methods I've tried.

My java components may all be disassembled; however, the dialog box does appear on the screen and the user can enter text and hit OK. The dialog box then closes. The trouble is, I can't seem to retrieve the string from the text field. I think I need to use an action listener or document listener.

Thanks again!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jerry Goldsmith:
If only I could use JOptionPane.showInputDialog()! I would make my life so much easier! The trouble is, I am implementing Java using J-Link with Pro/Engineer CAD software. The nice showInputDialog window always appears behind the Pro/Engineer window whether I user toFront() or a number of other methods I've tried.


That typically means that the JOptionPane has the wrong parent component. I don't know Pro/Engineer at all, but if you can get a reference to it's main window and use that as the parent component, I would expect it to work.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
GeeCON Prague 2014
 
subject: getText() doesn't work