Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: JTable won't redraw unti i resize the window

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default JTable won't redraw unti i resize the window

    Hi,

    Basically, i have a JTable with a custom TableModel derived from AbstractTableModel, i've added an addRow() function to my TableModel that adds an element to an internal database. it then calles fireTableDataChanged() to make the table refresh; However, i was only able to see the new row if i resize the window where JTable is in.

    Is this some sort of bug, or something is wrong with my implementation?

    Thank you in advance.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JTable won't redraw unti i resize the window

    Have you tried calling JTable.repaint() after the fireTableDataChanged() method?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    shadowfiend (September 4th, 2014)

  4. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: JTable won't redraw unti i resize the window

    Without seeing the code for your custom model there is not much we can do.
    The code where you add your JTable to its parent component might be useful too.

  5. The Following User Says Thank You to Cornix For This Useful Post:

    shadowfiend (September 4th, 2014)

  6. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: JTable won't redraw unti i resize the window

    Hi,

    I've tried calling repaint() method but the same problem occurs

    Forgive me about this somewhat unclean implementation.

    Here is my custom TableModel
    import java.sql.SQLException;
    import java.util.ArrayList;
    import javax.swing.event.TableModelListener;
    import javax.swing.table.AbstractTableModel;
     
    public class BooksTableModel extends AbstractTableModel {
     
        public BooksTableModel() throws SQLException {
            books = new BooksTable();
            columns = columnNames.length;
     
            if(!books.doesAlreadyExist()) {
                books.create();
            }
     
            data = books.getAll();
            rows = books.getCount();
        }
     
        public void addRow(Book book) {
            books.add(book);
            data.add(book);
            ++rows;
            fireTableDataChanged();
        }
     
        public BooksTable getInternalTable() {
            return books;
        }
     
        @Override
        public int getRowCount() {
            return rows;
        }
     
        @Override
        public int getColumnCount() {
            return columns;
        }
     
        @Override
        public String getColumnName(int columnIndex) {
            return columnNames[columnIndex];
        }
     
        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return String.class;
        }
     
        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }
     
        // TODO
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            switch(columnIndex) {
                case 0:
                    return data.get(rowIndex).getIsbn();
                case 1:
                    return data.get(rowIndex).getTitle();
                case 2:
                    return data.get(rowIndex).getAuthor();
            }
            return null;
        }
     
        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        }
     
        @Override
        public void addTableModelListener(TableModelListener l) {
        }
     
        @Override
        public void removeTableModelListener(TableModelListener l) {
        }
     
        BooksTable books;
        ArrayList<Book> data;
        final private String[] columnNames = {"ISBN", "Title", "Author"};
        final private int columns;
        private int rows;
    }

    Im actually using netbeans' GUI Designer tool and this is the stripped down version of the code generated
    private void initComponents() {
        booksJTable = new javax.swing.JTable();
        // ...
        booksJTable.setModel(new BooksTableModel());
        // ...
        jScrollPane1.setViewportView(booksJTable);
        // ...
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        // ...
        pack();
    }

  7. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JTable won't redraw unti i resize the window

    Quote Originally Posted by shadowfiend View Post
    Im actually using netbeans' GUI Designer tool
    There's your problem.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    shadowfiend (September 4th, 2014)

  9. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: JTable won't redraw unti i resize the window

    Well, it is kind of obvious why it isnt working:
     
        @Override
        public void addTableModelListener(TableModelListener l) {
        }
     
        @Override
        public void removeTableModelListener(TableModelListener l) {
        }
    So, when the JTable adds its listener to your model, what do you think will happen?
    And when you fire the event, how many listeners are going to get updated?

  10. The Following User Says Thank You to Cornix For This Useful Post:

    shadowfiend (September 4th, 2014)

  11. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: JTable won't redraw unti i resize the window

    There's your problem.
    Well, it is kind of obvious why it isnt working:
    Im an idiot.

    lol, i am completely new to this event driven mechanism. I should probably learn hand coding this stuffs first before relying to things like this GUI Designer

Similar Threads

  1. I always need to resize my window to see my programs
    By MW130 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 22nd, 2014, 05:06 AM
  2. String won't display in my window
    By JamEngulfer221 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 24th, 2011, 04:23 PM
  3. [SOLVED] JScrollPane width won't reduce with window
    By Douglas_Reissener in forum AWT / Java Swing
    Replies: 1
    Last Post: October 3rd, 2011, 10:19 AM
  4. Resize a window without native decorators
    By supertreta in forum AWT / Java Swing
    Replies: 0
    Last Post: January 11th, 2011, 02:06 PM
  5. image scaling on window resize
    By chopficaro in forum Java Theory & Questions
    Replies: 2
    Last Post: August 27th, 2010, 03:09 PM