JComboBox Problem

Hi Friends

I have a problem in JComboBox. That is I am implementing ActionListener iterface to get JComboBox's Selected Items. This is working well for me When there are More Than One item in the JComboBox. It is not working When there is only one item in the JComboBox. That is i am not able to get the Selected Value.

Please help me to come out of this Problem

Thank you for your service

cheers

Jofin

[440 byte] By [jofin123a] at [2007-11-27 0:43:12]
# 1
i am unsure as to why you need to implement an action listener for this.will the method getSelectedIndex() not suffice?perhaps a code example would help?
adam217a at 2007-7-11 23:01:45 > top of Java-index,Desktop,Core GUI APIs...
# 2
Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html]How to Use Combo Boxes[/url] for a working example.
camickra at 2007-7-11 23:01:45 > top of Java-index,Desktop,Core GUI APIs...
# 3

Hi i went through the Tutorials

Now i am want to know how to fire the ActionEvent in the JComboBox When there is only one item in the JComboBox

My code is here.import java.awt.event.*;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.*;

public class ActionTest extends JFrame implements ActionListener{

JComboBox jComboBox ;

public ActionTest() {

String[] items = { "item1"};

jComboBox = new JComboBox(items);

jComboBox.addActionListener(this);

JPanel pnl= new JPanel();

pnl.add(jComboBox);

getContentPane().add(pnl);

pack();

setVisible(true);

}

public static void main(String[] args) {

ActionTest actionTest = new ActionTest();

actionTest.setSize(250,250);

}

public void actionPerformed(ActionEvent e) {

Object obj=e.getSource();

if(obj==jComboBox)

{

System.out.println("Action Event Fired");

}

}

}

i want to fire the ActionEvent now with the One item in the JComboBox

Please help me to come out of this problem

Thank you for your service

Cheers

Jofin

jofin123a at 2007-7-11 23:01:45 > top of Java-index,Desktop,Core GUI APIs...
# 4

> i want to fire the ActionEvent now with the One item in the JComboBox

Wroks fine for me. Every time I reselect the item it fires the ActionEvent. I'm using JDK1.4.2 on XP.

If you want it to fire the first time you select the item then you can try the following:

jComboBox = new JComboBox(items);

jComboBox.setSelectedIndex(-1);

camickra at 2007-7-11 23:01:45 > top of Java-index,Desktop,Core GUI APIs...
# 5

Hi I tried this out.

when i set The Selected index -1 this ActionEvent is Fired just for once. That the first time when i click on the Item.

When i try to click on the item for the Second time it is not working

Please help me to come out of this Problem

Thank you very much for your service

cheers

Jofin

jofin123a at 2007-7-11 23:01:46 > top of Java-index,Desktop,Core GUI APIs...
# 6

> when i set The Selected index -1 this ActionEvent is Fired just for once. That the first time when i click on the Item.

Well, then it sounds like a platform issue.

But I don't see what is wrong with this behaviour. It makes sense to me. You get notified when the selection changes. If you select the same item over and over, why do you need to know a second time, since you have already done the required processing the first time the item was selected.

camickra at 2007-7-11 23:01:46 > top of Java-index,Desktop,Core GUI APIs...