I have a question about creating my own custom classes. I want my own class - NButton. The idea of this class was to quickly make JButtons, by adding my own custom argument sets. Here's my prototype for this:

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * NButton - Nick's JButton.
 * 
 * @author (Nick Saika) 
 * @version (1.0 - 2006)
 */
public class NButton
{
    /**
     * Constructor for objects of class Btn
     */
    public NButton(String s) 
    {
        JButton button = new JButton();
        button.setText(s);
    }
    public NButton(String s, ActionListener al)
    {
        JButton button = new JButton();
        button.setText(s);
        button.addActionListener(al);
    }
    
}

On it's own, the code compiles fine. But I get the following error when I try to implement it with the following code in the main runtime class with the line npanel.add(new NButton("Exit", new ActionExit(0))); .


(NOTE: ActionExit is another class I wrote which exits the program - I'm referencing it the same way as I am the NButton class, and it doesn't complain.

Recommended Answers

All 4 Replies

well, you need to make the button extend JButton (or Component) to be able to add it to a form; try with JButton because otherwise you'll have to write your own graphic routines and action notifiers; try this code:

[LEFT]import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * NButton - Nick's JButton.
 * 
 * @author (Nick Saika) 
 * @version (1.0 - 2006)
 */
public class NButton [B]extends JButton
[/B]{
    /**
     * Constructor for objects of class Btn
     */
    public NButton(String s) 
    {
      [B]  this.setText(s);[/B]
    }
    public NButton(String s, ActionListener al)
    {
        [B]this.[/B]setText(s);
        [B]this.[/B]addActionListener(al);
    }
    
}[/LEFT]

as you can see, your class is now acting alot more like a button (it's writing its own label and adding its own actionlistener as apposed to adding changing them on another object). Now, your JFrame will accept it as if it were a button/component aswell.

Matt

I'm a fan of Making a button a listener of itself.

public class ExtendJButton implements ActionListener
{
  public ExtendJButton(String label)
  {
    super(label);
    addActionListener(this);
  }

  public void actionPerformed(ActionEvent e)
  {
    System.out.println("Override this action event please.");
  }
}

Then create an anonymous inner class' with these buttons...

JButton jButton = new ExtendedJButton("Test Button")
{
  public void actionPerformed(ActionEvent e)
  {
    System.out.println("Your code for what ever the button should do goes here.");
  }
};

And then you're good. You never have to write an if statement to determine which button is being pressed. The button itself knows what it needs to do.

Then create an anonymous inner class' with these buttons...

you learn something new everyday. knowing that would have saved me alot of extension/workarounds in previous work.

Matt

Thanks for the help guys (and quite possibly girls).

The method which you proposed, hooknc, is the same model that's used in a Java book I have. What I've done is created a separate class that's the ActionListener, and then if I need to use it, I use it with the special button class that MattEvans helped me get working.

:cheesy:

EDIT: And with my method, you can just do the shorthand creating of the button - ie. without needing an if statement to figure out which button is being pressed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.