簡體   English   中英

如何使用Netbeans 8.2中的JComboBox下拉列表創建更新按鈕?

[英]How do I create an update button using JComboBox dropdown list in netbeans 8.2?

我想為下拉列表創建一個更新按鈕,以從netbeans中的數據庫獲取數據。 在下面嘗試了此方法,但無法使其正常工作。 有什么建議么? 非常感謝。

public class ViewProduct extends javax.swing.JFrame {
 ResultSet rs;
 PreparedStatement pst;
 Connection conn;    
 final void FillList(){
 try{   
        //establish connection to table
        String url = "jdbc:derby://localhost:1527/ProductInformation";
        String username = "admin1";
        String password = "admin1";

        Connection conn = DriverManager.getConnection(url,username,password);
        Statement stat = conn.createStatement();
        // get data from table in sql database
        String Query = "SELECT * FROM VIEWPRODUCT";
        ResultSet rs = stat.executeQuery(Query);
        //
        DefaultListModel DLM = new DefaultListModel();

        while(rs.next()){

        JList list = new JList();
        JComboBox ProductID_dropdown = new JComboBox();
        DefaultComboBoxModel listModel = new DefaultComboBoxModel();

        list.setModel(listModel);
        ProductID_dropdown.setModel(listModel);
        }

}catch(SQLException ex){
    JOptionPane.showMessageDialog(null, ex.toString());
}

您的代碼中有些不正確或缺少的東西。 例如,您沒有GUI。

而且由於我不知道您的項目或數據庫的確切結構,我不得不即興創作,並為您提供一種偽代碼方法。 您將不得不更改和配置我在此處顯示的幾項內容。

首先,在您的方法之外創建一個JComboBox並將其添加到JPanel之類的容器中:

JPanel pane = new JPanel();
JComboBox productID_dropdown = new JComboBox();
JButton btn_updateViewProducts = new JButton("Update Results");

在此處添加了用於更新結果的按鈕,其中包含一個ActionListener ,當有人單擊該按鈕時可以“監聽”。 通常,您需要檢索一個ResultSet並將其內容放入變量中,如以下示例所示

// ActionListener for the button
btn_updateViewProducts.add(new ActionListener{
    @Override
    // When the button is clicked...
    public void actionPerformed(ActionEvent arg0) {
        // ... get the results out of your database (here it's an example database! 
        // Configure for your own one)
        ResultSet rs = stat.executeQuery(Query);
        while(rs.next()){
            // "de-construct" every result of the ResultList into variables
            String query = "select COF_NAME, SUP_ID, PRICE, " + "SALES, TOTAL " + "from " + dbName + ".COFFEES";
            String coffeeName = rs.getString("COF_NAME");
            int supplierID = rs.getInt("SUP_ID");
            float price = rs.getFloat("PRICE");
            int sales = rs.getInt("SALES");
            int total = rs.getInt("TOTAL");
            System.out.println(coffeeName + "\t" + supplierID + "\t" + price + "\t" + sales + "\t" + total);

            // Put items into JComboBox
            productID_dropdown.addItem(coffeeName);
        }        
    }
});

// Add the now filled JComboBox to the pane
pane.add(productID_dropdown);

您應該考慮的其他問題:

  • GUI丟失
  • 方法FillList是最終的,這似乎很不正常,這背后的原因是什么? final在方法中意味着它不能被子類覆蓋,這在這里需要嗎?
  • FillList是一種方法,由於編碼約定,應該在開頭寫一個小寫字母, ProductID_dropdown (編碼約定)也是如此
  • 在大多數情況下,使用rs類的短變量作為ResultSet是完全可以的,但是,如果您的項目變大,則很難記住所有這些變量。 更好:讓他們告訴你他們是什么。 代替rs > resultSetViewProduct或類似的東西。
  • 從不使用PreparedStatement pst

我希望這有幫助。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM