Results 1 to 18 of 18
  1. #1
    shomid is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Thumbs up Build a ImageButton Component

    Hi,

    I want to create a ImageButton Component,So Add Images package to Project and add Images to this Package
    and created a class and add following Code:

    Java Code:
    public class btn extends JButton implements Serializable {
        
        public btn(){
            super();
            this.setText("ثبت");
            
            try{
            ImageIcon _Icon = new ImageIcon(ClassLoader.getSystemResource("Images/Add.png"));
            this.setIcon(_Icon);
            }catch(Exception err){
                System.out.println(err);
            }
        }
    }
    but no dispaly image on button.
    Please Help me

    Thanks,
    omid
    Last edited by shomid; 09-09-2011 at 08:59 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: Build a ImageButton Component

    Are you sure that you're accessing the file correctly? Is that going where you think it's going? Have you tried using a full path instead?

    Why don't you just pass that icon into the super constructor?

    PS- You should probably use standard naming conventions- classes start with a capital letter, variables with a lowercase letter.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    shomid is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Unhappy Re: Build a ImageButton Component

    Thank You

    But not resolve my problem.
    Path Image is right.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: Build a ImageButton Component

    Quote Originally Posted by shomid View Post
    Thank You

    But not resolve my problem.
    Path Image is right.
    You didn't answer my questions. What happened when you used the full path?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Build a ImageButton Component

    Is the folder with the Images folder on the classpath?

  6. #6
    shomid is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default Re: Build a ImageButton Component

    When my used the full path,Happen Error:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.awt.font.LineBreakMeasurer.nextOffset(LineBre akMeasurer.java:346)
    at java.awt.font.LineBreakMeasurer.nextOffset(LineBre akMeasurer.java:310)
    at sun.swing.SwingUtilities2.clipString(SwingUtilitie s2.java:450)
    at javax.swing.SwingUtilities.layoutCompoundLabelImpl (SwingUtilities.java:1003)
    at javax.swing.SwingUtilities.layoutCompoundLabel(Swi ngUtilities.java:873)
    at javax.swing.plaf.basic.BasicButtonUI.layout(BasicB uttonUI.java:421)
    at javax.swing.plaf.basic.BasicButtonUI.paint(BasicBu ttonUI.java:179)
    at javax.swing.plaf.metal.MetalButtonUI.update(MetalB uttonUI.java:114)
    at javax.swing.JComponent.paintComponent(JComponent.j ava:758)
    at javax.swing.JComponent.paint(JComponent.java:1022)
    at javax.swing.JComponent.paintChildren(JComponent.ja va:859)
    at javax.swing.JComponent.paint(JComponent.java:1031)
    at javax.swing.JComponent.paintChildren(JComponent.ja va:859)
    at javax.swing.JComponent.paint(JComponent.java:1031)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:5 64)
    at javax.swing.JComponent.paintChildren(JComponent.ja va:859)
    at javax.swing.JComponent.paintToOffscreen(JComponent .java:5111)
    at javax.swing.BufferStrategyPaintManager.paint(Buffe rStrategyPaintManager.java:285)
    at javax.swing.RepaintManager.paint(RepaintManager.ja va:1132)
    at javax.swing.JComponent.paint(JComponent.java:1008)
    at java.awt.GraphicsCallback$PaintCallback.run(Graphi csCallback.java:21)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGra phicsCallback.java:60)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraph icsCallback.java:97)
    at java.awt.Container.paint(Container.java:1797)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:738)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:683)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:663)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:128)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Build a ImageButton Component

    Is that all of the error message? Usually there is a line in the error message that refers to your program.
    Did you see the name of your class anywhere in the error message?

  8. #8
    shomid is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default Re: Build a ImageButton Component

    Yes,this is all error message.
    Is not my class in the error message!!!

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Build a ImageButton Component

    Do you see the name of your class in the error message?
    Please copy and paste here the line that has the name of your class

  10. #10
    shomid is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default Re: Build a ImageButton Component

    Is not name of my class in the error message!!!

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Build a ImageButton Component

    Sorry, I misread your response. Normal English would be:
    The name of my class is not in the error message

    Change your code to get the URL from getSystemResource into a variable and print its value before using it in ImageIcon's constructor to see if it is finding the file.

  12. #12
    shomid is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default Re: Build a ImageButton Component

    Added following Code:
    System.out.println(getClass().getResource("/Images/Add.png"));

    Result:
    file:/C:/Users/omid/Documents/NetBeansProjects/Projeh/JCom/build/classes/Images/Add.png

    Path Image is Right.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Build a ImageButton Component

    Is your program working now?

  14. #14
    shomid is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default Re: Build a ImageButton Component

    Yes,but no display image in button and gives the error mentioned above

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Build a ImageButton Component

    Are you using the code you posted in post#12 to get the URL of the image?
    Did you try what I suggested in post#11 to see what getSystemResource returned?

    If one method works and the other method does not work,
    Change your code to use the one that works.

  16. #16
    shomid is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default Re: Build a ImageButton Component

    Code:
    System.out.println(ClassLoader.getSystemResource(" Images/Add.png"));

    Result:
    file:/C:/Users/omid/Documents/NetBeansProjects/Projeh/JCom/build/classes/Images/Add.png

  17. #17
    shomid is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default Re: Build a ImageButton Component

    Thanks for your time/having me

  18. #18
    shomid is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default Re: Build a ImageButton Component

    I resolved my problem.

    I resolved my problem with removed this.setText("ثبت"); line.

Similar Threads

  1. Replies: 10
    Last Post: 04-11-2011, 09:34 PM
  2. HELP: how and where was this build?
    By timosilver in forum New To Java
    Replies: 1
    Last Post: 12-15-2010, 10:53 AM
  3. How to build jar with JNI
    By chyrl in forum Advanced Java
    Replies: 11
    Last Post: 07-22-2010, 08:43 AM
  4. ant build
    By sardare in forum New To Java
    Replies: 4
    Last Post: 04-02-2009, 10:02 PM
  5. how i can build exe file??
    By ahmed13 in forum Advanced Java
    Replies: 10
    Last Post: 02-06-2009, 07:54 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
  •