java 数据库刷新_java – 如何刷新JComboBox数据?

"本文讲述了开发者在Java Swing应用中遇到的问题,即如何刷新JComboBox中的数据。通过ActionListener实现的按钮“CREATE”添加新项目后,GUI并未实时更新。最终发现,一个简单的`password1.setText("")`操作解决了这一问题,揭示了可能的UI刷新机制。"
摘要由CSDN通过智能技术生成

我无法刷新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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值