0

I'm starting my adventure with programming and I've got one problem. I try make a simple calculator using awt. I can't go further because I don't know, how to change one variable - textField initialized in MainFrame. I want to change it in actionPerformed. Here's my code and I'll be grateful if You'll give me some guidance. Thanks!

package starter;
import java.awt.EventQueue;

public class Starter {
    public Starter () {
        EventQueue.invokeLater(new Runnable (){
            @Override
            public void run () {
                new MainFrame();
                System.out.println();
            }
        });
    }

    public static void main(String[] args) {
        new Starter();
    }
}

MainFrame

package starter;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MainFrame extends JFrame {
    private JTextField textField = new JTextField();
    DigitActionListener digitPressed = new DigitActionListener();

    public MainFrame() {
        super("Calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(true);
        setSize(350, 400);
        setLayout (new GridLayout (6, 4, 3, 3));

        JButton buttonClear = new JButton("Clear"); buttonClear.addActionListener(digitPressed);
        JButton button0 = new JButton ("0");        button0.addActionListener(digitPressed);
        JButton button1 = new JButton ("1");        button1.addActionListener(digitPressed);
        JButton button2 = new JButton ("2");        button2.addActionListener(digitPressed);
        JButton button3 = new JButton ("3");        button3.addActionListener(digitPressed);
        JButton button4 = new JButton ("4");        button4.addActionListener(digitPressed);
        JButton button5 = new JButton ("5");        button5.addActionListener(digitPressed);
        JButton button6 = new JButton ("6");        button6.addActionListener(digitPressed);
        JButton button7 = new JButton ("7");        button7.addActionListener(digitPressed);
        JButton button8 = new JButton ("8");        button8.addActionListener(digitPressed);
        JButton button9 = new JButton ("9");        button9.addActionListener(digitPressed);
        JButton multiplicationButton = new JButton ("*");   multiplicationButton.addActionListener(digitPressed);
        JButton divisionButton = new JButton ("/");         divisionButton.addActionListener(digitPressed);
        JButton additionButton = new JButton ("+");         additionButton.addActionListener(digitPressed);
        JButton substructionButton = new JButton ("-");     substructionButton.addActionListener(digitPressed);
        JButton equalsButton = new JButton ("=");           equalsButton.addActionListener(digitPressed);
        JButton commaButton = new JButton (".");            commaButton.addActionListener(digitPressed);

        add (buttonClear);
        add (new JLabel (""));
        add (new JLabel (""));
        JPanel textPanel = new JPanel();
        textPanel.setLayout(new BorderLayout());
        textPanel.add(textField, BorderLayout.CENTER);
        this.add(textPanel);
        add(button7);
        add(button8);
        add(button9);
        add(divisionButton);
        add(button4);
        add(button5);
        add(button6);
        add(multiplicationButton);
        add(button1);
        add(button2);
        add(button3);
        add(additionButton);
        add(commaButton);
        add(button0);
        add(equalsButton);
        add(substructionButton);
    }

    public JTextField getTextField() {
        return textField;
    }

    public void setTextField(String text) {
        textField.setText(text);
    }

}

DigitActionListener

package starter;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JTextField;

class DigitActionListener implements ActionListener {
    int size = 0;
    char[] tab = new char[size];

    public void pain (Graphics g, String s){
        g.drawString(s, 20, 10);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //Object source = e.getSource();
        String command = e.getActionCommand();

        if ("button0".equals(command)) {
            tab[size] = 0;
            String eq = String.valueOf(tab[size]);
            MainFrame mainFrame = new MainFrame();
            mainFrame.setTextField(eq);
            size++;
        }

    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Monika
  • 11
  • 2
  • *"I'll be grateful if You'll give me some guidance."* Ask a question. – Andrew Thompson Jan 08 '16 at 23:41
  • See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Jan 08 '16 at 23:43
  • You need to pass a reference of MainFrame to the instance of DigitiActionListener so that it can gain access to its functionality – MadProgrammer Jan 08 '16 at 23:43
  • Thanks all of You. Everything's fine, I think, but still cannot see result in textField. Nothing change when button0 was clicked. I thought that in textField I'll see my result (in this case it's digit 0). Am I right? Have You got any idea what can be wrong? Sorry for my language mistakes, I'm still working on it. – Monika Jan 10 '16 at 19:55

1 Answers1

4

Your problem is here:

    if ("button0".equals(command)) {
        tab[size] = 0;
        String eq = String.valueOf(tab[size]);
        MainFrame mainFrame = new MainFrame();
        mainFrame.setTextField(eq);
        size++;
    }

You're creating a new MainFrame object and changing its state, but understand that this will have no effect on the completely distinct displayed MainFrame object and will not change its state whatsoever (will not change what is displayed within its JTextField). There are a variety of possible solutions, but it all boils down to understanding what Java references are, and calling methods on the appropriate reference, here the appropriate MainFrame object. A simple solution could be for to pass the MainFrame object into your listener via a listener constructor, and then call the methods on this reference.

For example:

class DigitActionListener implements ActionListener {
    private MainFrame mainFrame;
    int size = 0;
    char[] tab = new char[size];

    public DigitActionListener(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
    }

    public void pain (Graphics g, String s){
        g.drawString(s, 20, 10);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //Object source = e.getSource();
        String command = e.getActionCommand();

        if ("button0".equals(command)) {
            tab[size] = 0;
            String eq = String.valueOf(tab[size]);
            // MainFrame mainFrame = new MainFrame(); // **** no, don't do this
            mainFrame.setTextField(eq);
            size++;
        }
    }
}

and then within MainFrame itself, do something like:

// pass *this* or the current MainFrame instance, into the DigitalActionListener
DigitActionListener digitPressed = new DigitActionListener(this);

Other issues -- learn to use and then use arrays and ArrayLists, as this can help you simplify your code, and thus make program improvement and debugging much easier.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373