Refreshing JTable

Hi all,
  I need help with regards to refreshing JTables (if that's what you call it).
  I implemented a screen that accepts text in a text field and then updates it to the table model.
  Using the createDefaultColumnsFromModel() command I managed to get it to refresh, but the columns all get bunched up to the left side of the table! Any way I can stop it from doing that?

  I'm using JDK 1.2.1

  Thanks
jernhungAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

heyhey_Commented:
are you adding dynamically new columns ?
setiawanCommented:
try

table.repaint();
table.revalidate();

  danny
vladi21Commented:
How update JTable after I modify the data?
TableModel model=table.getModel();
if (model instanceof AbstractTableModel){
((AbstractTableModel)model).fireTableDataChanged();
}

or implemant your TableModel explicitly.

DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);


also look

http://java.sun.com/docs/books/tutorial/uiswing/components/table.htm 

http://manning.spindoczine.com/sbe/files/uts2/Chapter18html/Chapter18.htm 
http://users.vnet.net/wwake/swing/faq.html#table
http://www.codeguru.com/java/Swing/JTable/index.shtml

if u still have problems - post ur code


Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

jernhungAuthor Commented:
I have actually subclassed AbstractTableModel already.

The model is working fine. It implements a vector, with each item representing a row in the table.

The problem is that I didn't know how to refresh the table when a new item has been added to the vector.

setiawan's solution was exactly what I need.

vladi21, thanks for your help, nevertheless.

If setiawan would propose the answer, I would gladly award the points to him/her.

vladi21Commented:
I think fireTableDataChanged();
better way and it probably call

table.repaint();
table.revalidate();

:)
vladi21Commented:
AbstractTableModel.java

    /**
     * Notify all listeners that all cell values in the table's rows may have changed.
     * The number of rows may also have changed and the JTable should redraw the
     * table from scratch. The structure of the table, ie. the order of the
     * columns is assumed to be the same.
     * @see TableModelEvent
     * @see EventListenerList
     */
    public void fireTableDataChanged() {
        fireTableChanged(new TableModelEvent(this));
    }


JTable.java
//
// Implementing TableModelListener interface
//

    /**
     * The TableModelEvent should be constructed in the co-ordinate system
     * of the model, the appropriate mapping to the view co-ordinate system
     * is performed by the JTable when it recieves the event.
     */
    public void tableChanged(TableModelEvent e) {
        if (e == null || e.getFirstRow() == TableModelEvent.HEADER_ROW) {
            // The whole thing changed
            clearSelection();

            if (getAutoCreateColumnsFromModel())
                createDefaultColumnsFromModel();

            resizeAndRepaint();
            if (tableHeader != null) {
                tableHeader.resizeAndRepaint();
            }
            return;
        }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
heyhey_Commented:
vladi:
in fact (as you can see yourself) the JTable code that you posted is executed on TableStructureChange, not on TableDataChange.

and this is not your first misleading answer
heyhey_Commented:
of course I agree that jernhung must call
fireTableDataChanged();
or
fireTableStructureChanged();
if he has changed the column data

peace
vladi21Commented:
I have posted only beginning of
public void tableChanged method

    /**
     * The TableModelEvent should be constructed in the co-ordinate system
     * of the model, the appropriate mapping to the view co-ordinate system
     * is performed by the JTable when it recieves the event.
     */
    public void tableChanged(TableModelEvent e) {
        if (e == null || e.getFirstRow() == TableModelEvent.HEADER_ROW) {
            // The whole thing changed
            clearSelection();

            if (getAutoCreateColumnsFromModel())
                createDefaultColumnsFromModel();

            resizeAndRepaint();
            if (tableHeader != null) {
                tableHeader.resizeAndRepaint();
            }
            return;
        }

        if (e.getType() == TableModelEvent.INSERT) {
            tableRowsInserted(e);
            return;
        }

        if (e.getType() == TableModelEvent.DELETE) {
            tableRowsDeleted(e);
            return;
        }

        int modelColumn = e.getColumn();
        int start = e.getFirstRow();
        int end = e.getLastRow();

        if (start == TableModelEvent.HEADER_ROW) {
            start = 0;
            end = Integer.MAX_VALUE;
        }

        int rowHeight = getRowHeight() + rowMargin;
        Rectangle dirtyRegion;
        if (modelColumn == TableModelEvent.ALL_COLUMNS) {
            // 1 or more rows changed
            dirtyRegion = new Rectangle(0, start * rowHeight,
                                        getColumnModel().getTotalColumnWidth(), 0);
        }
        else {
            // A cell or column of cells has changed.
            // Unlike the rest of the methods in the JTable, the TableModelEvent
            // uses the co-ordinate system of the model instead of the view.
            // This is the only place in the JTable where this "reverse mapping"
            // is used.
            int column = convertColumnIndexToView(modelColumn);
            dirtyRegion = getCellRect(start, column, false);
        }

        // Now adjust the height of the dirty region according to the value of "end".
        // Check for Integer.MAX_VALUE as this will cause an overflow.
        if (end != Integer.MAX_VALUE) {
            dirtyRegion.height = (end-start+1)*rowHeight;
            repaint(dirtyRegion.x, dirtyRegion.y, dirtyRegion.width, dirtyRegion.height);
        }
        // In fact, if the end is Integer.MAX_VALUE we need to revalidate anyway
        // because the scrollbar may need repainting.
        else {
            resizeAndRepaint();
        }
    }

....
other
TableModelListener interface
methods


For full comprehension of table API it is better to look on Swing sources :)
jernhungAuthor Commented:
OK, I got it working with events too. Thanks.

But instead of fireTableStructureChanged(),

fireTableRowsInserted(...) worked better for me.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.