• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable isn't getting updated

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've created a currency exchange calculator imports its date from the internet, stores it in a .dat file, and also displays all its data inside a JTable.

It should work the following way, if you update the JTable, the exchange rates the calculator uses, will also change, it used to work just fine, until I needed to import the data for the JTable from the database itself. I also have a method that manually update all the data in the database, that works as a stack, and the table and the calculator should always take the data from the last imported currency rates.

Now the problem is that, when you edit the table manually by yourself for the first time, it works perfectly, but if I'll update the data using the method, it will update the data as it should, and the calculator will use the updated data, but the table won't be effective anymore. meaning, after the method has been used, it doesn't update the table anymore, and editing manually the data in the table won't affect the data that the calculator uses.

Is there any way to update the entire table while I update the database? Right now it works the following way: The JTable constructor takes the 1st element in the database, and when I update the database, I believe the table just doesn't affect at all.

I've created a more simplify code to show the issue:



Database class:



I'm sorry it's kinda long, but it's the shortest I could've make it.

If you will update the table manually, and click on show, it will show the updated value you entered, so it does update the database, since the 'get_table_cell' method always takes the data from the database. If you click on update from the menu, everything that you will update from now on will be in vain for the table, but the "calculator" will still use the updated value from the database.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of cross posting a question (see: https://stackoverflow.com/questions/45691616/jtable-isnt-getting-updated) why don't you ask a clear question that makes sense the first time.

I have no real idea what your question is about. You state:

but if I'll update the data using the method,



What method? Maybe you know the method you are talking about be we don't. We don't have time to read through all of your code to try and understand the logic flow.

If you click on update from the menu,



What is this supposed to do. All I can see is that is invokes Extraction_BOI(...)

(by the way follow Java naming conventions. Method names should NOT start with an upper case character, Same with variable names}.

All this method does is create a 2D array with data, but the data is never used to update the JTable.

You define the method to return the 2D array, but you never use the returned value.

I'm sorry it's kinda long, but it's the shortest I could've make it.



We don't care about your application. The code you post should only show the concept of what you are trying to do. Then once you get the basic concept working you apply the knowledge to your real application.

So what you need to do is:

1. Create a simple frame with a JTable and some hard coded data.
2. Add an "Update" button to the frame. When you click this button you will referesh the table with a different set of hardcoded data. So your update() logic will need access to either the original JTable or the TableModel. Then you have a choice to either create an new TableModel and add it to the table, or simply update the existing TableModel. But the key point is you would NOT create a new table.

This is called a SSCCE. A SSCCE should be posted with every question.
 
Gilad Peeri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:Instead of cross posting a question (see: https://stackoverflow.com/questions/45691616/jtable-isnt-getting-updated) why don't you ask a clear question that makes sense the first time.

I have no real idea what your question is about. You state:

but if I'll update the data using the method,



What method? Maybe you know the method you are talking about be we don't. We don't have time to read through all of your code to try and understand the logic flow.

If you click on update from the menu,



What is this supposed to do. All I can see is that is invokes Extraction_BOI(...)

(by the way follow Java naming conventions. Method names should NOT start with an upper case character, Same with variable names}.

All this method does is create a 2D array with data, but the data is never used to update the JTable.

You define the method to return the 2D array, but you never use the returned value.

I'm sorry it's kinda long, but it's the shortest I could've make it.



We don't care about your application. The code you post should only show the concept of what you are trying to do. Then once you get the basic concept working you apply the knowledge to your real application.

So what you need to do is:

1. Create a simple frame with a JTable and some hard coded data.
2. Add an "Update" button to the frame. When you click this button you will referesh the table with a different set of hardcoded data. So your update() logic will need access to either the original JTable or the TableModel. Then you have a choice to either create an new TableModel and add it to the table, or simply update the existing TableModel. But the key point is you would NOT create a new table.

This is called a SSCCE. A SSCCE should be posted with every question.



My actual code is more than double the size of this code, I've simplified it the best I could, and deleted most of the code so this code shows only the problem, there is calculator there, just a JTestField that outputs the requested data from the table, and shows it is no longer connected to the updated database.

I took my question from stackoverflow after editing it there more several times and creating the new code just for the purpose of the question, but nobody replied there ever since I created the new code.

And my question, as I posted just above the code is: Is there any way to update the entire table while I update the database? Right now it works the following way: The JTable constructor takes the 1st element in the database, and when I update the database, I believe the table just doesn't affect at all.

I want the table to gets always the last element in the database, which implements vector, but not by copying each cell with a for loop, but by actually rely on the new String[][] that the database receives, like just before I add a new String[][] to the database.
 
Gilad Peeri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't edit my posts so  just a quick note I forgot to mention if it wasn't clear; I want the table to be able to edit the database even after I click the update button.

I've created a short video that shows the problem if it helps:
 
Rob Camick
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And my question, as I posted just above the code is: Is there any way to update the entire table while I update the database? Right now it works the following way: The JTable constructor takes the 1st element in the database, and when I update the database, I believe the table just doesn't affect at all.  



And again you haven't paid attention to my suggestion or listen to the comments in your other posting. You need to understand how MVC works.

You should only ever create the JTable once

Then you either:

1. create a new TableModel reset the model of the table by using table.setModel(...)
2. or you update the TableModel directly by using methods like setRowCount(0) and addRow(...)

Since you don't seem to understand this MVC concept that is why I suggested you start with the simple SSCCE to learn the basic concept first. Once you get this working (and we can help you with this if you post the code), then it will be easy to fix your real application code.
 
Gilad Peeri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:

And my question, as I posted just above the code is: Is there any way to update the entire table while I update the database? Right now it works the following way: The JTable constructor takes the 1st element in the database, and when I update the database, I believe the table just doesn't affect at all.  



And again you haven't paid attention to my suggestion or listen to the comments in your other posting. You need to understand how MVC works.

You should only ever create the JTable once

Then you either:

1. create a new TableModel reset the model of the table by using table.setModel(...)
2. or you update the TableModel directly by using methods like setRowCount(0) and addRow(...)

Since you don't seem to understand this MVC concept that is why I suggested you start with the simple SSCCE to learn the basic concept first. Once you get this working (and we can help you with this if you post the code), then it will be easy to fix your real application code.



Thanks, I really appreciate your answer.

At first I thought that creating a new tablemodel won't help me, since I wanted the calculator to obtain the data from the database itself, but since the table is always getting updated from the database by changing the tableModel, I can just get the calculator to obtain the data from the tableModel using getValueAt.

Really helped me to solve it by trying this one again
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I can just get the calculator to obtain the data from the tableModel using getValueAt.



Yes, the data in the TableModel is always in memory.

Getting data from a database is not very efficient since that implies an external data source.

The database should only be used to initially populate the JTable, then you work with the data in the table after that. If you change the data in the table then you update the database.
 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also posted at http://www.dreamincode.net/forums/topic/405813-jtable-isnt-getting-updated/
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Really helped me to solve it by trying this one again



And have you replied to all the other forums where the question is cross posted to tell people the solution has been found (and then post the solution) so people don't spend time on a question that already has an answer?

 
Gilad Peeri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:

Really helped me to solve it by trying this one again



And have you replied to all the other forums where the question is cross posted to tell people the solution has been found (and then post the solution) so people don't spend time on a question that already has an answer?



Yea I have replied in all the forums how I solved this, it was these 2 and stack, almost forgot about stack cause didn't get much help there, but I posted there now a full answer
 
And inside of my fortune cookie was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic