0

I have done the code for Movie Ticket Booking and now I have stopped at JList.

How to add data in JList?

Here is a code...

    JList listTime = new JList();
    listTime.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    listTime.setFont(new Font("Times New Roman", Font.PLAIN, 25));
    listTime.setBounds(329, 311, 200, 50);
    frame.getContentPane().add(listTime);
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/suhel","root","Suhel786");
        Statement stmt = con.createStatement();
        String sql="select distinct time from tbmovie where movie='"+select_mov+"'";
        ResultSet rs = stmt.executeQuery(sql);
        while(rs.next())
        {
            listTime.add(rs.getString("time"));//here is the Error
        }

    }
    catch(Exception e1){
        System.out.print(e1);
    }

I have retrieved data from database - only need is to add data in list.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 2
    See [How to Use Lists](https://docs.oracle.com/javase/tutorial/uiswing/components/list.html). Voting to close. – Andrew Thompson Jun 30 '18 at 09:18
  • 1
    It doesn’t look like you’ve done any great research effort before posting? Also the comment in your code says “here is the Error”, but you have forgot to tell us what the error is? Your question seems to be a clear duplicate of [Adding elements to a JList](https://stackoverflow.com/questions/16214480/adding-elements-to-a-jlist), q.v. – Ole V.V. Jun 30 '18 at 09:31

1 Answers1

1

First, initialize the JList with a ListModel, e.g. a DefaultListModel and then modify the ListModel with its methods. Alternatively, load the items from a database into an array, and create a JList afterwards with a constructor that accepts an array, if possible.

am9417
  • 994
  • 8
  • 18