Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hai frnds,

i need a solution pls help me.

i read an id and its value from a table (database table)and i need to ad this values to a combobox to make a list.
i need to show each name as item and when an item is selected i need the curresponding id.

is there is an option to bound each name as item and id as its value in java??
Posted

Make the two properties into an object which you then add to your combobox. You can then use getSelectedItem()[^] to return the object which contains both string and id.
 
Share this answer
 
Comments
ranju007 5-Aug-12 15:16pm    
can u make me more clear sir???
Richard MacCutchan 5-Aug-12 15:18pm    
Follow the link I gave you, and read the documentation on how to use the methods and properties of the JComboBox.
Java Tutorial on Combobox[^]

Check that Tutorial.

What looks pretty nice:
- create an Object with the String (id?)to display and the corresponding value in it.
Make sure there is a toString() in the Object returning the text that should be displayed.

- add a List of these Objects to the ComboBox.
It will automatically display the strings and you will easily find the corresponding value (see Richards answer) to the displayed String.

EDIT:
See this demo. The Combobox oComboBox is filled with values from the Object ComboValue. The ComboValue owns a method toString() which provides the String for the ComboBox to display.
CustomComboBoxListener is called as the ActionListener. He checks if the Component is the one with the ActionCommand and then extracts the value (can be done in a one-liner, here more lines to show what happens). The ActionListener just goes for the getSelectedItem() and does not even care for index numbers. So there can not be a "false friend" where you get the false value due to false counting.

I hope this is pretty clear - it's simple though.

For those searching for a simple code to copy for homework:
Have fun explaining the code to your teacher ;-)


Java
/**
 * Just a simple JFrame for demo
 */
public class MyFrame extends JFrame {
	private JComboBox oComboBox;
	private static final String ACTIONCOMMAND = "ComboBox";

	public MyFrame(){
		this.ignition();
	}

	private void ignition() {
		createCombobox();
		getContentPane().add(oComboBox, BorderLayout.NORTH);
	}

	private void createCombobox() {
		oComboBox = new JComboBox(getItems());
		oComboBox.setActionCommand(ACTIONCOMMAND);
		oComboBox.addActionListener(new CustomComboBoxListener());
	}

	// provides dumy data
	private ComboValue[] getItems() {
		ComboValue[] oItems = new ComboValue[3];
		oItems[0] = new ComboValue(123, "randomNumber 1");
		oItems[1] = new ComboValue(456, "randomNumber 2");
		oItems[2] = new ComboValue(789, "randomNumber 3");
		return oItems;
	}
	
	/**
	 * reacts to the events
	 */
	private class CustomComboBoxListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent oEvent) {
			if(oEvent.getActionCommand().equals(ACTIONCOMMAND)){
				JComboBox oBox = (JComboBox)oEvent.getSource();
				ComboValue oValue = (ComboValue)oBox.getSelectedItem();
				int iID = oValue.getID();
				System.out.println("The selected ID is: " + iID);
			} 
		}
	}
	
	/**
	 * Should be defined in own class file.
	 * Holds String Name and int ID (to be changed if needed)
	 */
	private class ComboValue {

		private int iID;
		private String strName;
		
		public ComboValue(int iID, String strName){
			 setID(iID);
			 setName(strName);
		}
		
		public void setName(String strName) {
			this.strName = strName;
		}
		public String getName() {
			return strName;
		}
		
		public void setID(int iID) {
			this.iID = iID;
		}
		public int getID() {
			return iID;
		}
		
		public String toString(){
			return getName();
		}
	}
}
 
Share this answer
 
v2
Comments
ranju007 6-Aug-12 10:48am    
i cant get you...

i read player id and player name from the database and i saved it in a resultset 're'. i have a jcombobox1. i need to display player names as item in jcombobox. and when i select an item (player name) i need his corresponding id.
TorstenH. 6-Aug-12 15:32pm    
I added an Edit to the answer. I hope you can see what I'm doing there. you can copy the code to a project (you should always have a workspace "playground" for trying things) and instance the MyFrame.
Have fun.
TorstenH. 7-Aug-12 10:54am    
no reaction? did it help or do you need more?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900