944,149 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3962
  • Java RSS
Mar 1st, 2007
0

Combo box problem

Expand Post »
hey guys., Help me out..

I am doing some project using java swing..in that i get stuck up with one problem in combo box...

I am having 5 items in the combo box(Contains same item names)
for ex:
abc
abc
abc
abc
abc

when i select any one of these item...i am getting the same index value for all items

for Ex:

if i select first abc means it should return the index value 0
and for second item it should return 1
like wise it should retrun index values for all..

but i am getting only 0 for all selected items....

if i change the items in to differnt names.. i am getting the correct output...

Help me out from this bug......
Similar Threads
Reputation Points: 119
Solved Threads: 7
Practically a Posting Shark
vinod_javas is offline Offline
871 posts
since Feb 2007
Mar 1st, 2007
0

Re: Combo box problem

I wonder what your code is...
Sounds like a problem with your ComboboxModel.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Mar 2nd, 2007
0

Re: Combo box problem

Here is my code..just try this one....
you will get correct out put ..
but if u change that item name means
abc1
abc2
abc3
abc4
abc5


to

abc
abc
abc
abc
abc

i cant get the selected index value .. please someone giveme solution








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

public class combo extends JFrame
{
private JComboBox jComboBox1;
private JPanel contentPane;
JTextField jtf=new JTextField();
public combo()
{
super();
initializeComponent();
this.setVisible(true);
}

private void initializeComponent()
{
jComboBox1 = new JComboBox();
contentPane = (JPanel)this.getContentPane();

jComboBox1.addItem("abc1");
jComboBox1.addItem("abc2");
jComboBox1.addItem("abc3");
jComboBox1.addItem("abc4");
jComboBox1.addItem("abc5");

jComboBox1.addItemListener(new cbx2dDtime());
contentPane.setLayout(null);
addComponent(contentPane, jComboBox1, 135,70,100,22);
addComponent(contentPane, jtf, 164,110,250,22);
this.setTitle("combo - extends JFrame");
this.setLocation(new Point(0, 0));
this.setSize(new Dimension(390, 300));
}

private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}

public class cbx2dDtime implements ItemListener
{
public void itemStateChanged(ItemEvent ie)
{
int sitem=jComboBox1.getSelectedIndex();
jtf.setText("Selected index "+sitem);
}
}

public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
new combo();
}
}
Reputation Points: 119
Solved Threads: 7
Practically a Posting Shark
vinod_javas is offline Offline
871 posts
since Feb 2007
Mar 3rd, 2007
0

Re: Combo box problem

guys Help me out.......I got confused with this problem...
Reputation Points: 119
Solved Threads: 7
Practically a Posting Shark
vinod_javas is offline Offline
871 posts
since Feb 2007
Mar 3rd, 2007
0

Re: Combo box problem

it's quite simple.
The JComboBox check whether the item you selected is in its list of items.
If the same item is in the list more than once (as in your case) it will simply return one of them (and almost certainly, based on my knowledge of the actual implementation of the class, the first one of those items).
It simply can't distinguish between them, as they are indeed all identical.
To change that create a custom ComboBoxModel and assign that to the JComboBox. In that make sure that the items are unique.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java compiler
Next Thread in Java Forum Timeline: to get current time in mobile..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC