加上在Java组合框项目运行 [英] add items runtime on combobox in java

查看:117
本文介绍了加上在Java组合框项目运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个的组合框在秋千的,我想在另一个函数上添加项目。但问题是,数据项都没有显示调用该功能后:

 公共无效为addItems()
{
    combo.addItem();
    //我想在这里补充项目正在调用该功能时,
    //但这些项目不调用此函数后显示
    //我打电话是按钮点击此功能
}


解决方案

在此 SSCCE 对我的作品>

 进口java.awt.BorderLayout中;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;进口javax.swing.JButton中;
进口javax.swing.JComboBox中;
进口javax.swing.JFrame中;
进口javax.swing.SwingUtilities中;
进口javax.swing.UnsupportedLookAndFeelException;公共类TestComboBox {    私人诠释计数= 0;    保护无效initUI(){
        最终的JFrame帧=新的JFrame(TestComboBox.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        最终的JComboBox组合框=新JComboBox中(新的对象[] {东西,东西,哔});
        JButton的增加=的新的JButton(添加项目);
        add.addActionListener(新的ActionListener(){            @覆盖
            公共无效的actionPerformed(ActionEvent的五){
                comboBox.addItem(本期特价货品+计数++);
            }
        });
        frame.add(组合框);
        frame.add(添加,BorderLayout.SOUTH);
        frame.pack();
        frame.setBounds(50,50,300,frame.getHeight());
        frame.setVisible(真);
    }    公共静态无效的主要(字串[] args)抛出了ClassNotFoundException,InstantiationException,IllegalAccessException,
            UnsupportedLookAndFeelException {
        SwingUtilities.invokeLater(Runnable的新(){            @覆盖
            公共无效的run(){
                新TestComboBox()initUI()。
            }
        });
    }
}

I have created a comboBox in Swing and I want to add items on it in another function. But the problem is that items are not displaying after calling that function:

public void addItems()
{
    combo.addItem("");
    // i want to add items here when this function is being called
    // but those items are not displaying after calling this function
    // i m calling this function on button click
}

解决方案

Works for me in this SSCCE:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UnsupportedLookAndFeelException;

public class TestComboBox {

    private int count = 0;

    protected void initUI() {
        final JFrame frame = new JFrame(TestComboBox.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JComboBox comboBox = new JComboBox(new Object[] { "Something", "Stuff", "Beep" });
        JButton add = new JButton("Add item");
        add.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                comboBox.addItem("Item-" + count++);
            }
        });
        frame.add(comboBox);
        frame.add(add, BorderLayout.SOUTH);
        frame.pack();
        frame.setBounds(50, 50, 300, frame.getHeight());
        frame.setVisible(true);
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestComboBox().initUI();
            }
        });
    }
}

这篇关于加上在Java组合框项目运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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