Results 1 to 12 of 12
  1. #1
    SWEngineer is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default 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.

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    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...

    Java Code:
    public class PicturePanel() {
    
         public PicturePanel(String filePath) {
              JLabel label = new JLabel();
              ImageIcon icon = new ImageIcon(filePath)
              label.setIcon(icon);
              this.add(label);
         }
    }
    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.

  3. #3
    SWEngineer is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default

    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.

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    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.

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    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)

  6. #6
    SWEngineer is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default

    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.

  7. #7
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    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?

  8. #8
    SWEngineer is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default

    Thanks for your reply.

    Sorry, I'm still not getting the idea...

  9. #9
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    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...

    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();
    }
    I would still recommend going down to one JPanel instance though and "NOT" replacing it but rather updating the ImageIcon being displayed.

  10. #10
    SWEngineer is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default

    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.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by SWEngineer View Post
    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?
    If you're talking about your createShowGUI method in your first post, then yes, as Stormy has already pointed out, it is not working since you create a JFrame but do nothing with it other than add a menu and display it. It has no "GUI" added to it.

  12. #12
    SWEngineer is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

Similar Threads

  1. displaying image into screen !
    By burningflower12 in forum AWT / Swing
    Replies: 14
    Last Post: 11-16-2009, 02:02 AM
  2. Displaying image in same jsp
    By SreenivasGurramkonda in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-08-2008, 06:01 AM
  3. A question about displaying an image...
    By SpaceY in forum New To Java
    Replies: 0
    Last Post: 08-24-2008, 06:50 PM
  4. Displaying an image in a group
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:01 PM
  5. Displaying different image types
    By splinter64uk in forum AWT / Swing
    Replies: 1
    Last Post: 12-05-2007, 08:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •