Results 1 to 12 of 12
Thread: Displaying an image
- 01-27-2010, 07:01 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
Displaying an image
Hello,
I have created a GUI with the menus "File" and "Help".
"File" has a menu item, "Open Image...".
I'm just having an issue DISPLAYING an image at the GUI.
I have my "Open Image..." menu item as follows:
openImageMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
fileChooser = new JFileChooser();
int sts = fileChooser.showOpenDialog(AbdMedImgEditor.this);
if(sts == fileChooser.APPROVE_OPTION)
{
File selectedFile = fileChooser.getSelectedFile();
picturePanel = new PicturePanel(selectedFile.getAbsolutePath());
}
}
}
);
And, the code that is responsible for displaying the image is:
public class PicturePanel extends JPanel
{
private ImageIcon picture;
PicturePanel(String fileName)
{
picture = new ImageIcon(fileName);
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(picture.getImage(),0,0,this);
}
}
And, finally the code that displays the creates and shows the GUI is as follows:
public static void createShowGUI()
{
JFrame frame = new JFrame("Img Editor");
AbdMedImgEditor abdEditor = new AbdMedImgEditor();
frame.setJMenuBar(abdEditor.createMenuBar());
frame.setSize(500, 500);
frame.setVisible(true);
}
Thanks.
- 01-27-2010, 08:43 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Instead of overriding paint, why not just add a JLabel to the Picture Panel and set the label's Image Icon to the image you want to display?
I haven't done this in awhile but I believe its something like this...
Another question... Are you sure you are adding the new panel to the JFrame? If you just override the variable, you are not really adding it to the existing JFrame and I don't see this being done anywhere.Java Code:public class PicturePanel() { public PicturePanel(String filePath) { JLabel label = new JLabel(); ImageIcon icon = new ImageIcon(filePath) label.setIcon(icon); this.add(label); } }
- 01-27-2010, 09:04 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
Thanks for your reply "Stormy Waters".
Yes, this is the issue I"m facing now. How can I add the pane to the JFrame?
Thanks.
- 01-27-2010, 09:52 PM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
That depends on your design and purpose for your GUI application. I would recommend just one panel object and to reuse it to display the image you select by updating the displayed image icon. Again its been awhile but here's the jist of it.
Java Code:public class PicturePanel() { private JLabel label; public PicturePanel() { super(); initGUI(); } private void initGUI() { this.add(getLabel()); } private JLabel getLabel(){ if (label == null) { label = new JLabel(); } } public void setDisplayedImage(String filePath) { getLabel().setIcon(new ImageIcon(filePath)); //Unsure which one refreshes so I wrote them all this.repaint(); this.invalidate(); this.validate(); } }
With this you should be able to add this once to your JFrame, then just update the object for whatever Image you want to display.
- 01-27-2010, 09:55 PM #5
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Here's a tutorial if you do not understand how to add a Panel to a JFrame
Using Top-Level Containers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
- 01-27-2010, 10:11 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
Thanks so much once again "StormyWaters".
First, I just have a few notes regarding your code:
public class PicturePanel() {....
Here, we shouldn't have the ( ) after PicturePanel, right, since this is a class and not a method.
Secondly, in
private JLabel getLabel(){....
Shouldn't we have a "return" Statement.
Now, the main issue is not with adding the panel to the frame as a cocept, but with how to do that in my code which seems I'm stuck in a way or another in.
I have sent you the whole code as a private message so you can see what I mean. Provided that it works but the "Open Image..." {openImageMenuItem} feature is the only thing I'm stuck in.
I'm sure it is a simple thing but not aware of.
I really appreciate your kind help.
- 01-27-2010, 10:23 PM #7
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Sorry for the few errors in the code as I wrote it up in the reply box off the top of my head. Just fix the few errors you noticed and it should work.(ie return the label, remove the parenthesis)
I'm not going to run your code but I will give you help. Just by glancing at it, if you are going to create new PicturePanel's in the actionPerformed method, make sure to remove the previous one if it exists.
After you add the panel call the validate, invalidate and repaint on the JFrame to make sure its updated. I don't really remember which one does what you need.
Are there errors in the actionPerformed when you add the panel?
- 01-27-2010, 10:49 PM #8
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
Thanks for your reply.
Sorry, I'm still not getting the idea...
- 01-27-2010, 10:54 PM #9
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
When you replace panels on a JFrame, the entire JFrame needs to be relayed out. To do this there is are calls you can make on the JFrame.
Try this in your actionPerformed...
I would still recommend going down to one JPanel instance though and "NOT" replacing it but rather updating the ImageIcon being displayed.Java Code://APPROVE_OPTION ---> A file or folder selected if(status == fileChooser.APPROVE_OPTION) { selectedFile = fileChooser.getSelectedFile(); picturePanel = new PicturePanel(selectedFile.getAbsolutePath()); add(picturePanel,BorderLayout.CENTER); repaint(); invalidate(); validate(); }
- 01-27-2010, 11:00 PM #10
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
Thanks so much.
I tried it out, still the same. But I think that the main problem is in the "createShowGUI" method. What do you think?
Thanks.
-
- 01-28-2010, 05:48 AM #12
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
displaying image into screen !
By burningflower12 in forum AWT / SwingReplies: 14Last Post: 11-16-2009, 02:02 AM -
Displaying image in same jsp
By SreenivasGurramkonda in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-08-2008, 06:01 AM -
A question about displaying an image...
By SpaceY in forum New To JavaReplies: 0Last Post: 08-24-2008, 06:50 PM -
Displaying an image in a group
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 08:01 PM -
Displaying different image types
By splinter64uk in forum AWT / SwingReplies: 1Last Post: 12-05-2007, 08:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks