nhay59
asked on
using scrollpane with an image loaded using a filechooser dialog
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
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
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.
ASKER
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
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
Can you post some sample code that demonstrates the problem?
How are you loading the image after the chooser bit? I would recommend
label.setIcon(new ImageIcon(fc.getFile().toU RL()));
validate();
label.setIcon(new ImageIcon(fc.getFile().toU
validate();
(the 'label' would be the scroll pane's child)
ASKER
Hi,
Thanks for the replies.
The actionPerformed method for the menu is as follows,
public void actionPerformed(ActionEven t evt) {
Object source = evt.getSource();
if (source == openItem) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirector y(new File("."));
chooser.setFileFilter(new javax.swing.filechooser.Fi leFilter() {
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(thi s);
if (r == JFileChooser.APPROVE_OPTIO N) {
String name = chooser.getSelectedFile(). getAbsolut ePath();
panel.loadImage(name);//pa nel 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(loadedIma ge, 0);
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
}
image = new BufferedImage(loadedImage. getWidth(n ull), loadedImage
.getHeight(null), BufferedImage.TYPE_INT_RGB );
Graphics2D g2 = image.createGraphics();
g2.drawImage(loadedImage, 0, 0, null);
repaint();
}
Thanks for the help.
Thanks for the replies.
The actionPerformed method for the menu is as follows,
public void actionPerformed(ActionEven
Object source = evt.getSource();
if (source == openItem) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirector
chooser.setFileFilter(new javax.swing.filechooser.Fi
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(thi
if (r == JFileChooser.APPROVE_OPTIO
String name = chooser.getSelectedFile().
panel.loadImage(name);//pa
//panel.validate();
}
and the loadImage method is,
public void loadImage(String name) {
Image loadedImage = Toolkit.getDefaultToolkit(
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(loadedIma
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
}
image = new BufferedImage(loadedImage.
.getHeight(null), BufferedImage.TYPE_INT_RGB
Graphics2D g2 = image.createGraphics();
g2.drawImage(loadedImage, 0, 0, null);
repaint();
}
Thanks for the help.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes you would need to override it and simply call the paintComponent of the superclass. It should work.
you don't need to override paintComponent(), what you have posted looks fine.
try caslling revalidate() on the scrollpane after updating the image.
try caslling revalidate() on the scrollpane after updating the image.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
:-)
>>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
>>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
validate();
on the panel