•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 392,082 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,973 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1946 | Replies: 4
![]() |
•
•
Join Date: Feb 2007
Location: India-Tamilnadu-Chennai
Posts: 871
Reputation:
Rep Power: 3
Solved Threads: 7
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......
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......
Adios,
Vinod......
Vinod......
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation:
Rep Power: 18
Solved Threads: 191
•
•
Join Date: Feb 2007
Location: India-Tamilnadu-Chennai
Posts: 871
Reputation:
Rep Power: 3
Solved Threads: 7
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();
}
}
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();
}
}
Adios,
Vinod......
Vinod......
•
•
Join Date: Feb 2007
Location: India-Tamilnadu-Chennai
Posts: 871
Reputation:
Rep Power: 3
Solved Threads: 7
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation:
Rep Power: 18
Solved Threads: 191
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.
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.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
- Combo box selection problem (VB.NET)
- VBA Combo Box Problem (Visual Basic 4 / 5 / 6)
- Combo Box In Jsp+ Ajax Urgent Please Help Me (JSP)
- Perplexed: Combo Box Not Holding Data (C#)
- combo box (PHP)
- combo box (ASP)
Other Threads in the Java Forum
- Previous Thread: Java compiler
- Next Thread: to get current time in mobile..



Linear Mode