JComboBox event handling problem

C

Carla

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

RedGrittyBrick

Carla said:
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.
 
K

Knute Johnson

Carla said:
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();
}
});
}
}
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top