gpt4 book ai didi

java - 当您键入时从 jcombobox 获取文本

转载 作者:行者123 更新时间:2023-11-30 01:03:54 25 4
gpt4 key购买 nike

目前我有一个解决这个问题的方法,它是使用一个文本字段和一个组合框,但这非常凌乱,希望删除文本字段,因为这是将数据输入到 mysql 数据库并检索它,所以我需要能够将结果添加到组合框,因为它是文本字段

 private void jTextField15KeyReleased(java.awt.event.KeyEvent evt) {                                         
String ThePub = jTextField15.getText();
int publengh = ThePub.length();
if (publengh > 2) {
jTextField15.setVisible(false);
fillpub(ThePub);
}
public void fillpub(String pub) {
Connection con;
ResultSet rs;
PreparedStatement pst;
String thedata;
try {
String cs = "jdbc:mysql://localhost:3306/booksalvation4";
String user = "root";
String password = "letmein";
pub = "'" + pub + "%'";
con = DriverManager.getConnection(cs, user, password);
String query = "select * from publisher where name like" + pub;
pst = con.prepareStatement(query);
rs = pst.executeQuery();
while (rs.next()) {
thedata = rs.getString(2);
jComboBox11.addItem(thedata);
}
} catch (SQLException ex) {
Logger.getLogger(mainJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

最佳答案

不要使用 KeyListener。

相反,您应该使用DocumentListener。您可以将 DocumentListener 添加到用作 JComboBox 编辑器的文本字段的文档。

请参阅 JComboBox 的 getEditor() 方法。一旦你有了ComboBoxEditor,你就可以获得编辑器组件,默认情况下它是一个JTextField。然后将 DocumentListener 添加到文本字段。

阅读 Swing 教程中关于 How to Write a Document Listener 的部分了解更多信息。

关于java - 当您键入时从 jcombobox 获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19851104/

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