simple input box ok button?



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();
}

.


Quantcast