Reputation: 1521
I have a private String variable called totalCost in my main Window GUI called class PriceCalculator, as well as a getter method called getCost(). I need to access this variable in my secondary window which pops-ups as an event from my main Window.
This is my second Window:
public class ExtraWindow extends JDialog {
public ExtraWindow(JFrame frame) {
super(frame, "Calculations", true);
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
getContentPane().setLayout(null);
this.setSize(300,300);
centerWindow(this);
}.......
....
As you can see, I think the second Window takes the main window as an argument. This is where I need the totalCost from the first window:
JLabel lblCost = new JLabel("Cost: ");
Since I need to use the information from the specific frame that it's using, I can't make an anonymous or another PriceCalculator and get it from there. I tried doing frame.getCost(), which lead me to casting frame as a PriceCalculator, but my program wouldn't run.
How would I access this variable from the first window, which is an argument for the second?
Sorry, forgot to add, where in the first window, the event happens:
public class Event implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
new ExtraWindow(PriceCalculator.this);
}
Upvotes: 0
Views: 111
Reputation: 347314
Change public ExtraWindow(JFrame frame)
to public ExtraWindow(PriceCalculator frame)
, this way you now have direct access to the PriceCalculator
and all it's methods...
Provide an additional parameter that passes the value to the constructor...
public ExtraWindow(JFrame frame, double cost) {...
Supply a setCost
method in ExtraWindow
Upvotes: 2