Results 1 to 14 of 14
  1. #1
    TheShagg is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default Package layout problems trying to make a new Swing component.

    The basic background info:

    I copied the JSlider code from the OpenJDK repo (note that I am running the Sun packages, because this is a windows machine), and created a new component, and UI L&F class for it, by renaming the JSlider code and making some tweaks. I set its package to javax.swing, and the L&F classes to be in the same respective packages. Eclipse does not complain, and the program will run, but when I try to build and show the frame I get the following message:

    IllegalAccessError
    tried to access class javax.swing.plaf.basic.LazyActionMap from class javax.swing.plaf.basic.BasicRangeSliderUI

    I also tried adding the LazyActionMap file to my project, but no change.

    I should note that I do not have the OpenJDK source in this project, I just have the sun libraries linked in (and working fine), and I am specifying my files to be in that same package... I am not sure if this is the right way to do that.

    Thanks for any help.

    P.S. You can see the code I copied here: jdk6/jdk6/jdk: Manifest
    I used JSlider, plaf/SliderUI.java, plaf/Basic/BasicSliderUI.java as my base.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    TheShagg is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    Yes I did google. That bug you posted is over 10 years old, and the last version they refer to is 1.3... I am using 1.6, so I do not think that is the problem.

    I do not think this is a bug in JDK, I think I am doing something wrong.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by TheShagg View Post
    I do not think this is a bug in JDK, I think I am doing something wrong.
    I think that your copy of the JSlider code is using a package protected class somewhere; are you sure your slider code is in the same package as the other Swing classes?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    TheShagg is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    are you sure your slider code is in the same package as the other Swing classes?
    Short answer: No I am not sure :D

    I tried to make it so that they are, you can see that from the error message:

    Quote Originally Posted by TheShagg View Post
    IllegalAccessError
    tried to access class javax.swing.plaf.basic.LazyActionMap from class javax.swing.plaf.basic.BasicRangeSliderUI
    But I don't have the full swing source in my project, I just have my additional files that I want to sort of "tack on to" the package (so that I can access the full functionality that is protected in the package).

    A somewhat equivalent situation would be if I had half a package in one jar file, and the other half in another jar file. Would this work?

    I don't really have a good sense of the hierarchy you are supposed to use if you want to make new swing components.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by TheShagg View Post
    Short answer: No I am not sure :D

    I tried to make it so that they are, you can see that from the error message:



    But I don't have the full swing source in my project, I just have my additional files that I want to sort of "tack on to" the package (so that I can access the full functionality that is protected in the package).

    A somewhat equivalent situation would be if I had half a package in one jar file, and the other half in another jar file. Would this work?

    I don't really have a good sense of the hierarchy you are supposed to use if you want to make new swing components.
    I suspect that your component is in another package than the package where that class it is trying to use is stored in. I don't know if that javax.swing package(s) is are sealed, but read this.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    TheShagg is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    I think that the packages are not sealed. According to that page, a different exception should be thrown.

    Also, this code returns false:

    Java Code:
            boolean isSealed = java.lang.Package.getPackage("javax.swing").isSealed();
            isSealed |= java.lang.Package.getPackage("javax.swing.plaf").isSealed();
            isSealed |= java.lang.Package.getPackage("javax.swing.plaf.basic").isSealed();

  8. #8
    TheShagg is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    If I went through the trouble to create a small project that demonstrated my problems, would anyone here actually try it out?

    I figured out why there isn't a plethora of swing components out there... nobody can figure out how to make them!

  9. #9
    TheShagg is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    Anybody have an ideas how to solve this? I'm still stumped, because I can't move the class out of the package without causing other problems with accessing methods of JComponent.

  10. #10
    TheShagg is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    I am having specific problems with:

    javax.swing.plaf.basic.LazyActionMap and
    javax.swing.plaf.basic.BasicGraphicsUtils.isLeftTo Right()

    It looks to me like the LazyActionMap gets re-implemented in every package that needs it, as well as the isLeftToRight() method. Very odd to me. It seems like the packages are sealed, but I have no other indication of that (see my post above), and I think it is throwing the wrong error for that.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    What you're trying isn't allowed.
    http://java.sun.com/javase/6/jre-6u2-license.txt
    Java Technology Restrictions. You may not create,
    modify, or change the behavior of, or authorize your
    licensees to create, modify, or change the behavior
    of, classes, interfaces, or subpackages that are in
    any way identified as "java", "javax", "sun" or
    similar convention as specified by Sun in any naming
    convention designation.
    db

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    That said, it isn't usually necessary or even desirable to replace a JDK/JRE or other library class to provide extended functionality. Could you describe the "new component" you are trying to create in terms of functionality and user interface?

    db

  13. #13
    TheShagg is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    Thank you Darryl, that might explain it. That license is silly, makes me want to ditch Java. I assume there is something blocking me, intentionally, but I cannot figure out what the mechanism is (it doesn't look like package sealing, but I may be wrong). I'm not really trying to modify anything, really I just want to augment the package by adding more classes.

    What I am doing:

    Basically I am taking the JSlider class, and I copied it's code into a new class called JRangeSlider. This new "Range Slider" class has 2 thumbs so that you can select a range.

    I also copied several of the UI classes for a couple different LookAndFeels.

    I should note that I am not trying to over-ride the JSlider, I am trying to create a seperate (J)component that is very similar.

    From what I can find, that part is actually OK, but the L&F classes, aka the UI classes, seem to be locked down.

    What I envision doing, but am failing:

    To me it would make sense that if I wanted to create a swing component I could copy and paste another component's code, and create a new class out of it in the same package. For instance, JSlider would be copied and modified into JRangeSlider.

    Then the L&F classes would be handled the same. I would copy and paste the SliderUI class into RangeSliderUI, BasicSliderUI into BasicRangeSliderUI, MetalSliderUI into MetalRangeSliderUI. In theory I would have access to all the helper functions that all the other components in that L&F implementation use - because I am writing something for the same L&F!

    The failure comes in that I can't use any of the helper functions/classes in the UI packages. Metal has it's own methods and classes that are locked down, Basic does to, Nimbus has its own. If I want to extend these L&Fs to my own new component, I am locked out of these helpers.... which is stupid, IMO.

    My work around:

    My work around so far is to not use any protected classes in the UI packages, rather, re implement them all. Crude, but it works, and it seems like that is the way each L&F does it. (See method "isLeftToRight" )

    Also, in the UpdateUI method of the component, I first see if there is a corresponding class available for the component in the present L&F. If there is not (which there won't be unless someone writes a L&F and includes my component) I specify a default. I am planning to add some logic in there so that the default UI class is chosen based on which L&F is configured, so that if I happen to write an implementation for "Nimbus", or "Metal" it will load automatically.

    This still has a disadvantage for anyone else who wants to use my package but wants to add a UI class for another L&F. They are going to be forced to subclass or something similar to use their UI implementation - because it won't be in my UpdateUI defaults code.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Ah, the dual thumb slider. I spent a lot of time a few months ago trying to implement it in a way that wasn't tied to a particular LaF, using two JSliders. I dropped the idea when I couldn't get rid of a 1-pixel misalignment in rendering arising out of integer math in the painting code.

    I also recall that I had problems with the way different LaFs compute the preferredSize.

    Wish you luck with your efforts.

    db

Similar Threads

  1. Swing Html Layout
    By Kiruthigadhandapani in forum AWT / Swing
    Replies: 1
    Last Post: 05-03-2011, 03:39 PM
  2. Swing layout is done wrong
    By Onra in forum New To Java
    Replies: 5
    Last Post: 10-08-2010, 04:36 AM
  3. Add Swing Layout.jar
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 10-02-2010, 04:42 AM
  4. swing component
    By cahyadiakbar in forum AWT / Swing
    Replies: 0
    Last Post: 09-22-2010, 10:40 AM
  5. Component Layout method help!!!
    By brandnew956 in forum New To Java
    Replies: 5
    Last Post: 04-14-2010, 08:19 PM

Tags for this Thread

Posting Permissions

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