Creating special classes
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.
indienick
Junior Poster in Training
71 posts since Aug 2005
Reputation Points: 23
Solved Threads: 2
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:
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 <strong>extends JButton
</strong>{
/**
* Constructor for objects of class Btn
*/
public NButton(String s)
{
<strong> this.setText(s);</strong>
}
public NButton(String s, ActionListener al)
{
<strong>this.</strong>setText(s);
<strong>this.</strong>addActionListener(al);
}
}
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
MattEvans
Veteran Poster
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64
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
MattEvans
Veteran Poster
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64
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:
[i]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.[i]
indienick
Junior Poster in Training
71 posts since Aug 2005
Reputation Points: 23
Solved Threads: 2