Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:39:01+00:00 2026-06-17T22:39:01+00:00

i am using Jtree for image list and displaying the selected image, but problem

  • 0

i am using Jtree for image list and displaying the selected image, but problem is that if the image size is larger than the canvas size(i.e. 300 X 300) it displays only top left of the image, how to display the whole image in fixed size canvas?

i am using following code:

private void displayImage(File file) {

    try 
    {
        BufferedImage image = ImageIO.read(file);
        ta.Picture = image;
    }
    catch (Exception e) 
    {}

    Graphics g = ta.imageCanvas.getGraphics();
    g.clearRect(0, 0, 300, 300);
    g.drawImage(ta.Picture, 00, 00, this);

} // displayImage

public void valueChanged(TreeSelectionEvent e) 
{
    // TODO Auto-generated method stub
    FileTreeNode node = (FileTreeNode) tree.getLastSelectedPathComponent();

    if (node.isLeaf()) 
    {

        currentFile = node.file;

        File ff = new File("F:/images_blue/" + currentFile.getName());
        displayImage(ff);

    } else
        currentFile = null;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:39:03+00:00Added an answer on June 17, 2026 at 10:39 pm

    I’d just like to point out that…

    Graphics g = ta.imageCanvas.getGraphics();
    g.clearRect(0, 0, 300, 300);
    g.drawImage(ta.Picture, 00, 00, this);
    

    Is a really bad idea.

    getGraphics may return null and is, at best, simply a snapshot of the last repaint cycle and could be invalidated or dereferenced on the next paint cycle.

    You should be extending from something like JPanel and overriding the paintComponent method. This would allow you to supply a getter and setter as apart of the image management and allow you to produce the scaled image in a self contained and reusable manner

    You check out Perfoming Custom Painting for me information

    And, as per my comment, you might like to check out these previous questions for examples

    • Scale the ImageIcon automatically to label size
    • Jpanel resize on repaint
    • Quality of Image after resize very low — Java
    • Java: maintaining aspect ratio of JPanel background image
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a JTree that is populated from a database. The tree is created
I am using the first example mentioned here for creating my JTree but I
I'm learning my way around Java JTree's but I have this little problem I
I am using a MouseListener to detect double clicks on JTree items. That JTree
When using a JTree , a user object of a DefaultMutableTreeNode can be set.
first i explain the context then the problem. I am using jstree to transform
I am using jsTree for creating a documentation list index. I use JSON to
In Swing when using a JTree/JList/JTable selecting an item changes its background color. Is
I'm using a custom TreeModel for a JTree. I have an issue when I
I've got a JTree which I'm using to display some (unsurprisingly) hierarchical data. Part

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.