• 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

Get total for JTable column

 
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
I have multiple JSpinner in application as image below.



If the quantity is 1, it will add a new row, if more than 1, the row will be modified. If the quantity is 0 , the row will be removed.
 
How can I subtract the total value if the quantity is decreasing ?

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our GUIs forum.
Don't try one Listener listening to several sources. Give each source its own listener class. Is change listener a functional interface? Can you write a λ expression instead?
I can never remember what you do about tables, but I would have thought for something small like that you can recalculate the whole row in the time it takes you to click the arrows on the spinner.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, don't write such a switch. Create a Food class which encapsulates price picture and description. Then iterate the row calculating the prices for all items.
 
John Joe
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

Is change listener a functional interface?


ChangeListener is an abstract interface that extends java.util.EventListener
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it is a functional interface. You can shorten that class to λ expressions.
 
John Joe
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

Campbell Ritchie wrote:So it is a functional interface. You can shorten that class to λ expressions.

There are multiple spinners in two tabs too which may share the same listener class too . Will it caused a problem?  
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Joe wrote:. . . There are multiple spinners . . . which may share the same listener class too . Will it caused a problem? . . .

It is not a case of sharing listener classes, but of sharing listener objects. You can have two instances of the same class with different fields which thereby do different things. But it is not usually a good idea to share the same object like that.
 
John Joe
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

Campbell Ritchie wrote:No, don't write such a switch. Create a Food class which encapsulates price picture and description. Then iterate the row calculating the prices for all items.

Is this an good idea by writing three different classes for each tab?
 
Marshal
Posts: 28176
95
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

John Joe wrote:Is this an good idea by writing three different classes for each tab?



If the other two tabs look just like that one, then I wouldn't write a different class for each tab. I'd have three instances of one class, with different contents. (Just like you might have three instance of the String class with different contents.)
 
John Joe
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

Paul Clapham wrote:If the other two tabs look just like that one, then I wouldn't write a different class for each tab. I'd have three instances of one class, with different contents. (Just like you might have three instance of the String class with different contents.)

No,for drinks tab, it contains five image while desserts tab only contains two.
 
Paul Clapham
Marshal
Posts: 28176
95
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

John Joe wrote:

Paul Clapham wrote:If the other two tabs look just like that one, then I wouldn't write a different class for each tab. I'd have three instances of one class, with different contents. (Just like you might have three instance of the String class with different contents.)

No,for drinks tab, it contains five image while desserts tab only contains two.



And a String object can contain five characters or only two.
 
John Joe
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
I still have no idea on how to iterate the row calculating the prices for all items.
 
John Joe
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
I able to solve it in the end. I remove total variable and assign the specific row value to new variables.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic