如何从NetBeans的jList中选择项目名称?

时间:2019-01-02 13:23:36

标签: java mysql database netbeans jlist

我一直在尝试从list中的mysql检索数据,并且工作正常,但是问题是我无法选择name中的list。 我想选择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);
}


}

Here is the image of my list ..Show List retreives the data from mysql ..and proceed button is used to take the selected name for modification

这里是“显示列表按钮”的代码:-

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类:-

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){

     fillList();

}

2 个答案:

答案 0 :(得分:0)

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());
}

}

答案 1 :(得分:0)

好吧,据我从您发布的代码中得知,fillList的基本问题是while循环。您为每个循环创建一个新的JList。这将使您在jList1方法中拥有的proceed引用无效(或者我应该说 dirty )。您不应该每次都创建一个新的JList。只需为每个循环保留相同的列表。甚至不要在循环外更改它。保持不变并每次更新其模型。

要更新模型,您至少具有以下两个选项:

  1. 每次创建一个新模型,对其进行更新(即在模型中添加所需的所有元素)并调用jList1.setModel(your_created_model)
  2. JList的模型一次设置为DefaultListModel(例如,在构建时),然后在每个fillList方法调用中:
    1. 从列表本身获取模型(通过getModel())。您知道它是DefaultListModel,因此可以安全地将其转换为一个。
    2. 从模型中删除所有元素(针对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);
    }
}

它利用第二个选项(每次重新填充模型)。