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