• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Image resizing in Java

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am totally new in Java. My teacher told me to develop a simple project which includes ADD,EDIT,DELETE etc features. I have done successfully all the features but when I add any image it doesn't fit into the jLabel which I take into a Desktop Pane. It displays as its actual size not according to the jLabel size. I tried a lot with the help of internet but its not working. Here is the code I've done so far,



I know it is a newbie type question and I am sorry for that but I really tried a lot but can't figure out what is the problem and how to solve it. Anyone please help me?
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the image getting scattered or distorted?
 
Rifa Ahmed
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, its not scattered or distorted. It just doesn't fit into the jLabel. It displays as its actual size, big or small.
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show remaining part of your code.
 
Rifa Ahmed
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I don't know what exactly do you want. So I am going to show you the main feature's code of my project,
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Database connectivity part is not important with respect to this issue.  The main thing what we would like to see is how you are initializing the frame/panel etc.  For e.g. what is the size of the JLabel that is showing the picture.
 
Saloon Keeper
Posts: 14264
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a few problems with your code:

  • Use conventional variable names. It should be personImage, not person_image.
  • Why is person_image a byte array and not a BufferedImage?
  • Your listener does way too much. Split it up in method calls that handle each concern separately:
  •  
    Rancher
    Posts: 3278
    30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    but when I add any image it doesn't fit into the jLabel



    That isn't how Swing works.

    A Swing component determines its preferred size based on the properties of the component. So when you create a button or checkbox or label. The preferred size of the component is determined by the size of the text and/or Icon you add to the component.

    So when you add a label to the frame and then pack the frame the size of the frame will be the size of the label.

    If the user then resizes the frame, the image will not grow/shrink. If you need this functionality then you need to create a custom component to paint the image and dynamically scale the image as the size of the component changes.

    For example you can check out the Background Panel. It is a custom panel that displays an image as a background. You can then add components on top of the image. You can check out the logic for the "scaled" background to see how to do the custom painting.
     
    Swastik Dey
    Bartender
    Posts: 2270
    20
    Android Java ME Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Right.  But I believe using a null layout with predefined size of components should also work.
     
    Rifa Ahmed
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    OK will you please look at this link kindly? Here in the 1st image is the size of the jLabel into which I want to display the image but when I click any stored data from jTable it gives me result like 2nd,3rd image.

    I am totally confused that how can I make you understand what exactly I wanted. I think now I can make myself clear that what I wanted? :/
     
    Rifa Ahmed
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry for my previous reply. Here is the link,

    webpage
     
    Rob Camick
    Rancher
    Posts: 3278
    30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    But I believe using a null layout with predefined size of components should also work.



    No it won't. The functionality of a JLabel is to paint the image at its actual size.

    As a side issue, you should NOT be using a null layout. Swing was designed to be used with layout managers.
     
    All of life is a contant education - Eleanor Roosevelt. Tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic