Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > return a String on click of a JButton

Reply
Thread Tools

return a String on click of a JButton

 
 
Allan Bruce
Guest
Posts: n/a
 
      06-15-2004
I have a method in one of my classes which adds a few items to a JComboBox,
and buttons for Ok and Cancel. What I want is for the user to select one of
the items in the JComboBox and click Ok which will cause the method to
return a String. I cant seem to do this, I think my design needs changing
slightly. Here is what I have:

/**
* getInitialState displays a new JPanel for the variable containing
* a drop doen listBox for each derivative so the user can select the
* values to set up the initial states. This returns a String which
* will be saved to file for parsing later.
*/
public String getInitialState()
{
JFrame lFrame = new JFrame();
Container lContainer = lFrame.getContentPane();
lContainer.setLayout(new BorderLayout());

JPanel lDerivsPanel = new JPanel();
lDerivsPanel.setLayout(new GridLayout(rows, cols, 5, 5));

// go through each derivative in turn
for (int i=0; i<mNumDerivatives; i++)
{
/*
* for each derivative, display the deriv, and underneath
* display a listbox
* if this is the 0th deriv and the variable is exogenous
* then display a checkbox to determine if the variable is
* fixed
*/
JPanel lSubPanel = new JPanel();
lSubPanel.setLayout(new GridLayout(3, 1, 2, 2));

// add the label of the deriv
String lDerivName = "deriv: " + i;
JLabel lLabel = new JLabel(lDerivName);
lSubPanel.add(lLabel);

// add the qspace tuples in a listbox
JComboBox lComboBox = new JComboBox(mQSpace[i].tupleLabels());
lSubPanel.add(lComboBox);

// add the checkbox for fixed if necessary
if (i==0 && mExoVar)
{
JCheckBox lCheckBox = new JCheckBox("Fixed");
}

lDerivsPanel.add(lSubPanel);
}

// set up the Ok and Cancel buttons
JPanel lOkCancelPanel = new JPanel();
JButton lOkButton = new JButton("Ok");
lOkButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
// if I return a String here then I get an error since
// actionPerformed is a null method
}
});

JButton lCancelButton = new JButton("Cancel");
lCancelButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent evt)
{

}
});

// here I want to return my String chosen, or null if cancel is
pressed
}


Thanks
Allan


 
Reply With Quote
 
 
 
 
Phillip Mills
Guest
Posts: n/a
 
      06-15-2004
In article <camo1h$uc9$(E-Mail Removed)2surf.net>,
"Allan Bruce" <(E-Mail Removed)> wrote:

> // here I want to return my String chosen, or null if cancel is
> pressed


Maybe I'm missing something, but why do you expect either button to be
pressed before you exit this method?

Seems to me you either need a dialog if you want the string immediately,
or you need OK's actionPerformed to do the saving if clicking one of the
buttons is completely optional.

--
Phillip Mills
Multi-platform software development
(416) 224-0714
 
Reply With Quote
 
 
 
 
Allan Bruce
Guest
Posts: n/a
 
      06-15-2004

"Phillip Mills" <(E-Mail Removed)> wrote in message
news:d7Dzc.23611$(E-Mail Removed). ..
> In article <camo1h$uc9$(E-Mail Removed)2surf.net>,
> "Allan Bruce" <(E-Mail Removed)> wrote:
>
> > // here I want to return my String chosen, or null if cancel is
> > pressed

>
> Maybe I'm missing something, but why do you expect either button to be
> pressed before you exit this method?
>
> Seems to me you either need a dialog if you want the string immediately,
> or you need OK's actionPerformed to do the saving if clicking one of the
> buttons is completely optional.
>


I dont want the method to return until a button has been pressed, is this
possible to do?
Thanks
Allan


 
Reply With Quote
 
Phillip Mills
Guest
Posts: n/a
 
      06-15-2004
In article <can04g$14v$(E-Mail Removed)2surf.net>,
"Allan Bruce" <(E-Mail Removed)> wrote:

> I dont want the method to return until a button has been pressed, is this
> possible to do?


That is just about the definition of a modal dialog.

<http://java.sun.com/docs/books/tutor.../dialog.html#i
nput>

--
Phillip Mills
Multi-platform software development
(416) 224-0714
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
click click click Andy Morrison Computer Support 7 12-20-2007 06:50 AM
SIngle-click on JButton registers multiple times - why? Todd Java 5 08-23-2007 10:05 AM
Display Multiple icons on a single JButton after each click arash470@googlemail.com Java 1 02-14-2007 07:58 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
JButton auto click patrick Java 1 04-02-2004 05:21 PM



Advertisments