Question

Hi I am working on a project and I am having trouble adding JButtons. For some reason, it wont print its name.

This class is supposed to add the custom buttons

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

public class FightPanel extends JPanel {

public static final int WIDTH = 600;
public static final int HEIGHT = 600;

public FightPanel() {
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    setLayout(new BorderLayout());

    FightButton test = new FightButton("test");
    add(test);
}
}

This class is custom button class

import javax.swing.JButton;

public class FightButton extends JButton {

private String name;

public FightButton(String name) {
    this.name = name;
    setName(name);
}

}

Was it helpful?

Solution

The setName() method does not control the text that is displayed on a button. It is basically just used by application code to identify a component.

The setText(...) is used to set the text that is displayed in the button.

did now know that you have to use the super constructor to do that.

You don't have to but that is the easiest way to do what you want since you are passing the text as a parameter to your class.

If you want to change the text AFTER the component has been created then you would use the setText() method.

OTHER TIPS

All you're doing is setting a non-displayed property of your new class and leaving the name state of the original parent class unchanged. Solution: use the super's constructor to help you out:

public FightButton(String name) {
    super(name);
}

In fact there really is no need for your FlightButton to have a name field since all that does is shadow the JButton's name field. In fact if this is all you add to FlightButton, I would get rid of this class and simply use JButton objects. Myself, that is what I usually do -- use plain vanilla JButtons, but use custom AbstractActions, and I feel that is likely where your energies should go.


Edit
You state in comment:

thank you i did now know that you have to use the super constructor to do that.

You don't have to, as you could also use setter methods and the like, but if you want your constructor to behave like a JButton's similar constructor, best to use the parent's constructor.


Edit 2
For example:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class FightPanel extends JPanel {

   public static final int FP_WIDTH = 600;
   public static final int FP_HEIGHT = 600;

   public FightPanel() {
      setLayout(new BorderLayout());

      JButton test = new JButton(new FightAction("Test", KeyEvent.VK_T));
      add(test);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(FP_WIDTH, FP_HEIGHT);
   }

   private static void createAndShowGui() {
      FightPanel mainPanel = new FightPanel();

      JFrame frame = new JFrame("FightPanel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class FightAction extends AbstractAction {
   public FightAction(String name, int mnemonic) {
      super(name);
      putValue(MNEMONIC_KEY, mnemonic);
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      System.out.println(e.getActionCommand() + " pressed!");
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top