Results 1 to 5 of 5
  1. #1
    TomTom is offline Member
    Join Date
    Dec 2015
    Posts
    3
    Rep Power
    0

    Default Resultset and JTable

    Hello,

    I have a Resultset named "resultset_result" in an "Arithmetic" class and a JTable named "table_result" in a "Result" class.
    Can you show me a way to insert the values of my Resultset from the "Arithmetic" class into the Jtable from the "Result" class ?

    Thanks.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Resultset and JTable

    There is a little more work involved than "insert this ResultSet into that JTable". You can go quick and dirty for smaller ResultSets:
    • Create a class that extends AbstractTableModel (or implements TableModel, but AbstractTableModel is preferred since it contains listener code)
    • Create a value object that holds each row of data
    • Loop over the rows and store value objects in the TableModel implementation. Most of the times, you will have a List<MyValueObject> where the index corresponds to the row index. The column index must be mapped to some property of the value object)
    • Implement the methods in AbstractTableModel (at least getRowCount(), getColumnCount(), getValueAt())
    • Hand the TableModel class to the JTable
    Last edited by SurfMan; 12-14-2015 at 02:39 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    TomTom is offline Member
    Join Date
    Dec 2015
    Posts
    3
    Rep Power
    0

    Default Re: Resultset and JTable

    Thanks for your answer.
    Can you explain how to create a value object that hold each row of data and insert it in the JTable ?

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Resultset and JTable

    A value object is nothing more than a (preferably small) object that holds values to display. For TableModels, I usually create one with only getters, where the data is set using the constructor. Let's create a value object for a Book:
    Java Code:
    class Book {
        private String isbn;
        private String title;
        private String author;
    
        public Book(String isbn, String title, String author) {
            this.isbn = isbn;
            this.title = title;
            this.author = author;
        }
    
        public String getAuthor() {
            return author;
        }
    
        public String getIsbn() {
            return isbn;
        }
    
        public String getTitle() {
            return title;
        }
    }
    Now for the TableModel, you need to tell it how many rows and columns there are, and what value to place in what cell. The absolute minimum TableModel looks like this:

    Java Code:
    class BookTableModel extends AbstractTableModel {
        private List<Book> books;
        private String[] columnNames = {"Title", "Author", "ISBN"};
    
        public BookTableModel() {
            books = new ArrayList<>();
        }
    
        public void addBook(Book book) {
            books.add(book);
            fireTableRowsInserted(books.size() - 1, books.size() - 1);
        }
    
        @Override
        public int getColumnCount() {
            return columnNames.length;
        }
    
        @Override
        public int getRowCount() {
            return books.size();
        }
    
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Book book = books.get(rowIndex);
            switch (columnIndex) {
                case 0: //title
                    return book.getTitle();
                case 1: //Author
                    return book.getAuthor();
                case 2: //ISBN
                    return book.getIsbn();
            }
            return null;
        }
    }
    Bonus: you can augment your TableModel with this:
    Java Code:
    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }
    
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    When you iterate over your ResultSet, fill the BookTableModel:

    Java Code:
    BookTableModel btm = new BookTableModel();
    
    ResultSet rs = preparedStatement.executeQuery(); //you already have created this PreparedStatement ofcourse
    while( rs.next()) {
       btm.addBook( new Book( rs.getString("isbn"), 
                                             rs.setString("title"), 
                                             rs.getString("author") 
                                           )
                          );
    }
    
    JTable bookTable = new JTable( btm);
    There are lots of caveats here like editing, table updating, large collections of books, etc. But I hope this gives you a general idea of what to do with value objects and a TableModel.

    Disclaimer: I have not compiled nor tested this code. Use at your own risk. If you computer explodes because of this, don't complain to me :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    TomTom is offline Member
    Join Date
    Dec 2015
    Posts
    3
    Rep Power
    0

    Default Re: Resultset and JTable

    Thanks for your explaination, it worked successfully without exploding ;)

Similar Threads

  1. ResultSet and JTable
    By franklyn@bellsouth.net in forum AWT / Swing
    Replies: 1
    Last Post: 09-10-2013, 09:56 AM
  2. Problem with Jtable with ResultSet
    By dnix in forum AWT / Swing
    Replies: 2
    Last Post: 08-18-2012, 11:36 PM
  3. Multiple Resultset in Jtable
    By hash004 in forum New To Java
    Replies: 1
    Last Post: 07-17-2011, 09:14 PM
  4. Replies: 2
    Last Post: 05-07-2011, 06:32 PM
  5. ResultSet to JTable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 09:01 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •