gpt4 book ai didi

java - 如何刷新 JComboBox 数据?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:31:09 25 4
gpt4 key购买 nike

我无法刷新 JComboBox 中的数据。

有一个按钮“Create”,它有 ActionListener,它将项目添加到 JComboBox

但是更改没有反射(reflect)在 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...

最佳答案

  • 您必须将 ComboBoxModel 添加到 JComboBox

  • 你可以添加/删除/修改值,

  • API 中实现的事件刷新您的 View (JComboBox),无需额外的代码行

  • 所有更新必须在 Event Dispatch Thread 上完成

编辑

也许我没看懂你的问题,如果你想将 JComboBox 添加到已经可见的 GUI,那么你必须调用(作为最后一行代码并且一个容器只成功一次)

myContainer.revalidate() // for JFrame up to Java7 is there only validate()
myContainer.repaint()

(抱歉@timaschew)

关于java - 如何刷新 JComboBox 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10904639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com