0

I was just trying to code stuff for fun, however I am stuck. I am trying to create 3 JButtons in one class A, and then call those buttons in another class B and then add ActionListeners in class B. So basically I only create the buttons in abstract class A and then call them in class B which extends class A and in class B I do stuff with those buttons. It sounds very simple, however I can't figure this out.

In class A:

public abstract class Activity implements ActionListener {

        public static JFrame frame;`
        public static JPanel panel;
        public static JLabel label;
        public JButton buttonleft;
        public JButton buttonmid;
        public JButton buttonright;

    protected void makenButtons(String textlinks, String textmidden, String textrechts) {
                JButton buttonleft = new JButton(textlinks);
                JButton buttonmid = new JButton(textmidden);
                JButton buttonright = new JButton(textrechts);
                panel.add(buttonleft);
                panel.add(buttonmid);
                if (textrechts!=null) {
                panel.add(buttonright);
            }

In class B:

package hi;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class MainActivity extends Activity{

    public static void main(String[] args) {
        Activity.makenFrame(380,120,"Choose mode");
        Activity.makenLabel("Choose a work mode: ");
        Activity.makenOpties("Run as server", "Run as client");
        //Activity.makenButtons("Start","Exit",null);
        MainActivity a = new MainActivity();
        a.actiesButtons("Start","Exit");
        Activity.toevoegenpanel();
        }

    public void actiesButtons(String textleft, String textright) {
        JButton buttonleft  = super.buttonleft;
        JButton buttonmid  = super.buttonmid;
        buttonleft.setText(textleft);
        buttonmid.setText(textright);
        buttonleft.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == buttonleft) {
                System.out.print("Hi");
        }
     }          
});
        buttonmid.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == buttonright) {
                System.exit(0);             }
        }           
    });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }
}

I deleted some not-important stuff. So basically I just like to know how to call the JButtons in class B. I tried:


 JButton buttonleft  = super.buttonleft;
        JButton buttonmid  = super.buttonmid;

However this gave me a nullpointerexception. Thanks in advance!

Edit: I know my problem is the exception, I just don't know how to fix it :( Could someone please write some quick pseudocode for me how to create a button in one class and call that button in another? :)

Prashant Gupta
  • 788
  • 8
  • 26
J. Doe
  • 11
  • 2
  • 2
    You never call makenButtons anywhere. So the buttons are never created. – JB Nizet Sep 14 '18 at 13:47
  • Edit: I know my problem is the exception, I just don't know how to fix it :( Could someone please write some quick pseudocode for me how to create a button in one class and call that button in another? :) – J. Doe Sep 14 '18 at 13:54
  • @JBNizet has your problem and the solution. Please don't ask others to create "quick pseudocode for me". That's not how this site works. Please read the [ask] for more on site best practices. – Hovercraft Full Of Eels Sep 14 '18 at 13:55
  • Also if you need to access an object that is within one class from another, you should create a private instance field for the object of interest and a public method that allows *limited* outside interaction (interaction that *you* control). You're declaring your JButton variables within a method, and so they are only visible within that method and cannot be accessed by outside classes without bad kludges. – Hovercraft Full Of Eels Sep 14 '18 at 13:58

0 Answers0