counter_button extends JButton

M

mehafi

Hi,
I wrote a class counter_button, which extends JButton class. The Text
of counter button is a value how much it was presed ( with additional
text):

public class counter_button
{
private int counter = 0;
public JButton button;

public counter_button()
{
button = new JButton("I was presed " + counter + " times");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
button.setText("I was presed " + ++counter + " times");
});
}
}

and I use this class ( in anoter class ):

....
counter_button my_button = new counter_button();
JFrame f = new JFrame("my frame");
Container con = f.getContentPane();
con.add(my_button.prz);

It works fine! But, is it possible to write counter_button class to
use it like that:
con.add(my_button);
instead of:
con.add(my_button.prz);

thanks in advance
 
A

Andrew Thompson

Hi,
I wrote a class counter_button, which extends JButton class.

So why are you posting this rubbish?
public class counter_button
{
...

This class extends Object, not JButton, and sure
does not have an ..
con.add(my_button.prz);

..attribute by the name of 'prz'.

Try posting an SSCCE* - that explains much better,
but dump the 'other class' and put a little test main
in the custom button class.
* <http://www.physci.org/codes/sscce.html>

As an aside, common Java nomenclature suggests
this class should be called CounterButton, rather
than counter_button.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200708/1
 
M

mehafi

Hi,
how to write a CounterButton class, which should extends JButton
class, which task is to write on it own text number of its pressed.
Object of this class shoud be added to container like Standard JButton
( without any variables ):

container.add(my_counter_button)

instead:

container.add(my_counter_button.some_variable)
 
L

Lew

Hi,
how to write a CounterButton class, which should extends JButton
class, which task is to write on it own text number of its pressed.
Object of this class shoud be added to container like Standard JButton
( without any variables ):

container.add(my_counter_button)

instead:

container.add(my_counter_button.some_variable)

Did you read Andrew's response?

You didn't provide enough information to hint what the problem actually is.

Put together an SSCCE. (See Andrew's post for the link.) The likelihood is
that doing so will help provide you insight into your problem, and the
certainty is that it'll make it possible for us on the newsgroup to understand
and maybe help with the problem you're having.

As an aside, common Java nomenclature as promulgated by Sun and practiced
nearly universally suggest that class names be spelled with upper-case letters
at the beginning of the class name and of each subsequent compound word part,
with no underscores, thus MyCounterButton, rather
than my_counter_button.

Maybe someone can help without further details from you, but I don't see how.
Andrew gave you really good advice in this regard that you seem to have
disregarded.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top