Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    [Java] JCOmbobox con ArrayList

    per riempire una jcombobox facevo così:
    codice:
    package operazioni_database;
    
    import java.sql.*;
    import libreria.*;
    
    public class DoFillCBAuthor {
    
        public static void fillAuthor() throws SQLException, ClassNotFoundException {
            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;
            conn = DoConnection.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * FROM author");
            while (rs.next()) {
                FramePrincipale.getComboAuthor().addItem(new Author(rs.getInt("author_id"), rs.getString("author_name")));
            }
            rs.close();
            stmt.close();
        }
    }
    ho pensato che nn fosse la maniera migliore e ho provato con ArrayList:
    codice:
    package operazioni_database;
    
    import java.sql.*;
    import java.util.ArrayList;
    
    public class FillAuthor {
    
        public static ArrayList fillAuthor() throws SQLException, ClassNotFoundException {
            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;
            ArrayList list = new ArrayList();
            conn = DoConnection.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * FROM author ORDER BY author_id");
            while (rs.next()) {
                list.add(new Author(rs.getInt("author_id"), rs.getString("author_name")));
            }
            rs.close();
            stmt.close();
            return list;
        }
    }
    nel jframe:
    codice:
            ArrayList list = FillAuthor.fillAuthor();
            comboUpAuthor.addItem(list);
    il problema è che nn va mai a capo.
    mi fa vedere tutti gli autori su una riga invece di "spezzarli".
    come posso separare i vari risultati??

    ArrayList va bene per riempire la jcombobox o c'è qualcos'altro di meglio??

  2. #2
    Ciao fermat,
    Il procedimento che effettui e formalmente corretto, solo che per poter passare al jComboBox una lista di elementi devi usare il costruttore apposito:
    codice:
    ArrayList list = FillAuthor.fillAuthor();
            comboUpAuthor.addItem(list.toArray());
    Ciao

  3. #3

    Re: [Java] JCOmbobox con ArrayList

    Originariamente inviato da fermat
    ho pensato che nn fosse la maniera migliore e ho provato con ArrayList:
    nel jframe:
    codice:
            ArrayList list = FillAuthor.fillAuthor();
            comboUpAuthor.addItem(list);
    il problema è che nn va mai a capo.
    mi fa vedere tutti gli autori su una riga invece di "spezzarli".
    come posso separare i vari risultati??
    sostituisci quella istruzione con questa:
    codice:
    for(Object a : list) 
         comboUpAuthor.addItem(a);

  4. #4
    grazie a entrambi!!

  5. #5
    avrei un altro problema:
    codice:
    public class Book {
    
        private int book_id;
        private String name;
        private String author;
        private String editor;
        private double price;
        private String isbn;
        private String note;
    
        public Book(int book_id, String name) {
            this.book_id = book_id;
            this.name = name;
        }
    
        public Book(int book_id, String name, String author, String editor, double price, String isbn, String note) {
            this.book_id = book_id;
            this.name = name;
            this.author = author;
            this.editor = editor;
            this.price = price;
            this.isbn = isbn;
            this.note = note;
        }
    
        public int getBookId() {
            return book_id;
        }
    
        public String getName() {
            return name;
        }
    
        public String getAuthor() {
            return author;
        }
    
        public String getEditor() {
            return editor;
        }
    
        public double getPrice() {
            return price;
        }
    
        public String getIsbn() {
            return isbn;
        }
    
        public String getNote() {
            return note;
        }
    }
    metodo per riempir:
    codice:
    public class DoFillBookId {
    
        public static ArrayList fillBook() throws SQLException, ClassNotFoundException {
            ArrayList list = new ArrayList();
            Connection conn = DoConnection.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM book");
            while (rs.next()) {
                list.add(new Book(rs.getInt("book_id"), rs.getString("name")));
            }
            rs.close();
            stmt.close();
            return list;
        }
    }
    poi nel frame:
    codice:
        private void fillBookId() throws SQLException, ClassNotFoundException {
            comboBookId.removeAllItems();
            comboBookId.addItem("*");
            ArrayList list = DoFillBookId.fillBook();
            for (Object obj : list) {
                comboBookId.addItem(obj);
            }
        }
    ottengo un risultato strano:
    operazioni_database.Book@ec0a9f9
    ......

    mi pare di aver fatto tutto come negli altri due casi.

  6. #6
    Originariamente inviato da fermat
    ottengo un risultato strano:
    operazioni_database.Book@ec0a9f9
    Perchè nella classe Book non hai ridefinito il metodo toString().

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.