Problem in JComboBox selected Index change

hi all

Can anyone send me the code for Loading values to JComboBox & then i want to Display Particular value for that Loaded value in JTextField.

Ex:

I loaded category cade for JComboBox & when JComboBox selected index change the particular category name should appear in JTextBox

Thankz

[326 byte] By [Sana_Boya] at [2007-11-27 7:01:09]
# 1

Hi,

I am sending a sample program.

Try this, if u had any problem just mail me at doubtsplz@gmail.com

import javax.swing.*;

import java.util.*;

import java.io.*;

import java.awt.*;

import java.awt.event.*;

public class SwingDemo extends JFrame implements ItemListener

{

JComboBox combo;

JTextField text;

Properties props;

public SwingDemo() throws Exception

{

super();

combo=new JComboBox();

text=new JTextField();

addItems("combodata.dat");

setLayout(new GridLayout(2,1));

add(combo);

add(text);

combo.addItemListener(this);

setTitle("JComboBox - Demo");

setSize(400,400);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pack();

}

public static void main(String[] args) throws Exception

{

new SwingDemo();

}

public void addItems(String str) throws Exception

{

FileInputStream fis=new FileInputStream(str);

props=new Properties();

props.load(fis);

Enumeration e=props.propertyNames();

while(e.hasMoreElements())

{

combo.addItem((String)e.nextElement());

}

}

public void itemStateChanged(ItemEvent ie)

{

//System.out.println(ie.getSource());

//System.out.println((ie.getSource()==combo));

if(ie.getSource()==combo)

{

System.out.println(combo.getSelectedItem());

System.out.println(props.getProperty(combo.getSelectedItem().toString()));

text.setText(props.getProperty(combo.getSelectedItem().toString()));

pack();

}

}

}

For the above program I took data from a file instead of the database

You have to create that file to execute the above program with the name combodata.dat and the values as follwos

1=BSc

2=BCA

3=BA/BCom

4=MSc(IS)

5=MSc(Cs)

6=MCA

7=BE/BTech

8=MBA

Bye...

Thanks & Regards,

Santhosh Reddy Mandadi

santhosh-mcaa at 2007-7-12 18:51:58 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...