-1

I have JFrame 1 which shows JLabel "Balance" - my bank account balance and 2 JButton components (Add income; Add expenses). By clicking one of these buttons I hide main frame and open income of expenses frame where I add the data.

After I input amounts into JTextField components and click "Save" button, in dialog field I can see that my record was saved, but when I click "Back" button, the "Balance" label stays 0 as if nothing was entered.

Could somebody help me? My code is a mess now so I doubt it would be helpful.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Read https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice – George Z. Mar 16 '19 at 23:48
  • [Passing information to a method or a constructor](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) – MadProgrammer Mar 17 '19 at 03:55
  • *"My code is a mess"* Nobody wants to see your code. What would be helpful is a cut-down version of the code that includes only the parts relevant to the problem - a [mcve]. In addition to the advice of @GeorgeZ. I will add: **Don't** extend `JFrame`. I've never (in a decade plus of helping people) seen a valid reason to do so. Extending GUI components / windows is likely contributing (indirectly) to the problem, in that (for example) the data in data models which should be shared, become closely tied to a specific class. – Andrew Thompson Mar 17 '19 at 04:56

1 Answers1

0

Here is an easy example where I tried to rebuild your program:

public class Class {
    public static void main(String[] args) {
        Frame1 frame1 = new Frame1();
        Frame2 frame2 = new Frame2();

        frame1.setChildWindow(frame2);
        frame2.setParentWindow(frame1);
    }
}

Frame1:

import javax.swing.*;

class Frame1 extends JFrame {
    private int balance = 0;
    private JLabel balanceLabel = new JLabel(String.valueOf(balance));
    private Frame2 childWindow;

    Frame1() {
        JPanel panel = new JPanel();
        panel.add(new JLabel("Balance:"));
        panel.add(balanceLabel);
        JButton balanceButton = new JButton("Balance");
        balanceButton.addActionListener(e -> {
            childWindow.setVisible(true);
            setVisible(false);
        });
        panel.add(balanceButton);
        getContentPane().add(panel);
        pack();
        setVisible(true);
    }

    void setChildWindow(Frame2 childWindow) {
        this.childWindow = childWindow;
    }

    void addBalance(int balance) {
        this.balance+=balance;
        balanceLabel.setText(String.valueOf(this.balance));
    }
 }

Frame2:

import javax.swing.*;

class Frame2 extends JFrame {
    private Frame1 parentWindow;

    Frame2() {
        JComboBox<Integer> comboBox = new JComboBox<>(new Integer[] {1,2,3,4,5,6,7,8,9});
        JButton addButton = new JButton("add");

        addButton.addActionListener(e -> {
            parentWindow.addBalance((Integer)comboBox.getSelectedItem());
            parentWindow.setVisible(true);
            setVisible(false);
        });

        JPanel panel = new JPanel();
        panel.add(comboBox);
        panel.add(addButton);
        getContentPane().add(panel);
        pack();
    }

    void setParentWindow(Frame1 parentWindow) {
        this.parentWindow = parentWindow;
    }
}

If you have any further questions, feel free to ask!

(But btw, in your next questions, post some code, so that other people can help you better. Even if it is a mess, other people could help you with that as well and give you hints what you could to better or how you could write your code cleaner ^^)

Mano176
  • 168
  • 13