Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-07-2007, 04:43 PM
Member
 
Join Date: Dec 2007
Posts: 15
Rep Power: 0
Soda is on a distinguished road
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!!!
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-07-2007, 07:26 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
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
Code:
JPanel panel = new JPanel(new GridBagLayout());
panel.add(label, new GridBagConstraints());
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-08-2007, 12:52 PM
Member
 
Join Date: Dec 2007
Posts: 15
Rep Power: 0
Soda is on a distinguished road
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 01:20 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-08-2007, 06:54 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
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:
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);
    }
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing Images shaungoater Advanced Java 0 03-17-2008 11:38 AM
Problems with JPanels and displaying Mastergeek666 AWT / Swing 1 01-19-2008 01:32 AM
Can't synchronize multiple JPanels in a JFrame vassil_zorev AWT / Swing 0 12-30-2007 05:22 PM
Help with images... toby Java Applets 1 08-04-2007 06:25 AM
Images in JSP Daniel JavaServer Pages (JSP) and JSTL 1 06-05-2007 07:01 AM


All times are GMT +2. The time now is 11:58 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org