java combobox属性 [英] java combobox properties

查看:113
本文介绍了java combobox属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hai frnds,

我需要一个解决方案,请帮助我.

我从一个表(数据库表)中读取了一个ID及其值,我需要将此值添加到组合框以创建一个列表.
我需要显示每个名称作为项目,当选择一个项目,我需要的curresponding ID.

在java中是否可以选择将每个名称绑定为项目,将ID绑定为其值?

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??

推荐答案

将这两个属性合并为一个对象,然后将其添加到对象中组合框.然后,您可以使用 getSelectedItem() [ ^ ]返回包含字符串和ID的对象.
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.


Combobox上的Java教程 [ ^ ]

检查该教程.

看起来不错:
-使用要显示的String (id?)创建一个Object并在其中显示相应的值.
确保对象中有一个toString()返回应显示的文本.

-将这些ObjectsList 添加到ComboBox.
它会自动显示字符串,您可以轻松找到所显示字符串的相应值(请参阅Richards答案).


看到这个演示.组合框oComboBox 中填充了对象ComboValue中的值. ComboValue拥有方法toString(),该方法为ComboBox提供要显示的字符串.
CustomComboBoxListener 被称为ActionListener.他用ActionCommand检查Component是否是那个组件,然后提取值(可以单行完成,这里多行显示会发生的情况). ActionListener仅用于getSelectedItem(),甚至不关心索引号.因此,不能存在由于错误计数而获得错误值的错误朋友".

我希望这很清楚-虽然很简单.

对于那些正在寻找用于复制作业的简单代码的人:
乐于向您的老师讲解代码;-)


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.


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 ;-)


/**
 * 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();
		}
	}
}


这篇关于java combobox属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆