Solved

using scrollpane with an image loaded using a filechooser dialog

Posted on 2006-07-04
14
245 Views
Last Modified: 2010-03-31
Hi,

I'm currently working my way through building a swing gui for an application. It contains three panels, with the bottom two panels split vertically. There is also a menu bar at the top of the frame, which currently allows a user to load an image from a directory. The image is displayed on the first panel, which has been set up using BorderLayout and a JScrollPane.

The problem is when the image is larger than the panel. The scroll bars should allow the user to scroll the image horizontally and vertically. However, this is not happening. All I can think is that the scroll bars interpret that the panel is always empty or something, and are not responding to the addition of the image once the application has been started.

Is there a way to either automatically scale the loaded image to the panel, and therefore no longer require scroll bars, or get the scroll bars to work correctly when an image is loaded?

Any help or advice would really be appreciated.

Thanks
0
Comment
Question by:nhay59
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17037937
After loading the image, try calling

validate();

on the panel
0
 
LVL 35

Expert Comment

by:girionis
ID: 17037949
To get the scrolling to work correctly have a look here: http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html where it has an exampel with an image and scroll pane.
0
 

Author Comment

by:nhay59
ID: 17038018
Hi,

Thanks for the replies. I've tried adding validate() after loading the image to the panel, but this has unfortunately had no effect upon the swing gui. I've also been through the Sun tutorial, and can't see anything that I've missed in implementing the JScrollPane.

I think this must be something to do with the panel starting off without any image etc, and then adding one, by the user, as soon as the application has begun.

Any ideas or help?

Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:girionis
ID: 17038099
Can you post some sample code that demonstrates the problem?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17038105
How are you loading the image after the chooser bit? I would recommend

label.setIcon(new ImageIcon(fc.getFile().toURL()));
validate();

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17038112
(the 'label' would be the scroll pane's child)
0
 

Author Comment

by:nhay59
ID: 17038145
Hi,

Thanks for the replies.

The actionPerformed method for the menu is as follows,

public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if (source == openItem) {
      JFileChooser chooser = new JFileChooser();
      chooser.setCurrentDirectory(new File("."));

      chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
        public boolean accept(File f) {
          String name = f.getName().toLowerCase();
          return name.endsWith(".gif") || name.endsWith(".jpg")
              || name.endsWith(".jpeg") || f.isDirectory();
        }

        public String getDescription() {
          return "Image files";
        }
      });

      int r = chooser.showOpenDialog(this);
      if (r == JFileChooser.APPROVE_OPTION) {
        String name = chooser.getSelectedFile().getAbsolutePath();
        panel.loadImage(name);//panel for displaying image
        //panel.validate();
      }

and the loadImage method is,

public void loadImage(String name) {
    Image loadedImage = Toolkit.getDefaultToolkit().getImage(name);
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(loadedImage, 0);
    try {
      tracker.waitForID(0);
    } catch (InterruptedException e) {
    }
    image = new BufferedImage(loadedImage.getWidth(null), loadedImage
        .getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    g2.drawImage(loadedImage, 0, 0, null);
    repaint();
  }

Thanks for the help.
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 75 total points
ID: 17038166
Can you try to add your panel to a JScrollPane and then add this JScrollPane to your gui?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 75 total points
ID: 17038177
No - that won't work. You would need to override the paintComponent method of the panel to do it like that or it'll disappear

I would make the code i just posted the sole code in loadImage
0
 
LVL 35

Expert Comment

by:girionis
ID: 17038208
Yes you would need to override it and simply call the paintComponent of the superclass. It should work.
0
 
LVL 92

Expert Comment

by:objects
ID: 17039239
you don't need to override paintComponent(), what you have posted looks fine.
try caslling revalidate() on the scrollpane after updating the image.
0
 
LVL 92

Accepted Solution

by:
objects earned 350 total points
ID: 17039348
you'll also need to set the preferred size of the component in the scroll pane (to the size of the image) before calling revalidate.
0
 

Author Comment

by:nhay59
ID: 17039378
Hi,

Thanks for the replies. I've added the suggestions, regarding preferred size and revalidate, and the scroll panel is now scrolling correctly. However, as is usually the case another problem has arisen. I shall have to open this as a new question  in order to ensure that points are allocated correctly for this question.

Thanks to all for their help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17040540
:-)

>>I've added the suggestions, regarding preferred size and revalidate, and the scroll panel is now scrolling correctly.

Try covering it with another window and then uncovering it
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question