simple input box ok button?

6

6e

Hi,

Im trying to do something simple, but for some reason Im having trouble
with this Java concept..

I am creating a small pop-up input box, and I want the user to be able
to insert text into a JTextField, and hit "ok" to return close the
screen AND return the text in JTextField.

So I know I need to handle actionperformed to make this happen, but I
cannot assign the text this way... The only thing Ive thought of is
creating a global variable for this purpose, but it seems like overkill
for a such a simple routine...

Code is below, I would appreciate some advice. Thanks!

----------
private String SetCategoryName(){

//regular initiation of the objects to be added to the frame
final String strCatName;
JFrame frSetName = new JFrame("Add Category");
JLabel lblCategory = new JLabel("Category Name:");
final JTextField txfText = new JTextField();
JButton butOk = new JButton("OK");
JButton butCancel = new JButton("Cancel");

//layout - currently not used
SpringLayout sprLayout = new SpringLayout();
frSetName.setLayout(sprLayout);

//set size of frame
Dimension dimFrameSize = new Dimension(200, 100);
frSetName.setSize(dimFrameSize);
frSetName.setMaximumSize(dimFrameSize);
frSetName.setMinimumSize(dimFrameSize);

//add objects/buttons to window
frSetName.add(lblCategory);
frSetName.add(txfText);
frSetName.add(butOk);
frSetName.add(butCancel);

//button "ok" is supposed to close the frame AND assign the text to be
returned...
butOk.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {

}});

//return text...
return txfText.getText();
}
 
O

Oliver Wong

6e said:
Hi,

Im trying to do something simple, but for some reason Im having trouble
with this Java concept..

I am creating a small pop-up input box, and I want the user to be able
to insert text into a JTextField, and hit "ok" to return close the
screen AND return the text in JTextField.

So I know I need to handle actionperformed to make this happen, but I
cannot assign the text this way... The only thing Ive thought of is
creating a global variable for this purpose, but it seems like overkill
for a such a simple routine...

Code is below, I would appreciate some advice. Thanks!

I've come across the same problem and I don't know of an elegant
solution for it. Anyway, here's what I usually do:

You have code that actually makes the pop-up show up right? And that's
the code that needs access to the value in the JTextField, right? Well, the
pop-up itself is an object, right? So add a method in the pop-up class that
returns the value in the JTextField. The calling code then creates and shows
the pop up box as usual, and then waits for the "ok" button to be pressed.
It then calls the method you added to get the value that was in the
JTextField.

- Oliver
 
A

Andrew Thompson

I am creating a small pop-up input box, and I want the user to be able
to insert text into a JTextField, and hit "ok" to return close the
screen AND return the text in JTextField. ...
..it seems like overkill for a such a simple routine...

<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JOptionPane.html>

String inputValue = JOptionPane.showInputDialog("Input a value");

HTH

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"Her voice was soft and cool, her eyes were clear and bright, ..but she's
not there"
The Zombies 'She's Not There'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top