问题描述
我有一个组合框,如您在从表中获取值的代码中所见.因此,单击确定"后,表的值将更改.如何在不关闭和打开jframe的情况下看到这些新值到compobox?我今天对java.awt.EventQueue.invokeLater和其他人员进行了很多研究,但我无法使其正常工作,因为我对Java和常规编程是陌生的.所以这是代码:
I have a combo box as you can see in the code that takes the values from a table.So after I click ok the values of the table change.How can I see these new values to the compobox without to close and open the jframe ?I made a lot of study today about java.awt.EventQueue.invokeLater and othe staff but I can not make it work, I am new to java and to programing general.So here is the code:
public class Compo extends JFrame implements ActionListener
{//start of class Compo
//start of variables
private JComboBox<String> CompoBox;
private String array[];
private JButton okButton;
private JPanel panel;
//end of variables
public Compo ()
{//start of Compo method
super("Example");
panel=new JPanel(null);
//table = new String[3];
array= new String[3];
array[0]="alpha";
array[1]="beta";
array[2]="charlie";
CompoBox= new JComboBox<>(array);
CompoBox.setBounds(50, 70, 100, 20);
panel.add(CompoBox);
okButton=new JButton("ok");
okButton.setBounds(50, 120, 70, 30);
okButton.setActionCommand("ok");
okButton.addActionListener(this);
panel.add(okButton);
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 200);
setVisible(true);
}//end of compo method
@Override
public void actionPerformed(ActionEvent event)
{//start of actionperformed
String testString=event.getActionCommand();
if (testString.equals("ok"))
{//start of if
for (int i = 0; i < array.length; i++)
{
String sample= array[i];
array[i]=sample+"aa";
}
}//end of if
}//end of aciton performed
}//end of class Compo
推荐答案
这应该是您正在寻找的答案,希望您会接受:
this should be the answer you are looking for, hope you'll accept it:
if (testString.equals("ok")) {
CompoBox.removeAllItems();
for (int i = 0; i < array.length; i++) {
String sample = array[i];
CompoBox.addItem(sample + "aa");
}
}
顺便说一句.通用comqo-pox的操作如下:
btw. the generic comqo-pox is done like this:
CompoBox = new JComboBox<String>(array);
这篇关于刷新JComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!