【问题标题】:How to select item name from jList in netbeans?如何从netbeans中的jList中选择项目名称?
【发布时间】:2019-01-02 13:23:36
【问题描述】:

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

}

【问题讨论】:

    标签: java mysql database netbeans jlist


    【解决方案1】:
    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());
    }
    
    }
    

    【讨论】:

    • 我已经通过从 mysql 检索数据创建了列表 .. 但我无法从列表中选择名称 ... 提前感谢 .. 任何进一步的帮助表示赞赏。 .
    • 首先在应用程序的用户界面中选择 jList1 中的行(名称)。然后按下按钮调用proceed()方法
    • yahh ...我做了完全一样的..但没有帮助...我附上了一张图片..看到了
    • 那我如何使用变量 i 来执行该语句?
    • 以上给出的答案用于从mysql填充Jlist1
    【解决方案2】:

    嗯,就我从您发布的代码中可以看出,fillList 的基本问题是 while 循环。您为每个循环创建一个新的JList。这将使您在proceed 方法中拥有的jList1 引用无效(或者我应该说脏)。您不应该每次都创建一个新的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);
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-01
      • 2013-10-05
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 2011-09-08
      • 2016-11-25
      相关资源
      最近更新 更多