return a String on click of a JButton

Discussion in 'Java' started by Allan Bruce, Jun 15, 2004.

  1. Allan Bruce

    Allan Bruce Guest

    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
    Allan Bruce, Jun 15, 2004
    #1
    1. Advertising

  2. In article <camo1h$uc9$2surf.net>,
    "Allan Bruce" <> 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
    Phillip Mills, Jun 15, 2004
    #2
    1. Advertising

  3. Allan Bruce

    Allan Bruce Guest

    "Phillip Mills" <> wrote in message
    news:d7Dzc.23611$...
    > In article <camo1h$uc9$2surf.net>,
    > "Allan Bruce" <> 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
    Allan Bruce, Jun 15, 2004
    #3
  4. Phillip Mills, Jun 15, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bao

    Help JButton

    Bao, Jul 18, 2003, in forum: Java
    Replies:
    2
    Views:
    442
    Kevin Pors
    Jul 19, 2003
  2. Sandip Chitale
    Replies:
    0
    Views:
    696
    Sandip Chitale
    Aug 7, 2003
  3. patrick

    JButton auto click

    patrick, Apr 2, 2004, in forum: Java
    Replies:
    1
    Views:
    1,043
    Steve W. Jackson
    Apr 2, 2004
  4. Replies:
    1
    Views:
    1,299
  5. Todd
    Replies:
    5
    Views:
    1,019
    Roedy Green
    Aug 23, 2007
Loading...

Share This Page