This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable - how can I repaint it with updated data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable - how can I repaint it with updated data" Watch "JTable - how can I repaint it with updated data" New topic

JTable - how can I repaint it with updated data

B Walshe

Joined: Jun 05, 2014
Posts: 2

Here's what I would like to happen:

  • Java/Swing dialog box appears on the user's screen
  • It allows the user to enter 2 values into labelled text fields (fname/sname)
  • They select a "Click Me" push button which I hope will write the data into the table

  • But the table does not get refreshed.

    If I minimise the dialog box and then open it again, the data still does not appear.

    The code I originally built using eclipse/WindowBuilder.
    I've cut it back and tried to isolate the issue.
    But I still cannot figure it out.

    I've added some diagnostic log messages to ensure that I am seeing the various events I think I need to see and have put code in the action callbacks etc.

    As I am a complete newbie at this sort of thing I am probably doing something silly - or forgetting to do something - or completely missing some significant point.
    Can someone help me out with this please?
    And thank you in advance.

    Rob Camick
    Ranch Hand

    Joined: Jun 13, 2009
    Posts: 2372

    You are using the wrong DefaultTableModel constructor. You should create the model by only using the column names and 0 rows of data, since your data will be entered by the user and you don't know how many rows will be added. Read the DefaultTableModel API for the appropriate constructor to use

    To update data in the table, you need to update the data in the TableModel. You do this by using the addRow(...) method of the DefaultTableModel:

    The TableModel will then notify the table that the data has changed and the table will repaint itself automatically.
    B Walshe

    Joined: Jun 05, 2014
    Posts: 2
    That's it Rob!
    Got it straight away.
    Its that old TableModel stuff that I need to get my head around and the relationship between Tables and TableModels.

    Thank you for your on to my next conundrum.
    I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
    subject: JTable - how can I repaint it with updated data
    It's not a secret anymore!