Resizing Image when JFrame resized?

I have a JFrame with a JPanel added to the contentPane (BorderLayout.CENTER). In the JPanel is a JLabel (BorderLayout.CENTER) with an ImageIcon set to it. The image is a subimage of a larger image whose width is scaled to fit the current JLabel width (which is dictated by the JPanel width and finally dictated by the JFrame width).
When the user resizes the JFrame, the image of the ImageIcon should be replaced with a new image which has been scaled to fit the new width and that reflects the new portion of the larger source image that is visible.
The JFrame already has a ComponentListener added for retaining the current JFrame size and screen position, but calling my resizeDisplay() method from componentResized() does not seem to have the updated JLabel size. I have added a ComponentListener to the JLabel and called my resizeDisplay() method from componentResized() but it does not seem to have the updated JLabel size either.
How can my application properly react to JFrame resizes so that the method is retrieving the new size values?
Thanks,
Robert Templeton

Its always easier to solve a problem when sample code (not a complete program) is posted with the question.
Maybe I don't understand the question, but when I run the following code the output reflects the size of the label after it has been resized. I'm using JDK1.3 on Window 98.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class TestLabel extends JFrame
     public TestLabel()
          JPanel panel = new JPanel();
          setContentPane( panel );
          panel.setPreferredSize( new Dimension( 200, 200 ) );
          panel.setLayout(new BorderLayout() );
          final JLabel myLabel = new JLabel( new ImageIcon("dukeWaveRed.gif"), JLabel.CENTER );
          panel.add( myLabel );
          myLabel.addComponentListener( new ComponentAdapter()
               public void componentResized(ComponentEvent e)
                    System.out.println( myLabel.getSize() );
     public static void main(String[] args)
          TestLabel frame = new TestLabel();
          frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
          frame.pack();
          frame.setVisible(true);
}

Similar Messages

  • How to set an image on JFrame

    i just want to know that how to set an image on JFrame.
    thanks in advance

    dear moazzam
    why are you coming here in the forms when you have very brillient java certified teacher
    who knows every thing:)
    just mail him and ask this questain from him.
    im sure you will be given a good replay
    take care !

  • Photoshop CC distorted image when zoomed out. Text, edges and Straight lines.

    I get this distorted image when working on a file. When I zoom in its fine. But as soon as I zoom out all the lines, text , and edges start to become distorted. I've checked that all my drivers are up to date. I've re installed photoshop and still the same thing. Any Suggestions. ( files look fine when saved or exported, this just happens while Im working )

    That looks like a Moiré pattern.  Did you scan the image?
    If not, can you tell us what operating system?
    And is your video card driver fully up to date from the maker's website?

  • Problem with display an image on JFrame in Java 6

    I'm trying to display an image on JFrame in this way:
    1.) I'm creating a variable in the class:
        private Image imgSpeaker = null;2.) I'm overiting the paint method:
    public void paint(Graphics g) {
            super.paint(g);   
            g.drawImage(imgSpeaker, 330, 230, null);  
        }3.) In the constructor I'm using this code:
       imgSpeaker = Toolkit.getDefaultToolkit().getImage("D:\\speaker.gif");
            MediaTracker trop = new MediaTracker(this);
            trop.addImage(imgSpeaker,0);
            try {
                trop.waitForID(0);    // waiting untill downloading progress will be complite
            } catch (Exception e) {
                System.err.println(e);
            }        The Image will be displayed on form, but if I catch the window and move it (for example: down as possible)
    the image is not refreshed (repainted). What I'm doing wrong? I've been used this solution in Java 5 and everything
    works perfectly. Any of described methods aren't depricated... Anyone can help me?

    Thanks for your code, I've been tryed to apply your solution (with create a Buffered Image), but it still not working correctly. Firstly, the image isn't loading, and secondly the basic problem is not give in. Why have you been using "bg2d.draw(img, x, y, w, h, frame);" not in the paint method?
    I'm use "Free Design" layout. Below I put on completly code from Netbeans.
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.image.BufferedImage;
    public class ImageTest extends javax.swing.JFrame {
        private Image imgSpeaker = null;
        private BufferedImage bi = null;
        private MediaTracker trop = null;
        public ImageTest() { 
                initComponents();
                trop = new java.awt.MediaTracker(this);
                imgSpeaker = java.awt.Toolkit.getDefaultToolkit().getImage("D:\\speaker.gif");
                try {
                    trop.addImage(imgSpeaker, 0);
                    trop.waitForID(0);
                } catch (java.lang.Exception e) {
                    java.lang.System.err.println(e);
                bi = new java.awt.image.BufferedImage(100, 100, java.awt.image.BufferedImage.TYPE_INT_RGB);
                java.awt.Graphics2D bg2d = (java.awt.Graphics2D) bi.createGraphics();
                trop.addImage(bi, 1);
    //            java.io.File f = new java.io.File("D:\\speaker.gif");
    //            try {
    //                bi = javax.imageio.ImageIO.read(f);
    //            } catch (IOException e) {
    //               java.lang.System.err.println(e);
        public void paint(Graphics g) {
    //      super.paint(g);
           try {                                                   
                trop.waitForAll();                                 
            } catch (Exception e) {
                System.err.println(e);
            g.drawImage(bi, 50, 100, imgSpeaker.getHeight(this), imgSpeaker.getHeight(this), this);
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
        private void initComponents() {
            jButton1 = new javax.swing.JButton();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            jButton1.setText("jButton1");
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(283, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addGap(44, 44, 44))
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(252, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addGap(25, 25, 25))
            pack();
        }// </editor-fold>
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new ImageTest().setVisible(true);
        private javax.swing.JButton jButton1;
    }

  • Firefox doesnt load an image when *lots* of tabs are open

    Hi,
    An old website I created has a problem displaying a very large background image when *loads* of tabs are open (which makes me think its something to do with memory).
    An example page of the site in question is:
    http://www.southbankcentre.co.uk/about-us
    The white background image (which is super large for reasons not to discuss now) will show fine when, say, up to 10 tabs are open. However, if you have loads and loads of tabs open, this image doesn't display/render (even though firebug says it has).
    Some info about the image:
    http://www.southbankcentre.co.uk/sites/all/themes/southbank-centre/images/template-3-col.png
    79.6kb
    940px by 23000px (super huge I know)
    The problem is when it doesnt load, the copy is hard to read (as you can imagine).
    This doesnt seem unique to my computer. It happens to other peoples firefox browsers (and they have slightly different versions and different plug-ins).

    I'm not having any problems on that page with over 80 tabs open and the number of tabs shouldn't matter.
    Did you check if you can reproduce it with a clean new profile without extensions?
    Start Firefox in [[Safe Mode]] to check if one of your add-ons is causing your problem (switch to the DEFAULT theme: Tools > Add-ons > Themes).
    * Don't make any changes on the Safe mode start window.
    See [[Troubleshooting extensions and themes]] and [[Troubleshooting plugins]]

  • My mac will not show images when on the internet?

    My mac will not show images when on the internet?  I think I have inadvertently changed some settings but nor sure which one.

    I am going to assume you are using Safari here. If so then go to the Apple menu bar and hit Safari-> Preferences.  Under the Apperance tab there is a check box that is called "Display images when the page opens".  Check if that is unticked.

  • Inserting an image to Jframe and save it ,  in SQL database

    Hai guys,
    I just wanna know about the inserting image in to a Jframe (in netBeans) it will be a grate help if you tell me the way to call a image to Jframe through a Button click and save the image in MySQl database.
    Is anyone has a idea about this task, please tell me
    Thanks,

    Image class should work to get the data, then add it to a Canvas object, then you can save the bytes from the Image into an SQL statement through JDBC.
    The implementation details will vary based on which approch and database you select.

  • Why do I get ?'s instead of images when e-mails are received?

    Why do I get a question mark instead of an image when I receive e-mails?

    When you receive email in HTML format (that is, as a web page), the message will usually contain links to images hosted on a remote server. When you display those images, the sender of the message will know that you've read his message, when you read it, who your ISP is, and approximately where you are. You may regard this as an invasion of your privacy. If the message is spam, then your address has been confirmed as valid and you've been confirmed as someone who opens spam messages. That will ensure you get even more spam.
    If you're using the Mail application, you can prevent that information from being sent out. Select
    Mail ▹ Preferences ▹ Viewing
    from the menu bar and uncheck the box marked
    Display remote images in HTML messages
    You'll still be able to display images when you want to by clicking the button marked
    Load Images
    in the top right corner of the message window. If you do want the images to load automatically, check the box.

  • PC/64/Windows 7/XIStandard recently installed/unable to put footer into document because I can't move to the bottom of the image when in footer application mode.  Display is at recommended 1920x1080.  Is there a solution or is it a flaw in the program?

    PC/64/Windows 7/XIStandard recently installed/unable to put footer into document because I can't move to the bottom of the image when in footer application mode.  Display is at recommended 1920x1080.  Is there a solution or is it a flaw in the program?

    Don't have a direct answer. But did you install the updates. They might resolve the problem.

  • How can I improve quality of image when burning a movie to a DVD

    How can I improve quality of image when burning a movie to a DVD?

    What resolution are you using?  If the DVD is to be playable on an ordinary DVD player, only ordinary TV resolution is supported.  You need Blue ray for HD.
    Geoff.

  • Using Lightroom 5.5 and Photoshop CC2014. Images no longer open in Photoshop when using "edit in" command. Photoshop opens but no image when trying to move between LR and PS. Any suggestions?

    Using Lightroom 5.5 and Photoshop CC2014. Images no longer open in Photoshop when using "edit in" command. Photoshop opens but no image when trying to move between LR and PS. Any suggestions?

    1. You have allowed Apple to auto-upgrade your Mac
    Turn off auto-update here:
    Menu > Apple > System Preferences > App Store
    2. The Icons on your dock are Aliases not real apps
    They point to where the apps really are which is:
    For Pages 5.2.2 in your Applications folder
    For Pages '09 in your Applications/iWork folder
    3. You are alternately opening documents randomly with either version of Pages
    Both Pages have the same file extension .pages and there is no certainty as to which version opens them when you double click on a file.
    right click on the file and choose which version you want to open it
    4. Pages '09 can not open Pages 5 files
    Pages 5 can not open Pages '08 files, and will convert and change Pages '09 and Word files.
    It will also auto save opened files into its own format.
    You can export these back to Pages '09 if you need to:
    Menu > File > Export > Pages '09
    5. Yes Pages 5.2.2 is a marked downgrade
    Pages 5.2.2 has a few improvements but has had over 110 features removed and is buggy.
    Sooner or later you will not be able to open a file or have it damaged in some way and it has a complex obscure file format largely incompatible with everything else, so you will not be able to rescue the contents of your file. If Pages or some third party server/eMail don't trash your file, eventually Apple will do it for you as it did when it released Pages 5 last September. I recommend using Pages '09 for the time being whilst you look for viable alternatives, some are here already and some are on the way.
    For further information about what has happened:
    http://www.freeforum101.com/iworktipsntrick/viewforum.php?f=22&sid=0882463378700 abf43a0f2433506bbbc&mforum=iworktipsntrick
    Peter

  • "Insufficient data for an image" when saving a file

    I'm running Acrobat 9.5.0 on a Macbook Pro running OS X 10.6.8.
    After scanning a document from my Canon MX882, or opening and editing an existing pdf I get the following error when I try to save the file:
    Insufficient data for an image
    When I try to save the file again, I get this:
    The document could not be saved. There was a problem reading this document (109).
    Sometimes the error also has (8) instead of (109).
    When this occurs, the thumbnail of the page appears correct in the left pane; however, the content of the page itself is entirely or partially missing.
    Any suggestions would be appreciated.
    -c

    I am also having this problem, especially with scanned documents. Since I need to download scanned documents in my work this is a big problem!Sometimes I can read but not save the document, sometimes one or two of the pages will be blank from the start. I have the latest version of Adobe Reader XI (11.0.10). I tried reducing the page zoom, a suggested workaround on this page: Acrobat: “Insufficient data for an image” error after updating to 10.1.4 or 9.5.2,  but that did not help at all. FWIW I use a Windows 8.1 PC with 4 GB RAM. I usually download PDFs from the internet using Firefox.

  • How to stop the dock from showing all hidden images when hiding then showing applications

    How to stop the dock from showing all hidden images when hiding then showing applications

    It's not really something to snap a screen shot of. I believe it would need to be a video clip. For instance, when I am working on multiple images in Photoshop, I often need many images open at the same time, but I only have one image viewable and active. The hero image is active on screen while I hide the other images in the dock by hitting the yellow - button on the images. Then I "Hide" Photoshop to do something in the Finder. When I go back to Photoshop, every image that is hidden in the dock will pop open again in the active window, not just the image I was currently working on. I want these images to stay in the dock until I need them, no matter how many times I Hide the Show Photoshop.

  • Problem in loading images when i am connected on company network

    Hi friends, I am using firefox since last 4 months on my windows 8 pro laptop.but since last month I am facing problem in loading images when i am connected on company network but same time it is working fine with ie10. But all these thinks are working well at my home when I am using broadband.

    I don't completely understand your issue. Does this issue occur on 1 network and does not occur on another? Have you tried clearing cache and cookies and making sure your plugins are up to date?
    Many site issues can be caused by corrupt cookies or cache. In order to try to fix these problems, the first step is to clear both cookies and the cache.
    Note: ''This will temporarily log you out of all sites you're logged in to.''
    To clear cache and cookies do the following:
    #Go to Firefox > History > Clear recent history or (if no Firefox button is shown) go to Tools > Clear recent history.
    #Under "Time range to clear", select "Everything".
    #Now, click the arrow next to Details to toggle the Details list active.
    #From the details list, check ''Cache'' and ''Cookies'' and uncheck everything else.
    #Now click the ''Clear now'' button.
    Further information can be found in the [[Clear your cache, history and other personal information in Firefox]] article.
    Did this fix your problems? Please report back to us!
    Please check if all your plugins are up-to-date. To do this, go to the [http://mozilla.com/plugincheck Mozilla Plugin Check site].
    Once you're there, the site will check if all your plugins have the latest versions.
    If you see plugins in the list that have a yellow ''Update'' button or a red ''Update now'' button, please update these immediately.
    To do so, please click each red or yellow button. Then you should see a site that allows you to download the latest version. Double-click the downloaded file to start the installation and follow the steps mentioned in the installation procedure.

  • How stop PS6 from removing the DPI value from an image when using "save for the web"?

    How stop PS6 from removing the DPI-value from an image when using "save for the web"?
    Example:
    - Open a tif image, that contains a dpi value (resolution).
    - Use the splice tool in PS6.
    - Export the slices with "Save for web", as gif-files.
    Then the dpi value is removed, the gif files has no dpi value (it's empty).
    How can we stop PS6 from removing the dpi value when using "save for web"?
    OR:
    When using the slice tool, how can we save the sliced pieces without PS removing the dpi value?

    you can make your art go a little bit over the bounds. or you can make sure your artboart and art edges align to pixels

Maybe you are looking for

  • My Hyperlink button not working in FaceBook

    I created a slide show and a link to my web site and to music site. When I upload the QT file to FaceBook, the buttons are there, but no link. David

  • Need table name for currency of material - Accounting veiw 1

    Hi, Could anyone tell that the currency field that is displayed in MM02 Accounting 1 is taken from which table? Regards, Sankaran

  • Requirement in alternative unit of measure

    Hi, A material is maintained with EA as Base unit of measure and alternative unit of measure is maintained as 100m=1 EA. when MRP  is run, the requirement appears in EA and not meteres. The issuing UOM is maintained as M in plant/storage view. Is the

  • I need to transfer non-purchased music itunes to iphone

    In a nutshell. 2005 I bought a PC and copied my entire CD collection to itunes (1000+ songs). I boughjt an ipod in 2006 and copied all the music to the ipod. I've only ever purchased 1 album from itunes store. The PC died. Dead. Gone forever. I bough

  • Safari 6 not opening .pdf files

    New Mac, fresh install of Mountain Lion When I click to open a .Pdf from a web page,while in Safar, I get a black window Nothing opens in Preview or in Acrobat No option to download The same item opens easily in FireFox into Acrobat My hunch is that