return a String on click of a JButton

A

Allan Bruce

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.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
 
P

Phillip Mills

Allan Bruce said:
// 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.
 
A

Allan Bruce

Phillip Mills said:
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
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top