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

    Default How to add Images to JPanels?

    Have tried a couple of methods but its not working...

    Here is one that I have tried:

    panel = new JPanel(new ImageIcon("path"));



    I just need a simple code, similar to the one I tried........



    Thnx much!!!

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    String path = "path_to_image";
    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel(new ImageIcon(path));
    label.setHorizontalAlignment(JLabel.CENTER);
    panel.add(label);  // default center section
    If you do not want the label to expand to fill the center section you can try
    Java Code:
    JPanel panel = new JPanel(new GridBagLayout());
    panel.add(label, new GridBagConstraints());

  3. #3
    Soda is offline Member
    Join Date
    Dec 2007
    Posts
    15
    Rep Power
    0

    Default

    Um...

    What if the layout of my panel is set to null? I have components added to the panel. I need the panel to have an image....

    If I used that...will the label cover the components? By the way, I have a label in the panel....
    Last edited by Soda; 12-08-2007 at 12:20 PM.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    In working with null layout you can add the label and call setBounds on it to position it. Use the image width and height for the bounds width and height. You can always set an etched border on it (temporarily, during development) to see its size.
    The other option is to draw the image on the panel:
    Java Code:
    class Pseudo extends JPanel {
        BufferedImage image;
    
        Pseudo(BufferedImage image) {
            this.image = image;
            // or load it in this class
            setLayout(null);
            add components...
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = 
            int y = 
            g.drawImage(image, x, y, this);
        }
    }

Similar Threads

  1. Comparing Images
    By shaungoater in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 10:38 AM
  2. Problems with JPanels and displaying
    By Mastergeek666 in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2008, 12:32 AM
  3. Can't synchronize multiple JPanels in a JFrame
    By vassil_zorev in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2007, 04:22 PM
  4. Help with images...
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 05:25 AM
  5. Images in JSP
    By Daniel in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-05-2007, 06:01 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
  •