简体   繁体   中英

How to show a arraylist of string on jlabel?

Hi I have an arraylist of strings, I want to show the content of the arraylist on JLabel separated by a space or comma. But it shows me only one String, the last one.

 public void ShowMovie(int idMovie) throws SQLException, IOException {
        int ID = idMovie;
        String IDMOVIE = Integer.toString(ID);
        IDMovieLabel.setText(IDMOVIE);

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Cover.class.getName()).log(Level.SEVERE, null, ex);
        }
        con = DriverManager.getConnection("jdbc:mysql://localhost/whichmovie", "Asis", "dekrayat24");

        String sql = "SELECT Title,Year,Country,recomendacion,Cover,Rating,NameDirec,Name FROM movie "
                + "Inner join direction on (movie.idMovie=direction.idMovie5)"
                + "Inner join director on (direction.idDirector=director.idDirector)"
                + "Inner join cast on (movie.idMovie=cast.idMovie4)"
                + "Inner join actor on (cast.idActor=actor.idActor)"
                + "where idMovie= '" + ID + "'";

        st = con.prepareStatement(sql);
        rs = st.executeQuery(sql);

        while (rs.next()) {

            String titulo = rs.getString(1);
            int añoInt = rs.getInt(2);
            String año = Integer.toString(añoInt);

            byte[] imagedataCover = rs.getBytes("Country");
            byte[] imagedataCover1 = rs.getBytes("Cover");
            format = new ImageIcon(imagedataCover);
            format2 = new ImageIcon(imagedataCover1);

            TituloLabel.setText(titulo);
            AñoLabel.setText(año);
            CountryLabel.setIcon(format);
            DirectorLabel.setText(rs.getString(7));

            int Recomend = rs.getInt(4);
            String Recom = Integer.toString(Recomend);

            RecommendLabel.setText(Recom);

            int Rating = rs.getInt(6);
            String Rat = Integer.toString(Rating);

            RatingLabel.setText(Rat);
            starRater1.setSelection(Rating);
            starRater1.setEnabled(false);

            Image imgEscalada = format2.getImage().getScaledInstance(CoverLabel.getWidth(),
                    CoverLabel.getHeight(), Image.SCALE_SMOOTH);
            Icon iconoEscalado = new ImageIcon(imgEscalada);
            CoverLabel.setIcon(iconoEscalado);

            ArrayList<String> actors = new ArrayList<>();
            actors.add(rs.getString(8));
            System.out.println(actors);// Here i can see i get 9 actors.

            StringBuilder sb = new StringBuilder();

            boolean first = true;
            for (String s : actors) {
                if (!first) {
                    sb.append(' ');
                }
                sb.append(s);
                first = false;
            }

            CastLabel1.setText(sb.toString());

        }
        rs.close();
        st.close();
        con.close();

    }

Any help ?

Edit:unfortunately no solution has helped me, maybe I'm doing something wrong in the method, I post the full method.

The problem is you are resetting the label for each step in the for loop, and not creating a cumulative result. See below:

StringBuilder buf = new StringBuilder();
for(int i = 0; i < actors.size(); i++){
    buf.append(actors.get(i));
    if(i < actors.size() -1){
        buf.append(" ");
    }
}
CastLabel1.setText(buf.toString())
String text = ""; 
for(int i = 0; i < actors.size(); i++){
   text = text + actors.get(i);
   if(i < actors.size() - 2){
        text = text + ", ";   
   }
} 
CastLabel1.setText(text);

You should build the string you want to show first then set it to the text of the label:

StringBuilder sb = new StringBuilder();

boolean first = true;
for (String s : actors) {
    if (!first)
        sb.append(' ');
    sb.append(s);
    first = false;
}

CastLabel1.setText(sb.toString());

What you're currently doing is changing the entire label text during each iteration, so the final text is that of the last element in the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM