Home > database >  Trying to create a really simple button for my Panel, but even though i implement the action listene
Trying to create a really simple button for my Panel, but even though i implement the action listene

Time:07-05

I am trying just to get the button to display some text in the console, but whatever i do it isn't working here is the code for the Button class:

public class Button extends JButton implements ActionListener {
JButton button;
Button (){
    button = new JButton();
    this.setText("Click NOW");
    button.addActionListener(this);
    
    this.setForeground(Color.white);
    button.setBounds(300, 100, 100, 50);
    this.setBackground(Color.red);
    this.setBorder(null);
}
@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource()== button) {
        System.out.println("Display if you work");
    }
  }
}

There are no errors displayed and the code compiles correctly, it just isn't displaying the text in the terminal.

CodePudding user response:

This code creates two JButtons, one the button field inside of the class, that you add the action listener to:

public class Button extends JButton implements ActionListener {
    JButton button; // here!

    Button (){
        button = new JButton();  // here!
        this.setText("Click NOW");
        button.addActionListener(this); // and add the listener here

and the other which is the instance of this class that extends JButton:

// here !!!
public class Button extends JButton implements ActionListener {
    // ....

and which is likely the one that is displayed as elsewhere you likely have this code:

Button button = new Button();

and then add this button to the GUI. Again, this "button" is from your Button class which extends JButton but doesn't have the action listener added to it.


You can solve this in one of two ways:

  1. Don't create the new JButton button field inside of your new class and instead add the ActionListener to the this JButton, the instance of this class, for example:
public class Button1 extends JButton implements ActionListener {
    // JButton button;

    Button1() {
        // button = new JButton();
        this.setText("Click NOW");
        // button.addActionListener(this);
        this.addActionListener(this);

        this.setForeground(Color.white);
        // button.setBounds(300, 100, 100, 50); // You really don't want to do
        // this
        this.setBackground(Color.red);
        this.setBorder(null);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // no need for the if block
        // if (e.getSource() == button) {
        System.out.println("Display if you work");
        // }
    }
}
  1. Don't create a class that extends JButton but instead create code that creates a single JButton (not two) and add the ActionListener to the same object that is added to the GUI.

I'd go with number 2 myself and make it a method that returns a button with my properties of interest:

private JButton createMyButton(String text) {
    JButton button = new JButton(text);
    button.setForeground(Color.WHITE);
    button.setBackground(Color.RED);
    button.setBorder(null);
    button.addActionListener(e -> {
        System.out.println("Display if you work");
    });
    
    return button;
}

Side notes:

  • Avoid giving your class names that clash with core Java classes, such as class Button which clashes with the java.awt.Button class.
  • Avoid use of null layouts and setBounds. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.

For that reason you're far better off learning about and using the layout managers. You can find the layout manager tutorial here: Layout Manager Tutorial, and you can find links to the Swing tutorials and to other Swing resources here: Swing Info.

CodePudding user response:

In your actionPerformed method, use equals in the if statement, like this:

if (e.getSource().equals(button)) {
   System.out.println("Display if you work");
}

It should work. == doesn't work in this case.

  •  Tags:  
  • java
  • Related