Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Populating a JTable across two actions

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working on a chemical calculator and using a JTable to do this.  There are 4 rows in total: name, quantity, grams and mls.  

I have a form that allows the user to select the name of a chemical and a quantity and pressing a button adds this to the first two columns of each row.

The other two columns are calculated values that depend on the quantities of all the chemicals, as well as other information held in a database about each chemical.

I would like these two columns to be populated when I click my 'calculate' button.

I'm not really sure how to go about this as I've already used the getValueAt() method of the table model to populate just the first two columns of each row.  Can this even be done?  Or does the table have to be populated as a whole in one action?
Would it be better to create another table with all the values after the grams and mls have been calculated and have this new table displayed in place of the first?

Sorry if I'm not explaining this very well, I'm pretty new to Swing.  If you need any clarifications, just let me know.

Thanks.
 
Sheriff
Posts: 16132
268
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Have a look at this tutorial: https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

In particular, look at the setValueAt() method of the example in the "Create a Table Model" section on that page.
 
Château Na Fleur
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Junilu,

Thanks for the response, I'll be sure to check that out.

I have one more question.  When you add a row to a JTable and then call fireDataTableChanged(), is the entire table repopulated or is it just the new row that is added?  That is, are the methods to add a row to the table called again for each row or just for the row being added?

Thanks again
 
Marshal
Posts: 72478
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch (again)

I know precious little about JTables, but have you looked at the documentation for that method to add rows? What does it say? The fireXXX() methods usually don't change the source but send an event to some object listening for it.
 
Château Na Fleur
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply, Campbell.  Ok, so it seems that is does redraw the entire table.  

So I'm thinking maybe I could set up a sort of flag so that if the flag is set to false, then the calculated values are simply returned as null (since I need the total quantities to calculated them), and if it is set to true (on pressing the 'calculate button') then a method is called to return the proper calculated values.  Hmm I'll need to think on that but I could end up running into scope problems if I'm changing variables in other classes...
 
Campbell Ritchie
Marshal
Posts: 72478
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes me suspicious. It sounds complicated and might go horribly wrong.
 
Château Na Fleur
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what I tried was adding a private static boolean 'state' (to be renamed) variable to the row creator class and a static method that can change its value.  When the 'calculate' button is pressed, my controller class calls that method to change the value and the table is redrawn.

When state is false the getter for grams return null and when it is true, the getter will return the calculated value.

I've managed to get it to work doing this but if this sounds like absolute crap, please say.

Thanks again
 
Campbell Ritchie
Marshal
Posts: 72478
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it is static, that is a formula for trouble. I you have more than one table running, they will interfere with each other. You should develop an allergy to static
 
Château Na Fleur
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's just the one table I'm working with.  Yeah see that's what I was worried about, a lot of people say to avoid static variables.  I'll scrap that plan then and have a think tomorrow.

Thanks for your input
 
Campbell Ritchie
Marshal
Posts: 72478
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Château Na Fleur wrote:. . . a lot of people say to avoid static variables. . . .

With good reason.

Thanks for your input

That's a pleasure
 
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Château Na Fleur wrote:I have one more question.  When you add a row to a JTable and then call fireDataTableChanged(), is the entire table repopulated or is it just the new row that is added?  That is, are the methods to add a row to the table called again for each row or just for the row being added?



For the first of your questions: Neither.

 And for the second question: The fireDataTableChanged() method doesn't modify the JTable in any way. It just notifies the objects that were listening to the table model (Note: You don't add rows to a JTable, you add the rows to a table model. The JTable will be one of those listeners and will react by redrawing itself.)

And my question: Why aren't you calling fireTableRowsInserted(row, row) when you insert a row into a table model?
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic