6 Replies - 1327 Views - Last Post: 22 March 2011 - 07:45 PM Rate Topic: -----

#1 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 11-March 11

Question about refreshing a jtable/grabbing values from jtable

Posted 21 March 2011 - 07:57 PM

Howdy!

I've been looking into this for awhile now and am just looking for some clarification. I am trying to get my jtable to refresh after I insert a record to the database. I've been reading up on this and I have to use the tableModel of the jtable in order to refresh it. I've read up on the repaint() method but haven't had much luck with that.

I used vectors to create the columns/rows in my jtable - would there be a different option to refreshing or would it be the same as if I used, let's say, arrays to create it?

Also, for grabbing items out of the table would I have to find the location of each cell and use that for the corresponding text field? I've been looking around for so long I would just like a definite push towards the right answer.

Any information would be wonderful!

Is This A Good Question/Topic? 0
  • +

Replies To: Question about refreshing a jtable/grabbing values from jtable

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Question about refreshing a jtable/grabbing values from jtable

Posted 21 March 2011 - 08:23 PM

it is not repaint()

it is table.valueChanged(ListSelectionEvent e);

ListSelectionEvent has a constructor to specifiy the range of the cells updated/deleted/inserted
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10774
  • View blog
  • Posts: 40,122
  • Joined: 27-December 08

Re: Question about refreshing a jtable/grabbing values from jtable

Posted 21 March 2011 - 08:43 PM

When dealing heavily with data for a JComponent, you should focus on the TableModel to modify the JTable's contents.

Also check out my blog entry on Models and Renderers for JComponents.
Was This Post Helpful? 1
  • +
  • -

#4 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 11-March 11

Re: Question about refreshing a jtable/grabbing values from jtable

Posted 22 March 2011 - 12:24 PM

Hello,

Thank you for your suggestions.

I have a question about my code. I've tried debugging my code to see if my tableChanged() method is getting hit after I insert into the table however it's not.

I also declared a tableModelListener in my default constructor

public void tableChanged(TableModelEvent e) {

        int row = e.getFirstRow();
        int column = e.getColumn();
        DefaultTableModel model = (DefaultTableModel)e.getSource();
        //String columnName = model.getColumnName(column);
        //Object data_1 = model.getValueAt(row, column);

        

    }



I was wondering if just calling the fireTableDataChanged() would refresh the table once I can get the program to work with my tableChanged()?

I'm still learning this jtable stuff(and omg is there depth in the jtable class) so be gentle if this is completely wrong. :)

This post has been edited by Criel: 22 March 2011 - 12:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Question about refreshing a jtable/grabbing values from jtable

Posted 22 March 2011 - 05:57 PM

View PostCriel, on 22 March 2011 - 02:24 PM, said:

I have a question about my code. I've tried debugging my code to see if my tableChanged() method is getting hit after I insert into the table however it's not.

tableChanged() will be call by the tableModel if you change your model

If you overload TableModel, YOUR implementation will have to take the reponsability of calling tableChanged()
Was This Post Helpful? 0
  • +
  • -

#6 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 11-March 11

Re: Question about refreshing a jtable/grabbing values from jtable

Posted 22 March 2011 - 07:31 PM

View Postpbl, on 22 March 2011 - 05:57 PM, said:

View PostCriel, on 22 March 2011 - 02:24 PM, said:

I have a question about my code. I've tried debugging my code to see if my tableChanged() method is getting hit after I insert into the table however it's not.

tableChanged() will be call by the tableModel if you change your model

If you overload TableModel, YOUR implementation will have to take the reponsability of calling tableChanged()


I may not be understanding this but I assumed that if inserting into the jtable that would constitue a change in the table. Am I not thinking correctly with this?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Question about refreshing a jtable/grabbing values from jtable

Posted 22 March 2011 - 07:45 PM

Lets call apples apples and oranges oranges.

A JTable is just a fancy GUI component that displays the data contained in its model.

The TableModel is the folk that keeps the data and answers to questions asked by the GUI (JTable)

OK to simplify the life of people who do not want to bother with the Model and want a simple to use simple JTable (like the ones you create when you invoke the constructor with an array) the API provides short cuts to access the data.

For example the JTable class as a method

Object o = table.getValueAt(int row, int col);

I can bet what you want that the actual code of the method getValueAt() of the JTable class is:
public Object getValueAt(int row, int col) {
  return this.getModel().getValueAt(roiw, col);
}


Now, did you, overload the TableModel ? TableChanged() should be called by the TableModel.

P.S.
actually does a little bit more, I just checked
It converts the actual rows column in case the user as changed them :)

    /**
     * Returns the cell value at <code>row</code> and <code>column</code>.
     * <p>
     * <b>Note</b>: The column is specified in the table view's display
     *              order, and not in the <code>TableModel</code>'s column
     *		    order.  This is an important distinction because as the
     *		    user rearranges the columns in the table,
     *		    the column at a given index in the view will change.
     *              Meanwhile the user's actions never affect the model's
     *              column ordering.
     *
     * @param   row             the row whose value is to be queried
     * @param   column          the column whose value is to be queried
     * @return  the Object at the specified cell
     */
    public Object getValueAt(int row, int column) {
        return getModel().getValueAt(convertRowIndexToModel(row),
                                     convertColumnIndexToModel(column));
    }


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1