Reworded: Wait until button pressed

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 JFrame 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)
{

}
});

lContainer.add(lDerivsPanel, BorderLayout.NORTH);
lContainer.add(lOkCancelPanel, BorderLayout.SOUTH);
lFrame.show();

// wait until button clicked for return value
}


Basically, I dont want to return from the method until the user has clicked
either Ok or Cancel. Does anyone know how to do this?
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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top