简体   繁体   English

单击jComboBox中的项目时,如何在jTextField中设置值?

[英]How do i set the value in jTextField when i click an Item in jComboBox?

How do i set the value in jTextField when i click an Item in jComboBox? 单击jComboBox中的项目时,如何在jTextField中设置值? Like when i select Platinum in jComboBox the jTextField will display its value like 15% or Gold and set the jTextField to 10% 就像当我在jComboBox中选择Platinum时,jTextField将显示其值,例如15%或Gold,并将jTextField设置为10%

membox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {

    if(e.getStateChange() == ItemEvent.SELECTED) {

        jTextField6.setText((String) membox.getSelectedItem());
    }
}

}); });

im stuck here it only displays what i clicked in the jComboBox any help will be so much appreciated thank you in advance 我卡在这里,它只显示我在jComboBox中单击的内容,将不胜感激,谢谢您提前

Create a class 建立课程

public class ItemClass {
private String name;
private String value;

public ItemClass(String name1, String value1) {
    name = name1;
    value = value1; 
}

@Override
public String toString()
{
    return name;
}

public String getValue() {
    return value;
}
}

and add this code in yours... 并在您的代码中添加此代码...

    ItemClass oro = new ItemClass("gold","10%");
    ItemClass platino = new ItemClass("platinum","15%");
    JComboBox jc = new JComboBox();
    jc = membox;
    jc.addItem(oro);
    jc.addItem(platino);

membox.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent e) {

    if(e.getStateChange() == ItemEvent.SELECTED) {
        Object obj=(Object) membox.getSelectedItem();
        ItemClass itemclass=(ItemClass)obj;
        String value = itemclass.getValue();
        jTextField6.setText(value);
    }
}

}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JTextField的2个JComboBox中使用值? - How do I use the value in 2 JComboBox's in JTextField? 我如何从JTextField到JComboBox获得价值? - How can I get value from JTextField to JComboBox? 通过单击可编辑JCombobox的弹出菜单自动选择项目后如何修改JTextField的值 - How to modify the value of JTextField after automatic item selection from popup menu by mouse click of editable JCombobox 如何在Java中从JTextField设置Bean属性值? - How do i set a Bean property value from JTextField in Java? 如果没有选择,如何在JTextfield中显示JComboBox的选择项? - How to show JComboBox's selected item in JTextfield when it selected otherwise not? 如何为 JTextField 设置 label? - How do I set the label for JTextField? 当USER选择JComboBox中的项目时,如何创建一个触发器 - How do I make a listener that fires when the USER selects an item in a JComboBox 如何获得从JComboBox中选择的项目的索引? - How do I get the index of the item selected from a JComboBox? 当我单击它时,如何使 JMenuItem 打开一个 JTextField? - How can I make a JMenuItem open a JTextField when I click it? 如何从通过JLabel单击动态创建的Java中的JTextField中获取值 - How do i get the value from JTextField in java that was created dynamically through a JLabel click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM