File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Updating JTable with AbstractTableModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Updating JTable with AbstractTableModel" Watch "Updating JTable with AbstractTableModel" New topic

Updating JTable with AbstractTableModel

Kalel Lorav

Joined: Mar 31, 2013
Posts: 1

I am new at programming and this is my first posting on here with the hope to get the help I need to finish my first programming assignment. My JTable comes up fine the first time, but when I enter new input, the table does not update with the new data. The problem is tied with my AbstractTableModel as I just don´t know how to override methods setValueAt and getValueAt. Here is what I have done. the data to be displayed in the table ir read from arrays int nh, String hNam, int nv, String proc_1 and String proc_2. fnh is the number of rows. The table has five columns and that´s remains the same. If someone would be kind enough to correct those two methods methods I would be deeply thankful.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Welcome to the Ranch
Why does your set method apparently not alter any of the values in the model? How do you set the value anywhere?
Why are you using an array of different kinds rather than an object which encapsulates the values for a row? There is something very un‑object‑ orientated about an Object[][]. No, later on you appear to be using parallel arrays; that is even worse design.
There is also something wrong with fnh. Not only is it a poor name for a variable, but also it is tightly coupled to something in another class’ implementation. Why are you not setting that in the constructor? Why are you not passing a Horse[] array to the constructor? That can encapsulate all the information required, even including the row count, because as you already know, all arrays record how many elements they contain. And the Horse class can encapsulate its count of fields, so you don’t need to mess about with integer literals like 5.

I shall move this discussion to the GUIs forum.
I agree. Here's the link:
subject: Updating JTable with AbstractTableModel
jQuery in Action, 3rd edition