Results 1 to 3 of 3

Thread: Extends JLabel

  1. #1
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Question Extends JLabel

    Hi,

    I want to use my own labels in the programm.
    So i have extend JLabel to put my own extentsions in it.

    Java Code:
    public class Label extends JLabel {
        
        public Label(String s) {
            super(s);
        }
    }
    But when i want to use the ".setHorizontalTextPosition" on such a Label it does not work.
    I get the following error message
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.awt.Label.setHorizontalTextPosition
    Java Code:
    Label label = new Label("text");
    label.setHorizontalTextPosition(0);
    Why is that ?
    The "".setHorizontalTextPosition" is public in JLabel thus an extends of JLabel must have acces to it.

    instead i have to make first an implementation of "Label"

    Java Code:
    public class LabelImpl extends Label {
    
        public LabelImpl(String s) {
            super(s);
        }    
    }
    Then this is working.

    Java Code:
    LabelImpl label = new LabelImpl("text") ;
    label.setHorizontalTextPosition(0);
    What is the reason for that ?

    Kind regards
    Dipke

  2. #2
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default Re: Extends JLabel SOLVED

    Problem solved.
    I have imported the awt.label instead of my label object.
    Thats why it was not working

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Extends JLabel

    Always a bad idea to name your custom classes the same as a JDK class (and especially so when the name of a class in the java.lang package is duplicated).

    That said, are you sure you want to use inheritance? It's usually better to write a method (or a static method in a utility class) that sets the desired attributes of an unextended JLabel.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. problem with two extends of Inheritance
    By MaceMan in forum New To Java
    Replies: 29
    Last Post: 04-14-2011, 05:06 PM
  2. Initialize Vs extends
    By N00Bie in forum New To Java
    Replies: 1
    Last Post: 03-22-2011, 03:58 PM
  3. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-07-2010, 12:02 AM
  4. extends question
    By mac in forum New To Java
    Replies: 8
    Last Post: 04-03-2010, 03:29 AM
  5. My icon extends my JButton
    By hitmen in forum AWT / Swing
    Replies: 5
    Last Post: 03-06-2009, 08:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •