Results 1 to 15 of 15
  1. #1
    xCLARAx is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Exclamation can display image in JFrame?

    i m trying to put image to my GUI application using JFrame.

    can anyone guide me how to?

    thx a lot

  2. #2
    mirage_87 is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Default

    did u try using jlabel........that's perhaps the easiest way i know to add images to yr gui's.....

  3. #3
    xCLARAx is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default

    i tried..but nothing coming out...i also tried ImageIcon but nothing happen =(

  4. #4
    mirage_87 is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.*;
    public class trial extends javax.swing.JFrame{
        public trial() {
            a = new javax.swing.JLabel();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            a.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.jpg"));
            add(a);
            pack();
        }
        
        public static void main(String args[]){
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new trial().setVisible(true);
                }
            });
        }
        
        private javax.swing.JLabel a;
    }

    try this out.......this would add the image to yr frame...
    you can look at group layout if u wanna set the position of the image and size
    Last edited by levent; 07-26-2007 at 08:21 PM. Reason: Code is placed inside [code] tag.

  5. #5
    xCLARAx is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Thumbs up

    okie...

    now application is running properly...

    thank u so much 4 ur help :)

  6. #6
    zahirali is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default hi

    can you explain the code that you placed on the forum... it worked for me too... but i dont fully understand it. thanks in advance.

  7. #7
    zahirali is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    can you explain this line please.

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

    Default

    The API will tell you what this means. Have look here, then come on back if any of it doesn't make sense: JFrame (Java Platform SE 6)

  9. #9
    zifis is offline Member
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Default

    I want to pull the image to my GUI when I click on a button, how can I alter this code to be able to do this? Any ideas? Sorry, I'm sooo new!! Thanks in advance :)

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

    Default

    How much Swing coding have you done? Can you post your attempt at a solution to this (we'd much rather work with your code here, not have you work with someone else's)? Have you gone through the Sun Swing tutorials? If not, most all of your questions will likely be answered there.

  11. #11
    zahirali is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default hi

    i need a brief explination of the code above..... been trying to figure out the logic of it... can someone help me with understanding it ? a few commnets would be nice!

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

    Default

    Really the best place to learn about this and other Swing code is in the Sun Swing tutorials. The link I posted above will get you to the JFrame tutorial. why not have a look there first and then come back if you have any specific questions?

  13. #13
    zifis is offline Member
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Default code

    Fubarable, I studied the Swing tutorials but I'm not very good at it yet since I'm new to Java. This is my code and I want to add the ability to pull a picture when the button is clicked (picture is scanned and put in a share folder by the scanner) and display it on the JLabel if possible... Here is a part of my code: (I'm not sure if it's gonna work or not :D )






    import java.awt.*;
    import java.awt.event.*;
    import java.awt.EventQueue;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;

    public class PullImage {

    public static void main(String[] args throws IOException {

    String path = "C:\\Documents and Settings\\bxu001\\My Documents\\My Pictures\\pic";
    BufferedImage image = ImageIO.read(new File(path));
    JFrame fr = new JFrame();
    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    fr.getContentPane().add(new PullImage().getContent(image));
    fr.setSize(600,600);
    fr.setVisible(true);
    }



    private BufferedImage getImage() {
    String path = "C:\\Documents and Settings\\bxu001\\My Documents\\My Pictures\\pic";

    URL url = getClass().getResource(path);
    BufferedImage image = null;

    try
    {
    image = ImageIO.read(url);
    }
    catch(IOException e) {
    System.out.println("Error occured : " + e.getMessage());

    }
    return image;


    }


    private JScrollPane getContent(BufferedImage image) {
    JPanel panel = new JPanel(new GridLayout(1,0));
    panel.add(new JLabel(new ImageIcon(image)));
    panel.add(new JLabel(new ImageIcon(getImage())));
    return new JScrollPane(panel);






    }
    }





    Thanks!
    Last edited by zifis; 04-03-2009 at 05:24 PM.

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

    Default

    I suggest that you try to create an SSCCE, and then post this.

    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

  15. #15
    zifis is offline Member
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Default code

    Here I go... I hope it comes out right! Thanks Fubarable! :)


    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class PullImage {
    	
    	
    	
    	private JScrollPane getContent(BufferedImage image) {
    		JPanel panel = new JPanel(new GridLayout(1,0));
    		panel.add(new JLabel(new ImageIcon(image)));
    		panel.add(new JLabel(new ImageIcon(getImage())));
    		return new JScrollPane(panel);
    	}
    	
    	private BufferedImage getImage() {
    		String path = "images/picc.jpeg";
    		
    		URL url = getClass().getResource(path);
    		BufferedImage image = null;
    		
    		try 
    		{
    			image = ImageIO.read(url);
    		}
    		catch(IOException e) {
    			System.out.println("Error occured : " + e.getMessage());
    			
    		}
    		return image;
    
    		
    	}
    	public static void main(String[]args) throws IOException {
    		
    		String path = "images/picc.jpeg";
    		BufferedImage image = ImageIO.read(new File(path));
    		JFrame fr = new JFrame();
    		fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		fr.getContentPane().add(new PullImage().getContent(image));
    		fr.setSize(600,600);
    		fr.setLocation(200,200);
    		fr.setVisible(true);
    	}
    	
    	
    }

Similar Threads

  1. Add an image to JFrame
    By Eranga in forum AWT / Swing
    Replies: 4
    Last Post: 02-01-2010, 04:09 PM
  2. How to display image ?
    By Birkoff in forum AWT / Swing
    Replies: 7
    Last Post: 06-09-2008, 08:58 AM
  3. Replies: 2
    Last Post: 03-24-2008, 06:29 AM
  4. how to display image in jsp page(Struts)
    By rama6262 in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 08:50 AM
  5. How to display an image in JApplet
    By fred in forum Java Applets
    Replies: 1
    Last Post: 07-24-2007, 03:02 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
  •