0

I'm working on an inventory system project. There is 'subtotal' label which calculates total amount from JTable 'amount' column in 'mainframe' which I want to be updated when we modify certain row of that table using another frame modifyFrame.

So I want this 'subtotal' to be updated using update button of modifyFrame. How to achieve that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Please share your code in form of [SSCCE](http://sscce.org). – Sergiy Medvynskyy Jan 25 '17 at 15:29
  • 1
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) **One way to allow the child GUI to update the parent GUI, is to provide it reference(s) to the component(s) that need to be updated. There are other, arguably better, ways as well.** – Andrew Thompson Jan 25 '17 at 15:41
  • thanks for the help.. as you said, providing reference is good idea but how to get specific component from parent frame's panel? – Yuvraj Tidame Jan 25 '17 at 18:36
  • Maybe you could make use of some kind of [Observer Pattern](http://www.oodesign.com/observer-pattern.html) – MadProgrammer Jan 25 '17 at 22:50

1 Answers1

0

Keep an instance variable of the JLabel around. When the button gets pressed do your calculations and call setText on the JLabel

Jayfray
  • 415
  • 3
  • 12
  • I tried to use reference of mainFrame in modifyFrame also able to access mainFrame's JPanel using getContentPane() but i don't know to how to access the JLable component from this jPanel of mainFrame refernce. Please help me.. – Yuvraj Tidame Jan 26 '17 at 06:00
  • If you have access to the getContentPane() you could do something like: `for (Component c : frame.getContentPane().getComponents()) { if (c instanceof JLabel) { // Code here } }` – Jayfray Jan 27 '17 at 16:35
  • That's assuming you have only one JLabel and it was added directly to the contentPane. The other option to use the Observer Pattern like MadProgrammer suggested above – Jayfray Jan 27 '17 at 16:38