Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help updating a DefaultTableModel  RSS feed

 
Jeffery Colins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have been going in circles trying to update a JTable and still looking for some guidance. For now I just need to take input from a textfield after clicking on a JButton and have that text be placed in the JTable. LeftPanel listens, while RightPanel holds the table. Here is what I have so far:

 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you are trying to create a custom TableModel. There is no need for that because your code is using the DefaultTableModel which already supports dynamic upates.

All updates should be done either to the JTable or the TableModel.

For example:



Of course you will also need to change your code so make the JTable an instance variable so that you can reference the table from the setValueAt() method of your panel.
 
Jeffery Colins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info Rob! I am not sure where to put my JTable instance variable though? In the RightPanel Class or LeftPanel Class? When I try making one it doesn't work.
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure where to put my JTable instance variable though?


You can only define an instance variable in the class where you actually create the object.

By the way you also need data in the table in order to use the table.setValueAt(...) method. Read the API for the DefaultTableModel so use an appropriate onstructor.
 
Jeffery Colins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am really not getting anywhere with this. The hints have helped, but unfortunately they have not been enough to solidify what it is I need to do. Its just a case of my lack of comprehension. I am reposting some changes I have made and done away with using the DefaultTableModel because I found some code online that I was trying to use as reference for an AbstractTableModel. I am getting a null pointer exception exactly where I am trying to update my table at line 46 of the LeftPanel Class. Of course, any further help is appreciated:



 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made and done away with using the DefaultTableModel


Why? The DefaultTableModel was not the cause of the problem. You have just made your code way more complicated. Use the standard JDK classes until you understand the basics of Java programming.

I am getting a null pointer exception exactly where I am trying to update my table at line 46 of the LeftPanel


The code in your left panel class looks like this:



You have defined two variables that are null. Nowhere in your LeftPanel class do you use the "new" keyword to create an instance of those classes. The JTable and TableModel are both created in the RightPanel class. If you want to access those variables in the RightPanel class then the LeftPanel classes needs a reference to the RightPanel class and then you need to create methods in the RightPanel class that will allow you to access those variables.

I suggest you combine your right and left classes into one class so that all the variables are defined in one class. And go back to using the DefaultTableModel. If you want to add a row of data you just use the addRow(..) method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!