How to resize an Icon in a JLabel dinamicaly

I'm trying to do a slideshow program and an obvious action that the user should be able do is to resize the images he puts in a slide. In my case the image is in a JLabel. I was able to move the image/text of the label but not to resize it. I want it dinamicaly as you usually do.
I'll post my move jlabel code for you to know more a less what I want.
[code/]
public void mouseDragged(MouseEvent e) {
          setCursor(new Cursor(Cursor.MOVE_CURSOR));
          int x = getX() + e.getX() - getWidth() / 2;
          int y = getY() + e.getY() - getHeight() / 2;
          setLocation(x, y);
I tried with the getScaledInstance() method but got confused...
This is a class that extends from JLabel.
Thanks in advance.

PinchaRata wrote:
I tried I can't cast the icon to set it
Image im = ((ImageIcon)getIcon()).getImage();
int x = getX() + e.getX();
int y = getY() + e.getY();
im.getScaledInstance(x, y, Image.SCALE_FAST);^^^^^ There's an error. getScaledInstamce returns_* a new, scaled instance. It doesn't change im.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Similar Messages

  • How to set an icon for a JLabel with Advanced Synth

    Hello,
    Do you know how to define an icon for a JLabel components with Advanced Synth (with a XML file) ? I have tried this :
    <style id="jLabelNotes">
            <imageIcon id="iconNotes" path="images/note.png" />
            <property key="Label.icon" value="iconNotes" />              
    </style>
    <bind style="defaultLabel" type="name" key="jLabelNotes" />But nothing append.
    Thanks.

    Try Sound Menu

  • How to resize the Icon such as 1.gif, 2.gif, 3.gif etc at my will??

    Dear friends:
    I have following code to add the Icon to the nodes of the JTree,
    But I hope to resize the Icon such as 1.gif, 2.gif, 3.gif etc at my will, not fixed one, How can I do it??
    Thanks
    Sunny
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    /** JTree with missing or custom icons at the tree nodes.
    *  1999 Marty Hall, http://www.apl.jhu.edu/~hall/java/
    public class CustomIcons extends JFrame {
      public static void main(String[] args) {
        new CustomIcons();
      private ImageIcon customOpenIcon           = new ImageIcon("1.gif");
      private ImageIcon customClosedIcon      = new ImageIcon("2.gif");
      private ImageIcon customLeafIcon           = new ImageIcon("3.gif");
      public CustomIcons() {
        super("JTree Selections");
    //   WindowUtilities.setNativeLookAndFeel();
    //   addWindowListener(new ExitListener());
        Container content = getContentPane();
        content.setLayout(new FlowLayout());
        DefaultMutableTreeNode root =
          new DefaultMutableTreeNode("Root");
        DefaultMutableTreeNode child;
        DefaultMutableTreeNode grandChild;
        for(int childIndex=1; childIndex<4; childIndex++) {
          child = new DefaultMutableTreeNode("Child " + childIndex);
          root.add(child);
          for(int grandChildIndex=1; grandChildIndex<4; grandChildIndex++) {
            grandChild =
              new DefaultMutableTreeNode("Grandchild " + childIndex +
                                         "." + grandChildIndex);
            child.add(grandChild);
        JTree tree1 = new JTree(root);
        tree1.expandRow(1); // Expand children to illustrate leaf icons
        JScrollPane pane1 = new JScrollPane(tree1);
        pane1.setBorder(BorderFactory.createTitledBorder("Standard Icons"));
        content.add(pane1);
        JTree tree2 = new JTree(root);
        tree2.expandRow(2); // Expand children to illustrate leaf icons
        DefaultTreeCellRenderer renderer2 = new DefaultTreeCellRenderer();
        renderer2.resize(100, 100);
        renderer2.setOpenIcon(null);
        renderer2.setClosedIcon(null);
        renderer2.setLeafIcon(null);
        tree2.setCellRenderer(renderer2);
        JScrollPane pane2 = new JScrollPane(tree2);
        pane2.setBorder(BorderFactory.createTitledBorder("No Icons"));
        content.add(pane2);
        JTree tree3 = new JTree(root);
        tree3.expandRow(3); // Expand children to illustrate leaf icons
        DefaultTreeCellRenderer renderer3 = new DefaultTreeCellRenderer();
        //renderer3.setPreferredSize(new Dimension(100, 100));
        renderer3.setOpenIcon(customOpenIcon);
        renderer3.setClosedIcon(customClosedIcon);
        renderer3.setLeafIcon(customLeafIcon);
        tree3.setCellRenderer(renderer3);
        JScrollPane pane3 = new JScrollPane(tree3);
        pane3.setBorder(BorderFactory.createTitledBorder("Custom Icons"));
        content.add(pane3);
        pack();
        setVisible(true);
    }

    Thanks,
    I declare as :
      private ImageIcon customOpenIcon           = new ImageIcon("com/aa/1.gif");
      private ImageIcon customClosedIcon      = new ImageIcon("com/aa/2.gif");
      private ImageIcon customLeafIcon           = new ImageIcon("com/aa/3.gif");but which method for customOpenIcon can be used as scale its size??
    Is it
    customOpenIcon.getImage().SCALE_AREA_AVERAGING;??
    why show red??
    I checked these, cannot find it, can you advice more or I select a wrong component??
    Thank
    sunny

  • Resizing the icons in the dock

    Can anyone tell me how to resize the icons in my dock, they appear too small for my liking? I have omitted a bunch to give more space but this has made no difference. Any pointers would be appreciated. Ta.

    Here's the quick way to resize the Dock:
    The separator bar to the right of your Application icons can be dragged up and down.
    Also try holding Option while dragging if you want it to snap to preset sizes.

  • How to resize af:commandImageLink icon

    Hi,
    I would like to resize the icon of the commandImageLink.
    Of course I could directly resize the source image, but I would like to do it programmatically.
    I tried setting the width and height of the commandImageLink, but they do not affect the icon size.
    I also tried sorrounding it with a <div> and setting the div height and width, but this didn't work either.
    Can someone help me ?
    Thanks.

    You can do it for single commandImageLink too..
    define in you css skin like
    af|commandImageLink.AFMyCommandImageSize::image{
    width: 1.0px;height: 1.0px;
    and in styleClass property of your af:commandImageLink [one you want to resize]:
    styleClass="AFMyCommandImageSize"
    Amit

  • How to resize to fit paper size? I have Envy 7640 printer.

    I accidentally was able to resize a photo the other day to fit the paper.  Now when I want to do it, I can't figure out how to do it.    I have a Mac.  Thank you in advance.

    Hi , Welcome to the HP Forums! I understand that you are wondering how to resize to fit paper size, with your HP Envy 7640 on a Mac. I am happy to help! What version of the Mac Operating System are you using? If you do not know the Operating System you are using, please visit this website. Whatsmyos. What is the name of the program you are printing from? In the meantime, please see this article, OS X Mountain Lion: Scale a document to fit your printer’s paper, as I am sure the settings are universal between Mac OS X Operating Systems. Hope this works!  “Please click the Thumbs up icon below to thank me for responding.”

  • Resizing an imageIcon of a jLabel on a jLayeredPane

    I have 2 questions, somewhat related:
    Question 1:
    I have a jLayeredPane with a number of jLabels of imageIcons.
    I need to resize the top imageIcon when I move a slider. To get the top component on the jLayeredPane, I use:
    Component topComponent = layeredPane.getComponent(0);However, I can't figure out how to get the image IN the jLabel IN the Component to set its size. Any ideas?
    Question 2:
    Also, it is easy to add items to a jLayeredPane. For example:
    layeredPane.add(myLabel);but how do I delete an item?

    Q1:
    Component topComponent = layeredPane.getComponent(0);
    JLabel l=(JLabel)topComponent ;
    Q2:
    layeredPane.remove(layeredPane.getIndexOf(myLabel));
    Regards,
    Stas

  • How to resize a label automatically?

    Hi;
    I have a form and a label in it. That label should resize itself to fit the text in it. And after this resizing my code resize the form so that label fits in the form. The problem is, I dont know how to resize the label. (I dont mean label.setSize())
    I can try it with taking length of the text inside and calculate the length. But some characters are bigger than others. (for example: I and M) So there can be mistakes. Is label supporting such a thing?

    I don't know what you are talking about. As far as I can tell, the default behaviour for a JLabel is to resize itself to fit the text inside it. That's certainly how mine behave. So if yours don't do that then remove some code until they do.
    The exception is if they don't have enough room to expand because they're taking up the whole width of the panel they are in. You asked how to resize the form so that there will be room? I don't think that's a good idea. Windows that change their size are an annoying UI feature, which is why you rarely see them. And what happens when your form resizes itself to be three times as wide as your screen?
    However you might consider in future posting Swing questions in the Swing forum so you don't have to take advice from mediocre Swing programmers like me.

  • How to resizing wallpaper on homescreen

    How to resizing wallpaper on homescreen on the iPad Mini? As soon as you use the walpaper for homescreen it does not fit. On the screen says "Move and Scale" I did but comes back like you are zooming.

    Hi, Jochie. 
    Thank you for visiting Apple Support Communities.
    You may find the information in this article helpful.
    iOS 7 uses a parallax effect to create the perception of depth on your Home screen and elsewhere. When this feature is on, you may notice that your:
    Wallpaper, icons, and alerts shift slightly as you move your phone.
    When setting a wallpaper in Settings > Wallpapers & Brightness, the photo or image will be slightly zoomed and cannot be scaled to fit to the screen.
    You can change this behavior by enabling Settings > General >Accessibility > Reduce Motion.
    Note: If zoomed, you will need to rescale your wallpaper to fit to the screen.
    iOS 7: How to reduce screen motion
    http://support.apple.com/kb/HT5595
    Cheers,
    Jason H.

  • CheckBox - how to set custom icons

    I need to figure out how to set a custom icon for a selected checkbox via the UIManager
    This works for un-selected checkboxes:
    UIManager.put("CheckBox.icon", new ImageIcon("../images/unselectedCheckBoxIcon.png")));
    how to change the icon when it is selected?
    I don't know the UIManager property for a selected checkbox icon and using the setSelectedIcon on the checkbox object it self does not seem to work:
    checkbox.setSelectedIcon(new ImageIcon("../images/selectedCheckBoxIcon.png"));
    Thanks for any ideas how to solve this!!

    Hi mKorbel, many thanks for your suggestion!
    For anyone interestsed,
    here is a small example of how I made it working.
    Unfortunately I cannot attatch the icon images that I use, but the selected icon can be downloaded from here, resize it to 32*32, remove the cross in the middle to save it as the unselected icon.
    * Modified sample code taken from here
    * http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizingJCheckBoxCheckMarkIcon.htm
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.net.URL;
    import javax.swing.AbstractButton;
    import javax.swing.ButtonModel;
    import javax.swing.Icon;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    public class IconCheckBoxSample {
      public static void main(String[] args) {
            JFrame frame = new JFrame("Iconizing CheckBox");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Icon checked = new CheckBoxIcon();
            Icon unchecked = new CheckBoxIcon();
            JCheckBox aCheckBox1 = new JCheckBox("Pizza", unchecked);
            aCheckBox1.setSelectedIcon(checked);
            JCheckBox aCheckBox2 = new JCheckBox("Calzone");
            aCheckBox2.setIcon(unchecked);
            aCheckBox2.setSelectedIcon(checked);
            Icon checkBoxIcon = new CheckBoxIcon();
            JCheckBox aCheckBox3 = new JCheckBox("Anchovies", checkBoxIcon);
            JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust", checked);
            frame.setLayout(new GridLayout(0,1));
            frame.add(aCheckBox1);
            frame.add(aCheckBox2);
            frame.add(aCheckBox3);
            frame.add(aCheckBox4);
            frame.setSize(300, 200);
            frame.setVisible(true);
    class CheckBoxIcon implements Icon {
      public void paintIcon(Component component, Graphics g, int x, int y) {
        AbstractButton abstractButton = (AbstractButton)component;
        ButtonModel buttonModel = abstractButton.getModel();
        if(buttonModel.isSelected()) 
            g.drawImage(createImage("../images/selectedCheckBoxIcon.png","info"), x, y, component);
        else
            g.drawImage(createImage("../images/unselectedCheckBoxIcon.png","info"), x, y, component);
      public int getIconWidth() {
        return 33;
      public int getIconHeight() {
        return 33;
      protected static Image createImage(String path, String description) {
            URL imageURL = CheckBoxIcon.class.getResource(path);
            Image icn = null;
            if (imageURL == null) {
                if(null==icn){
                    //System.out.println("path: "+path);
                    icn = new ImageIcon (CheckBoxIcon.class.getResource(path.replace("..",""))).getImage();
                    if(null!=icn)
                        return icn;
                    else{ 
                        System.err.println("Resource not found: " + path);
                        return null;
                 return null;
            } else {
                return (new ImageIcon(imageURL, description)).getImage();

  • How can i move icons from one screen to the other?

    How do i move icons from one page to another?

    Tap and hold any icon for a couple of seconds. They'll all start to wobble. Tap and drag the one you want to move to the side of the screen and the next screen will appear; drop the icon on whatever screen you want it on.
    You can also do it quicker in iTunes - connect the phone and click the Apps tab in the main window. You'll see a picture of your phone with the apps laid out. Just click and drag

  • How to make an icon image using Photoshop

    I found out how to do this recently so I decided that I wanted to make a tut for those who don't know how to make an icon image. This icon image is for the libraries tab on your computer. Under the libararies tab there is Music, Pictures, Documents, and Photos
    First you will need the ICO (Icon image format) Format extension for photoshop which can be downloaded here:
    http://www.telegraphics.com.au/svn/icoformat/trunk/dist/README.html
    The download link and tutorial on how to install it is all in the link above.
    Once you have that all set you can now launch photoshop to create your icon image. Once you have launched it, create a new document with the size as in the image below.
    [IMG]http://i1297.photobucket.com/albums/ag25/dusty951512/256x256_zpsbf3dcf8e.png~original[/IMG]
    Create the image you want. I used a simple one by using the custom shape tool by pressing "U" on your keyboard and with the
    basic blending options.
    [IMG]http://i1297.photobucket.com/albums/ag25/dusty951512/IconImage_zpsd788c709.png~original[/IMG]
    The reason why the image is pixelated is because it is an icon image. Since the image is only 256x256 pixels when you zoom in on it that will get you the pixely result. The reason why I zoomed  in is so you can see it. But don't worry this is no the end result. Just continue reading and you will see.
    So once you have created the icon go ahead and press
    file>save as>(under format choose the ICO)>and choose the name that you want to name it. And save it in your C: drive. You will see why to save it in your C: drive in a sec.
    [IMG]http://i1297.photobucket.com/albums/ag25/dusty951512/SampleicoPic_zpsd252bfba.png~original[/IMG]
    So now that you have created the icon and saved it now you can create the new library.
    [IMG]http://i1297.photobucket.com/albums/ag25/dusty951512/NewLibrary_zps8ca703b2.png~original[/IMG]
    Name the library whatever you want I named it "Sample" for tutorial purposes. Notice how it gives you a default boring icon image for your library.
    [IMG]http://i1297.photobucket.com/albums/ag25/dusty951512/Sample1_zpsb5472840.png~original[/IMG]
    So now once you have created and named your library now it is time to get the icon image into place.
    Go to computer/c: Drive/users/YOU/ And now once you have reached this area you will need to access a hidden folder named "appdata" to do so press "Alt" then a menu bar will show. Click
    tools>folder options>and view. Find the option to view hidden folders then press apply then ok. Now we shall continue so AppData>Roaming>Microsoft>Windows>Libraries
    Now you should see all the libraries including the one you just created.
    [IMG]http://i1297.photobucket.com/albums/ag25/dusty951512/showhiddenfolder_zpsad4a3c94.png~orig inal[/IMG]
    [IMG]http://i1297.photobucket.com/albums/ag25/dusty951512/Libraries_zpsf6243bc0.png~original[/IMG]
    Once you have reached your destination then open a new text document with notepad and drag the library you just created in notepad. The result should look like this:
    [IMG]http://i1297.photobucket.com/albums/ag25/dusty951512/Notepad_zps251a86f0.png~original[/IMG]
    once you have reached this point click at the end of the second to last line down then press enter and enter in this information
    <iconReference>c:\"NAME OF ICO FILE YOU CREATED IN PS".ico</iconReference>
    Example:
    [IMG]http://i1297.photobucket.com/albums/ag25/dusty951512/iconreference_zps1c1a3eca.png~origina l[/IMG]
    Once you have entered that information go to file>save and the icon image should appear on the library you created.
    [IMG]http://i1297.photobucket.com/albums/ag25/dusty951512/Finished_zps267f893a.png~original[/IMG]
    Now you are officially finished. Go and spread the news and joy. Bye for now
    -Dusty951512

    It is Windows only because all those screen shots are exclusively Windows, the file structure and paths do not resemble those of the Mac in the least.  As a Mac user, there's nothing I could take from your tutorial.  Sorry, 
    No drives named with letters like C: on the Mac, for instance.  No backward slashes either, ever.  No such paths either.  No "Notepad" on the Mac, we use TextEdit; but such a text editor is not remotely needed on the Mac to make and/or edit icons.  Etc.
    Those folders are not even called "Libraries" on the Mac…  Nothing resembling your tutorial at all.
    The icons in the Finder's Sidebar are not customizable at all in recent version of OS X.
    =  =  =
    You can edit any post of yours only until someone replies to it.  At this time your post is not editable by you any longer.

  • I can't figure out how to resize my picture for my home or lock screens.

    I can't figure out how to resize my picture for my home or lock screens. It shouldn't be his difficult. Any suggestions? I'm running the latest software. Thanks.

    whichever app store you are connecting to, hyou need a credit card with an address in that country. Also, itunes gift cards must be in local currency too.
    If you are in japan, you need to use the japan app store

  • How to resize a jpg/gif file to a fix size using jimi ?

    I have search from the web and didn't find any example on doing this.
    Can any one give example on how to resize a jpg image. let say 120x240
    to a fixed size 40x40 ?
    thank you

    Hi.
    When you got that image in form of a file, just load it and invoke the image's getScaledInstance(...)-method.
    Here's how it could work:
    import java.awt.*;
    public class Test {
    public static void main(String[] argv) {
      // define where the image comes from:
      URL toImage = new URL("file:/C:/test.jpg");  // or the like
      // get the image:
      Image image = Toolkit.getDefaultToolkit().createImage(toImage);
      // scale the image to target size (40x40 here):
      Image smallImage = image.getScaledInstance(40, 40, Image.SCALE_DEFAULT);
      // you might want to do other things like displaying it afterwards
    }HTH & cheers,
    kelysar

  • TS3274 How do I move icons from one screen to another?

    How do I move icons from one screen to another. 

    Hold down on one icon until they start shaking, then drag the icon to the edge of the screen, and the screen should slide to the next one, you can then drop it on that screen or drag it again to the edge to move to the next screen.
    Page 21 of the user guide explains this under Arranging Apps:
    http://manuals.info.apple.com/en/ipad_user_guide.pdf

Maybe you are looking for