• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to extend JLabel class??  RSS feed

 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends:
I need to add some methods and variables into JLabel, it means I hope to extend JLabel class, can any guru tell How to extend JLabel class??
For example, I need to add:
[1]. methods getUserName(); and setUserName();
[2]. variable, such as Last Name, etc
into JLabel Class or any other similiar class,
how to do it??
Do I need to rewrite JDK6 API?
Thanks
[ August 31, 2007: Message edited by: Michelle Wang ]
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLabel is not declared as final, so you can always extend it. It doesn't mean that you need to override all the methods that you extend from. You can always add and override the methods that you want.

Hope this helps.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Freddy Wong. Extending JLabel is really easier than (I think) you assume. Just give it a go; you have nothing to lose.

Just out of curiosity, what are you trying to achieve with a JLabel subclass?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the little you have said until now, it sounds lot like this is one of the many circumstances where you really, *really* should prefer composition over inheritance...
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Freddy Wong:
JLabel is not declared as final, so you can always extend it. It doesn't mean that you need to override all the methods that you extend from. You can always add and override the methods that you want.

Hope this helps.



Great, Thanks a lot,
But where is JLabel original class JLabel.java located in JDK 6??
is it in C:\jdk1.6.0\jre\lib or C:\jdk1.6.0\lib or somewhere else??
I cannot find the original JLabel Class.
Can you tell me??
Or I can download all source file from java.sun.com??
Any link available for JDK6 source code??
Thanks
[ September 01, 2007: Message edited by: Michelle Wang ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do you need the source? And again, why are you trying to subclass it? As mentioned, composition may be better than inheritance.

just search the java site for the source. it shouldn't be hard to find.
[ September 01, 2007: Message edited by: pete stein ]
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pete stein:
why do you need the source? And again, why are you trying to subclass it? As mentioned, composition may be better than inheritance.

just search the java site for the source. it shouldn't be hard to find.

[ September 01, 2007: Message edited by: pete stein ]


Good question,
I declare
NewEmployee selectedLabel;
here, NewEmployee.java class extends JLabel{}
As we know, in the method,
public void mousePressed(MouseEvent e) {
.......
Component[] c = ((JComponent)e.getSource()).getComponents();
}

Because I add several JLabels in JPanel, then Array c now gets all JLabels in it,
and NewEmployee.java has some custom methods I need but JLabel.java class does not have them such as getEmployeeName(), setEmployeeName() etc, here NewEmployee.java subclass JLabel class, so when I cast :
selectedLabel = (NewEmployee)c[i];
It throws errors as follows:
javax.swing.JLabel cannot be cast to atest.NewEmployee...

So how to solve this problem??
Thanks
[ September 01, 2007: Message edited by: Michelle Wang ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So in essence you have an array (c) that holds either objects of a certain class (JLabel) or holds a subclass of this class (NewEmployee), and you want to iterate through this array, casting NewEmployees as themselves when they are encountered, correct? If so, have you looked at the instanceOf construct? This may be just what you need:



Remember to watch your use of parenthesis. They can help you a great deal.

Out of curiosity, why use a JLabel where a JButton seems more natural?
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLabel is provided to do some UI stuff. Why do you need to add getName()/setName() methods to it? If you really need to do this, how about this way?

Does that helps?
 
Amit Saste
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its easy..
JLabel is not the final class,so you can extend it without overriding all of the methods declared in JLabel class..we override all the methods only when we implements one of the interface like ActionListener.
example: class labeldemo extends JLabel{//your method declaration}..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!