user2580289
user2580289

Reputation:

JButton not initialized in a method

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener{
    JButton[] nums;
    JButton eq, mult, div, clr, clrEnt, sub, add, dot;
    JTextArea txtArea = new JTextArea();

    public Calculator() {
        super("Calculator");
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pane = new JPanel(new GridLayout(6, 1, 0, 5));
        add(pane);
        JPanel paneSecond = new JPanel(new GridLayout(1, 3, 5, 5));
        JPanel paneThird = new JPanel(new GridLayout(1, 3, 5, 5));
        JPanel paneFourth = new JPanel(new GridLayout(1, 3, 5, 5));
        JPanel paneFifth = new JPanel(new GridLayout(1, 3, 5, 5));
        JPanel paneSixth = new JPanel(new GridLayout(1, 3, 5, 5));
        JPanel paneSeventh = new JPanel(new GridLayout(1, 3, 5, 5));
        nums = new JButton[11];
        //add = new JButton();
        addOpButton(eq, "=");
        addOpButton(mult, "x");
        addOpButton(div, "/");
        addOpButton(clr, "C");
        addOpButton(clrEnt, "CE");
        addOpButton(sub, "-");
        addOpButton(add, "+");
        addOpButton(dot, ".");

        for (int i = 0; i < nums.length; i++) {
            nums[i] = new JButton("" + i);
            nums[i].setActionCommand(nums[i].toString());
            nums[i].addActionListener(this);
        }

        addPanel(paneSecond, 1, 4);
        addPanel(paneThird, 4, 7);
        addPanel(paneFourth, 7, 10);
        //addButtons(paneFifth, add, nums[0], sub);
        pane.add(paneSecond);
        pane.add(paneThird);
        pane.add(paneFourth);
        pane.add(paneFifth);
        pane.add(paneSixth);
        pane.add(paneSeventh);
        pack();
    }

    void addPanel(JComponent pane, int start, int condition) {
        for (int i = start; i < condition; i++) {
            pane.add(nums[i]);
        }
    }

    void addButtons(JComponent pane, JButton btn1, JButton btn2, JButton btn3) {
        pane.add(btn1);
        pane.add(btn2);
        pane.add(btn3);
    }

    void addOpButton(JButton btn, String op) {
        btn = new JButton(op);
        btn.setActionCommand(op);
        btn.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
    }

    public static void main(String[] args) {
        Calculator calc = new Calculator();
    }
}

In the addOpButton method none of my buttons are being initialized. I am not sure why though. The string is being passed and used but not the button. Even after initializing one of the buttons (the "add" button in the constructor with the removal of the comment) the button is still null in the method. I cannot figure out why. Any help is appreciated.

Upvotes: 0

Views: 1243

Answers (1)

Xabster
Xabster

Reputation: 3720

Java is "pass by value". That means that if you try to pass a reference, the reference is copied and then passed.

In practices this means that the btn parameter you give to addOpButton is a copy, and the other copy is held by whoever called the method. When you then do btn = new you're assigning something to the copy, giving it a command and an actionlistener, and then it's discarded. The caller still has a reference to his own copy.

Upvotes: 4

Related Questions