JComboBox event handling problem

Discussion in 'Java' started by Carla, Nov 1, 2008.

  1. Carla

    Carla Guest

    Hi,
    I have a JComboBox1 in JFrame1 and it has two options "Y/N". If the
    option Y is selected, I want to open another JFrame2 and only after
    submitting all the details, one should be able to get back to JFrame1.

    It looks like I am not listening for the right events.

    This is what I am doing currently.

    =========================================================
    private void bagCBoxActionPerformed(java.awt.event.ActionEvent evt)
    {

    if (bagCBox.getSelectedItem().toString() == "Y") {
    new BaggageInfoInsertionGUI().setVisible(true);
    }
    }

    =========================================================
    public void initFormFields() {
    bagCBox.addItem("Y");
    bagCBox.addItem("N");
    }
    =======================================================

    What is happening that even when I am adding these items into the
    JComboBox1, it is opening up the JFrame2. And the worst, even if the
    JFrame2 is opened up - every time I am selecting Y from the JComboBox1
    - it is opening up another JFrame1 every time.

    Please help.
     
    Carla, Nov 1, 2008
    #1
    1. Advertising

  2. Carla wrote:
    > Hi,
    > I have a JComboBox1 in JFrame1 and it has two options "Y/N". If the
    > option Y is selected, I want to open another JFrame2 and only after
    > submitting all the details, one should be able to get back to JFrame1.
    >
    > It looks like I am not listening for the right events.
    >
    > This is what I am doing currently.
    >
    > =========================================================
    > private void bagCBoxActionPerformed(java.awt.event.ActionEvent evt)
    > {
    >
    > if (bagCBox.getSelectedItem().toString() == "Y") {


    Generally it is a bad idea to compare String values using == use
    ..equals("Y") instead. Better still make Y and N constants
    static final String YES = "Y", NO = "N".
    then refer to the YES and NO constants instead of "Y" and "N".
    Better yet use an enum { YES, NO }.
    Even better use a JCheckBox if the only options are YES and NO.

    > new BaggageInfoInsertionGUI().setVisible(true);
    > }
    > }
    >
    > =========================================================
    > public void initFormFields() {
    > bagCBox.addItem("Y");
    > bagCBox.addItem("N");
    > }
    > =======================================================
    >
    > What is happening that even when I am adding these items into the
    > JComboBox1, it is opening up the JFrame2. And the worst, even if the
    > JFrame2 is opened up - every time I am selecting Y from the JComboBox1
    > - it is opening up another JFrame1 every time.


    Sounds like you have a listener on the combobox, normally I'd have a
    listener on an "OK" button.

    >
    > Please help.


    There's not enough source code there for me to help much (though someone
    else might be able to guess more accurately than I can).

    See http://sscce.org for how to create a small complete compilable
    example that illustrates your problem to us.


    --
    RGB
     
    RedGrittyBrick, Nov 1, 2008
    #2
    1. Advertising

  3. Carla wrote:
    > Hi,
    > I have a JComboBox1 in JFrame1 and it has two options "Y/N". If the
    > option Y is selected, I want to open another JFrame2 and only after
    > submitting all the details, one should be able to get back to JFrame1.
    >
    > It looks like I am not listening for the right events.
    >
    > This is what I am doing currently.
    >
    > =========================================================
    > private void bagCBoxActionPerformed(java.awt.event.ActionEvent evt)
    > {
    >
    > if (bagCBox.getSelectedItem().toString() == "Y") {
    > new BaggageInfoInsertionGUI().setVisible(true);
    > }
    > }
    >
    > =========================================================
    > public void initFormFields() {
    > bagCBox.addItem("Y");
    > bagCBox.addItem("N");
    > }
    > =======================================================
    >
    > What is happening that even when I am adding these items into the
    > JComboBox1, it is opening up the JFrame2. And the worst, even if the
    > JFrame2 is opened up - every time I am selecting Y from the JComboBox1
    > - it is opening up another JFrame1 every time.
    >
    > Please help.


    When you respond to the ActionListener for the JComboBox, close the
    first frame and open the other. When you are done, you can do the
    reverse. If you are going to open the second frame several times,
    create all of your GUI up front unless there is some great reason not to.

    Other options are to disable the JComboBox while you have the second
    frame open. Is is possible that you really need a modal dialog instead
    of another frame?

    Simple example below. It doesn't have any code to deal with closing the
    second window with the X.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class test {
    JFrame f1,f2;

    public test() {
    f1 = new JFrame("F1");
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f2 = new JFrame("F2");

    JButton b = new JButton("Close F1 and open F2");
    b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    f1.setVisible(false);
    f2.setVisible(true);
    }
    });
    f1.add(b);
    f1.pack();
    f1.setVisible(true);

    b = new JButton("Close F2 and open F1");
    b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    f2.setVisible(false);
    f1.setVisible(true);
    }
    });
    f2.add(b);
    f2.pack();
    }

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    new test();
    }
    });
    }
    }

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Nov 1, 2008
    #3
    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. Tobi Krausl

    JComboBox problem, pse help!

    Tobi Krausl, Nov 27, 2003, in forum: Java
    Replies:
    6
    Views:
    4,282
    Tobi Krausl
    Dec 1, 2003
  2. Allan Mullan

    JComboBox problem

    Allan Mullan, Apr 18, 2004, in forum: Java
    Replies:
    0
    Views:
    529
    Allan Mullan
    Apr 18, 2004
  3. Derek

    Problem with JComboBox

    Derek, May 9, 2005, in forum: Java
    Replies:
    3
    Views:
    857
  4. Genevieve
    Replies:
    13
    Views:
    3,739
    Genevieve
    Jul 6, 2005
  5. Replies:
    0
    Views:
    607
Loading...

Share This Page