0

I am new to Java Programming. I would like to create JButton inside the class and create ActionListener in the main by passing button arguments. But it throws an error:

Cannot make a static reference to the non-static field newBtn

My code is as below.

import java.awt.event.*;  
import javax.swing.*;    
public class TestBtn { 

TestBtn() {
}

public void myBtn(JButton mybtn){   
    JFrame f=new JFrame("My Example");   

    mybtn =new JButton("Click Here");  
    mybtn.setBounds(50,100,95,30);  

    f.add(mybtn);  
    f.setSize(400,400);  
    f.setLayout(null);  
    f.setVisible(true);   
} 
}

// My Main Program

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

import javax.swing.*;  
public class TestBtnMain {
    JButton newBtn; 

public static void main(String[] args) {
    TestBtn btn = new TestBtn();
    btn.myBtn(newBtn);

    newBtn.addActionListener(new ActionListener(){  
        public void actionPerformed(ActionEvent e){  
            System.out.println("Success");
            }  
        });
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rajesh
  • 195
  • 1
  • 2
  • 15
  • just don't do it like that... very bad programming style. `public class TestBtnMain { JButton newBtn; ` should be `public class TestBtnMain { static JButton newBtn; ` – XtremeBaumer Apr 06 '17 at 06:25
  • *"I would like to create `JButton` inside the class and create `ActionListener` in the main by passing button arguments."* Why? See [What is the XY problem?](http://meta.stackexchange.com/q/66377) *"I am new to Java Programming."* Then you probably should avoid GUI programming for a while. It is an advanced topic. Wehn you get back to GUI programming, `f.setLayout(null);` this is one of the things you should **never** do. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to .. – Andrew Thompson Apr 06 '17 at 06:36
  • .. pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Apr 06 '17 at 06:37
  • You are really getting things in the wrong place. It is absolutely pointless to create an action listener in your main to pass it around this way. Instead: have a class that extends JFrame; and create your button in there; and simply directly add a listener in that place. Collect experience, and then, when looking into larger examples, learn how to further separate your classes. – GhostCat Apr 06 '17 at 06:40

3 Answers3

2

Unlike to C++ Java has no possibility for transfer of parameters by reference. So I would change your code as following:

import java.awt.event.*;  
import javax.swing.*;    
public class TestBtn { 

    TestBtn() {

    }
    public JButton myBtn(){   

        JFrame f=new JFrame("My Example");   

        JButton mybtn =new JButton("Click Here");  
        mybtn.setBounds(50,100,95,30);  

        f.add(mybtn);  
        f.setSize(400,400);  
        f.setLayout(null);  
        f.setVisible(true);   
        return mybtn;
    } 

}

Main class:

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

import javax.swing.*;  
public class TestBtnMain {

    public static void main(String[] args) {
        TestBtn btn = new TestBtn();
        JButton newBtn = btn.myBtn();

        newBtn.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent e){  
                System.out.println("Success");
            }  
        });


    }
}

And, as already mentioned by Andrew Thompson, you should use a layout manager to make your UI working with the different platforms/screen resolutions

Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
0

You are using the variable newBtn without using any object. It is an instance variable and hence needs an object to be accessed.

This statement should be changed

btn.myBtn(newBtn);

Use an instance of the class TestBtnMain to access newBtn

or you can make newBtn static.

static JButton newBtn;
Pooja Arora
  • 574
  • 7
  • 19
0

Your TestBtn class should be :

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

public class TestBtn { 
  private JFrame f;
  private JButton mybtn;

TestBtn() {
       f=new JFrame("My Example");
       mybtn =new JButton("Click Here"); 
}

public void myBtn(){   

    mybtn.setBounds(50,100,95,30);  
    mybtn.addActionListener(new ActionListener(){  
        public void actionPerformed(ActionEvent e){  
           System.out.println("Success");
        }  
    });

    f.add(mybtn);  
    f.setSize(400,400);  
    f.setLayout(null);  
    f.setVisible(true);   
  } 
}

And You main function :

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

public class TestBtnMain {

  public static void main(String[] args) {
      TestBtn btn = new TestBtn();
      btn.myBtn();
  }
}
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65