Jonathan Simon
Jonathan Simon

Reputation: 47

Java/Swing JButton does not display its text and does not perform its action

I want to write a simple street crossing traffic light system. I want to make a button which will start the whole program (open up the GUI of the traffic light system). But already my first button starts to make problems. It doesnt display its text and the action it should perform won't happen. I am really a beginner so its probably some dumb and obvious fault but please have a look I would be really happy ^^

package kreuzung;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JFrame;

public class HomeFrame extends JFrame{

    public HomeFrame(String title) {
        super(title);

    this.setLayout(new FlowLayout(FlowLayout.CENTER));

    Button test = new Button("noAction");


    Container cont = getContentPane();

    cont.add(test, BorderLayout.CENTER);



    }
}

And this would be the generated button which doesn't do the things its supposed to do

package kreuzung;

import javax.swing.Action;
import javax.swing.JButton;

public class Button extends JButton{

    private String actionName;

    public Button(String actionName) {

        this.actionName = actionName;                       //set the Action name of this button                             

        JButton button = new JButton();                     //instantiate this Button
        button.setText(actionName);                         //set the Action Name as Button Text
        button.setSize(30, 30);             
        button.setBounds(5, 5, 25, 25);     




        button.addActionListener(new Evt(this.actionName));     //add an Action Listener to the button 
                                                            //and gets the Action from the Evt Class
    }

}

And last but not least here's the Evt class which should take care of the action performing

package kreuzung;
import java.awt.event.*;

import javax.swing.JFrame;

public class Evt implements ActionListener {

    private String actionName;

    public Evt(String actName) {

        this.actionName = actName;

    }



    @Override
    public void actionPerformed(ActionEvent e) {
        switch(actionName) {
        case "noAction":
            JFrame frame = new HomeFrame("Home");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 400);
            frame.setVisible(true);
            break;
        }
    }

}

Upvotes: 1

Views: 1319

Answers (2)

Frakcool
Frakcool

Reputation: 11163

There are several errors in your code:

  1. You shouldn't extend JFrame, see Extends JFrame vs. creating it inside the program
  2. Don't call setBounds(...) the Layout Managers will take care of positioning your components
  3. Don't leave too much extra space between lines or after / before opening / closing braces {} it becomes hard to read
  4. Don' call Button as a class name, it could be confused with java.awt.Button class.

It doesnt display its text and the action it should perform won't happen

In this class:

public class Button extends JButton {
    private String actionName;
    public Button(String actionName) {
        this.actionName = actionName;
        JButton button = new JButton();
        button.setText(actionName);
        button.setSize(30, 30);             
        button.setBounds(5, 5, 25, 25);     

        button.addActionListener(new Evt(this.actionName));
    }
}

You extend from JButton and then you create a JButton inside of it! so, you have 2 JButtons, the one from the class (inherited) and the one you create inside it. But you're setting the text to the one created inside but you're adding the other one (without text) to your JFrame.

In a metaphor, it's like:

  • You write in a page something
  • You get a new white page and add it to your book, instead of adding the one you wrote in to your book.

There's no need to extend JButton in your current program, so just create a new JButton instance and that's it.

Otherwise if you really want to use a custom JButton class do this:

public class MyCustomButton extends JButton { // Change class name
    private String actionName;
    public MyCustomButton(String actionName) {
        super(actionName); //Sets the text
        this.actionName = actionName;

        button.addActionListener(new Evt(this.actionName));
    }
}

Upvotes: 2

Romano
Romano

Reputation: 455

You do not really need to create a JButton's children class since you do not add any particular attribute to it. Instead you should be able to make it works this way:

public class HomeFrame extends JFrame{

    private static final String BUTTON_ACTION_NAME = "myActionName";

    public HomeFrame(String title) {
        super(title);

        this.setLayout(new FlowLayout(FlowLayout.CENTER));
        JButton test = new JButton();
        test.setText(BUTTON_ACTION_NAME);
        test.setSize(30, 30);             
        test.setBounds(5, 5, 25, 25);     
        test.addActionListener(new Evt(BUTTON_ACTION_NAME));
        Container cont = getContentPane();
        cont.add(test, BorderLayout.CENTER);
    }
}

Upvotes: -1

Related Questions