Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-07-2007, 04:43 PM
Member
 
Join Date: Dec 2007
Posts: 15
Soda is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 12-07-2007, 07:26 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
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
Soda is on a distinguished road
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
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
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
Sponsored Links
Reply


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

vB 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 +3. The time now is 11:27 AM.


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