• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

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
0
jernhung
Asked:
jernhung
  • 4
  • 3
  • 2
  • +1
1 Solution
 
heyhey_Commented:
are you adding dynamically new columns ?
0
 
setiawanCommented:
try

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

  danny
0
 
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


0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
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.

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

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

:)
0
 
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;
        }
0
 
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
0
 
heyhey_Commented:
of course I agree that jernhung must call
fireTableDataChanged();
or
fireTableStructureChanged();
if he has changed the column data

peace
0
 
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 :)
0
 
jernhungAuthor Commented:
OK, I got it working with events too. Thanks.

But instead of fireTableStructureChanged(),

fireTableRowsInserted(...) worked better for me.


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now