我无法刷新JComboBox
内的数据。
有一个“Create
”按钮,其中包含ActionListener
,可将该项添加到JComboBox
。
但是这些更改没有反映在GUI中:我仍然没有看到新添加的项目。
repaint()
没有帮助。
更新:这是一个(几乎)完整的GUI代码:
public class Main extends JFrame implements ActionListener
{
static Connection conn;
static PreparedStatement ps = null;
static ResultSet res;
static Statement sta;
private final static int ITERATION_NUMBER = 1000;
public void GUI () throws SQLException {
setBounds(0, 0, 320, 240);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
close(ps);
close(res);
close(conn);
System.exit(0);
}
});
setMinimumSize(new Dimension(320, 240));
setResizable(false);
this.setTitle("Accounts");
JPanel panel = new JPanel();
GridLayout2 GL = new GridLayout2(4,3);
GL.setHgap(10);
panel.setLayout(GL);
Font font = new Font("Serif", Font.BOLD, 20);
Font font2 = new Font("Courier New", Font.BOLD, 16);
JLabel label1 = new JLabel("Username");
JLabel label2 = new JLabel("Password");
JLabel label3 = new JLabel("Controls");
label1.setFont(font2);
label2.setFont(font2);
label3.setFont(font2);
final JTextField username = new JTextField();
final JTextField password1 = new JPasswordField();
final JTextField password2 = new JPasswordField();
final JComboBox userBox1 = new JComboBox();
final JComboBox userBox2 = new JComboBox();
JButton create = new JButton("CREATE");
create.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
try {
createUser(conn, username.getText(), password1.getText());
userBox1.addItem(username.getText());
userBox2.addItem(username.getText());
} catch (NoSuchAlgorithmException
| UnsupportedEncodingException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
userBox1.removeAllItems();
userBox2.removeAllItems();
res = (ResultSet) sta.executeQuery("SELECT LOGIN FROM ACCOUNTS");
String temp;
for (int i=0; res.next(); i++) {
temp = (String)res.getString("LOGIN");
userBox1.addItem(temp);
userBox2.addItem(temp);
}
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(username);
panel.add(password1);
panel.add(create);
panel.add(userBox1);
panel.add(password2);
panel.add(modify);
panel.add(userBox2);
panel.add(new JLabel(""));
panel.add(delete);
add(panel);
setVisible(true);
}
解决方案:添加password1.setText(“”);刚刚“createUser”解决了问题!这很奇怪,也许它以某种方式刷新了GUI ......
答案 0 :(得分:7)
您必须将ComboBoxModel
添加到JComboBox
,
您可以add
/ remove
/ modify
加上值
在API中实施的事件刷新您的视图(JComboBox
),而不再使用代码行
所有更新必须在Event Dispatch Thread
编辑
也许我想念您的问题,如果您想将JComboBox添加到已经可见的GUI,那么您必须调用(作为最后一个代码行,并且只对一个Container成功一次)
myContainer.revalidate() // for JFrame up to Java7 is there only validate()
myContainer.repaint()
(对不起@timaschew)
答案 1 :(得分:3)
static class TestFrame extends JFrame implements ActionListener
{
private JComboBox testBox = new JComboBox();
private JButton testButton = new JButton();
int c = 0;
public TestFrame()
{
testBox = new JComboBox();
testButton = new JButton("Click Me!");
testButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(2,1));
panel.add(testBox);
panel.add(testButton);
this.add(panel);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
testBox.addItem("test" + c++);
}
}
此测试用例有效,您确定已将侦听器添加到单击的组件中吗?
答案 2 :(得分:0)
据我所知,ComboBox不能用“.removeAllItems()”,“。removeAll()”或“.repaint()”刷新。
如果要刷新它,则必须每次都创建一个新的ComboBox,然后向其中添加项目。以上面给出的代码为例:
userBox1 = new JComboBox(); // to replace userBox1.removeAllItems();
userBox2 = new JComboBox(); // to replace userBox2.removeAllItems();
res = (ResultSet) sta.executeQuery("SELECT LOGIN FROM ACCOUNTS");
String temp;
for (int i=0; res.next(); i++) {
temp = (String)res.getString("LOGIN");
userBox1.addItem(temp);
userBox2.addItem(temp);
}
我有类似的问题,但我这样解决了。
答案 3 :(得分:-3)
此代码是用于在按钮单击事件中刷新jframeform的按钮单击事件。
new room().show(); //room() is a jframeform
new room().setVisible(false);