簡體   English   中英

如何從netbeans的jList中選擇項目名稱?

[英]How to select item name from jList in netbeans?

我一直在嘗試從list mysql 檢索數據,這工作正常,但問題是我無法選擇listname ..我想選擇name (不是索引或值),以便我可以完成我的 where 子句

這是我的代碼:-

private void proceed(){

    PreparedStatement stmt = null;
    Connection conn = null;
    ResultSet rs=null;

    String i = jList1.getSelectedValue();




    try    {

     Class.forName("java.sql.DriverManager");

     conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");

      stmt = conn.prepareStatement("select * from hotelbookings where GuestName = '"+i+"'");

    rs = stmt.executeQuery();

    if (rs.next()){

    String BN = rs.getString("BookNo");
    String GN = rs.getString("GuestName");
    String AD = rs.getString("Address");
    String NOD = rs.getString("No_of_Days");
    String PN = rs.getString("PhoneNo");
    String ID = rs.getString("ID_Proof");
    String CN = rs.getString("Country");
    String ARD = rs.getString("Arival_Date");
    String DRD = rs.getString("Departure_Date");

    NewJFrame1_1 second = new NewJFrame1_1(BN,GN,AD,NOD,PN,ID,CN,ARD,DRD);
    second.setVisible(true);

    }

} catch(Exception e){
 JOptionPane.showMessageDialog(null, e);
}


}

這是我的列表的圖像 ..Show List 從 mysql 檢索數據 ..and 繼續按鈕用於獲取選定的名稱進行修改

這是顯示列表按鈕的代碼:-

private void fillList(){

        PreparedStatement stmt = null;
        Connection conn = null;


         try    {
         Class.forName("java.sql.DriverManager");

         conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");

          stmt = conn.prepareStatement("select GuestName from hotelbookings");

         stmt.executeQuery();

             ResultSet rs = stmt.getResultSet();
             int i =0;
             DefaultListModel info = new DefaultListModel();

             while (rs.next()){
                 String[] data = new String[100];
                 data[i] = rs.getString("GuestName");
                 jList1.setModel(info);
                 info.addElement(data[i]);
                 i = i + 1;
                 jList1 = new JList(info);
             }

}
      catch(Exception e){

                JOptionPane.showMessageDialog (this, e.getMessage());
}




    }

fillList 類:-

私有無效 jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

     fillList();

}

private void fillList(){

PreparedStatement stmt = null;
Connection conn = null;

try    {
    Class.forName("java.sql.DriverManager");
    conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");
    stmt = conn.prepareStatement("select GuestName from hotelbookings");
    stmt.executeQuery();
    ResultSet rs = stmt.getResultSet();
    int i =0;
    DefaultListModel info = new DefaultListModel();

    while (rs.next()){
        String[] data = new String[100];
        data[i] = rs.getString("GuestName");
        info.addElement(data[i]);
        i = i + 1;
    }
    jList1 = new JList(info);
} catch(Exception e){
    JOptionPane.showMessageDialog (this, e.getMessage());
}

}

好吧,據我從您發布的代碼中可以看出, fillList的基本問題是 while 循環。 您為每個循環創建一個新的JList 這將使您在proceed方法中擁有的jList1引用無效(或者我應該說)。 您不應該每次都創建一個新的JList 只需為每個循環保留相同的列表。 甚至不要在循環外更改它。 只需保持不變並每次更新其模型即可。

要更新模型,您至少有以下兩個選項:

  1. 每次創建一個新模型,更新它(即添加你想要的所有元素)並調用jList1.setModel(your_created_model)
  2. JList的模型設置為DefaultListModel一次(例如在構造時),然后在每個fillList方法中調用:
    1. 從列表本身獲取模型(通過getModel() )。 您知道它是一個DefaultListModel因此您可以安全地將其轉換為一個。
    2. 從模型中刪除所有元素(vis removeAllElements() )。
    3. ResultSet中的所有新元素添加到模型中。

以下面的代碼為例:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class Main {
    public static void fillList(final JList<String> list) {
        final DefaultListModel<String> model = (DefaultListModel<String>) list.getModel(); //Step 1: Get the model and cast it to DefaultListModel.
        model.removeAllElements(); //Step 2: Remove all elements from the model.
        final double rand = Math.random();
        for (int i = 0; i < 10; ++i)
            model.addElement("name " + Math.round((1 + i) * rand * 100)); //Step 3: Fill the model with new elements.
    }

    public static void proceed(final JList<String> list) {
        JOptionPane.showMessageDialog(list, "You selected: \"" + list.getSelectedValue() + "\"!");
        //Here you have the selected value approprietly to do whatever you like with...
    }

    public static void main(final String[] args) {
        final JList<String> names = new JList<>(new DefaultListModel<>()); //Do not forget to set the ListModel of the list to DefaultListModel!

        //List initialization:
        names.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        fillList(names);

        //Creating the buttons:
        final JButton fill = new JButton("Fill"),
                      proceed = new JButton("Proceed");

        //Adding listeners:
        fill.addActionListener(e -> fillList(names));
        proceed.addActionListener(e -> proceed(names));

        //Creating buttons' panel:
        final JPanel buttons = new JPanel();
        buttons.add(fill);
        buttons.add(proceed);

        //Scroll pane of list:
        final JScrollPane scroll = new JScrollPane(names);
        scroll.setPreferredSize(new Dimension(400, 200));

        //Main panel:
        final JPanel contents = new JPanel(new BorderLayout());
        contents.add(scroll, BorderLayout.CENTER);
        contents.add(buttons, BorderLayout.PAGE_END);

        //Frame:
        final JFrame frame = new JFrame("List of MesureTypes.");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(contents);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

它使用第二個選項(每次重新填充模型)。

暫無
暫無

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

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