0

I have a JFrame with buttons and when I click one of the buttons an integer decreases by 1. I am trying to show the integer in another JFrame but when I reference it I get an error saying non static variable cannot be referenced in a static context. How can I make this a non static variable?

Here is the code from when a button is clicked.

private void DietPepsiBTNActionPerformed(java.awt.event.ActionEvent evt) {                                             
    MessageLBL.setText("Enjoy your Diet Pepsi!");
    credit -= 1.00;
    stCredit = Double.toString(credit);
    CreditAMT.setText("$" + stCredit);
    Refresh();
    dietPepsi -= 1;
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Your error says that you are trying to use Object and its properties from a static context. Static contexts can't reference Objects. Please post full java code. – Ravi Trivedi May 03 '13 at 03:36
  • If you create a static variable outside JFrames doesn't work? – felipe May 03 '13 at 03:48
  • Show us the entirety of the relevant code. What variable do you want to share? What parts of your code are static? How do you declare the variable? – ApproachingDarknessFish May 03 '13 at 04:12
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson May 03 '13 at 05:28
  • 1
    Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use it consistently. – Andrew Thompson May 03 '13 at 05:47

3 Answers3

1

Provide some kind accessor in the master frame (to allow other components to read the value) (something like getValue() for example).

When ever the value is changed, fire some kind of event. You could cheat and use a PropertyChange event which would require you not to add any additional code, or you could fire something like a change event that notifies the other frame that the value has changed.

The second frame would then use the getValue method to read the value.

This would require the second frame to have a reference to the master (so it can get the value).

Better yet, just create a model, allow the model to fire events and share the model.

Have a look at Observer Pattern for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Brendon's answer is close. Ideally what you do what he suggests, create a separate object and pass it in to each frame. The frames then share the objects. Since the frames themselves will have references to the object, you don't need any kind of global reference.

YourModel model = new YourModel();
Frame1 frame1 = new Frame1(yourModel);
Frame2 frame2 = new Frame2(yourModel);

Additionally, you implement the PropertyChangeListener idiom to where each frame subscribes to the property changes in YourModel.

That way, when Frame1 makes changes to YourModel, Frame2 will be notified of them and can keep itself up to date automatically.

Then the game becomes a matter wiring together objects and their listeners. After that, it's almost magic how it all works together.

Ref: http://docs.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
0

Make a third object and pass a reference to both jframes. This shared object can store any properties that you need

brendon
  • 363
  • 3
  • 11