Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

counter_button extends JButton

Thread view: 
mehafi@gmail.com - 11 Aug 2007 08:27 GMT
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
Andrew Thompson - 11 Aug 2007 08:53 GMT
>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.

Signature

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

mehafi@gmail.com - 11 Aug 2007 11:07 GMT
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)
Lew - 11 Aug 2007 12:48 GMT
> Hi,
> how to write a CounterButton class, which should extends JButton
[quoted text clipped - 7 lines]
>
> 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.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.