Display a jpg image on a JPanel - java

What would be the most appropriate image type to display a jpg image (loaded from a local folder) on a JPanel?
Cheers.

ImageIcon image = new ImageIcon("image/pic1.jpg");
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( label, BorderLayout.CENTER );

You could use a javax.swing.ImageIcon and add it to a JLabel using setIcon() method, then add the JLabel to the JPanel.

I'd probably use an ImageIcon and set it on a JLabel which I'd add to the JPanel.
Here's Sun's docs on the subject matter.

I would use a Canvas that I add to the JPanel, and draw the image on the Canvas.
But Canvas is a quite heavy object, sine it is from awt.

You could also use
ImageIcon background = new ImageIcon("Background/background.png");
JLabel label = new JLabel();
label.setBounds(0, 0, x, y);
label.setIcon(background);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.add(label);
if your working with a absolut value as layout.

Related

How can I change the color of the titled border in my GUI?

I've already looked at other similar posts on how to do this, but I don't understand any of them. Most of them use this object called "TitledBorder", but I simply use the method ".setBorder()". I want to change the colour of the border and the colour of the titles as well. Please help me out, and thank you!
private void layoutView()
{
//The JPanel that holds the JTextField. This is the first
//JPanel that I want to change the color of the titled border
JPanel question = new JPanel();
question.add(this.question);
question.setBorder(BorderFactory.createTitledBorder("Ask a question"));
//The JPanel that holds the JLabel.
//This is the second JPanel that I want to change the colour of.
JPanel questionAnswerPanel = new JPanel();
questionAnswerPanel.add (this.questionAnswer);
questionAnswerPanel.setBorder(BorderFactory.createTitledBorder("Prediction"));
//The JPanel that holds the question JPanel so it can be centered
JPanel center = new JPanel();
center.setLayout(new BorderLayout());
center.add(question, BorderLayout.CENTER);
//The complete layout
this.setLayout(new BorderLayout());
this.add(center, BorderLayout.CENTER);
this.add(questionAnswerPanel, BorderLayout.SOUTH);
}
question.setBorder(BorderFactory.createTitledBorder("Ask a question"));
What don't you understand about that statement? Did you read the API?
The BorderFactory returns an instance of the TitledBorder class, so you assign it to a variable and then you can invoke any method from the TitledBorder class.
//question.setBorder(BorderFactory.createTitledBorder("Ask a question"));
TitledBorder border = BorderFactory.createTitledBorder("Ask a question");
border.setTitleColor( Color.RED );
question.setBorder( border );

Unable to add contents over my background image in java

I am creating a profile page. At the left, I have added a jlabel containing a background image to my jpanel. Now over this panel, I want to add another jlabel for the icon. However, it is not showing up. Please help me.
P.s: This is only part of the code
public CustomerProfile()
{
super("Customer Profile");
setLayout(new BorderLayout());
//PROFILE DETAILS JPANEL
jpProfile = new JPanel();
add(jpProfile, BorderLayout.WEST);
//BACKGROUND IMAGE OF THE PROFILE JPANEL
ImageIcon background_img = new ImageIcon("background.jpg");
Image img = background_img.getImage();
Image tempImg = img.getScaledInstance(350, 600, Image.SCALE_SMOOTH);
background_img = new ImageIcon(tempImg);
background = new JLabel("",background_img,JLabel.CENTER);
jpProfile.add(background);
//PROFILE ICON
ImageIcon profImg = new ImageIcon("female.png");
jlProfileIcon = new JLabel();
jlProfileIcon.setIcon(profImg);
//ADDING PROFILE ICON TO JPANEL
jpProfileIcon = new JPanel();
jpProfileIcon.add(jlProfileIcon);
//jpProfile.setOpaque(false);
//jpProfileIcon.setOpaque(false);
jpProfileIcon.add(jlProfileIcon);
background.add(jpProfileIcon);
}
By default only a JPanel uses a layout manager.
The JLabel does not use a layout manager so the size/location of any component added to the label is not changed. The default size of a component is (0, 0) so there is nothing to paint.
Try:
background = new JLabel("",background_img,JLabel.CENTER);
background.setLayout( new BorderLayout() ); // added
…
background.add(jlProfileIcon);
The following code is not needed:
//jpProfileIcon = new JPanel();
//jpProfileIcon.add(jlProfileIcon);
That is you don't need to create a JPanel, just to add the label to it.
A better option is to paint the background image onto a panel and then you can just add your label normally to the panel. See: Background Panel for a class that implements this functionality.

Make a Java JLabel Icon Resizeable

How does one resize an image that will be placed in a JLabel?
ImageIcon icon = ImageIcon("img.gif");
JLabel label = new JLabel(icon);

How can add JLabel on JDesktopPane

I want to add a JLable on my JDesktopePane..i wrote the below given code..but the Label is not displayed on the pane.
{
frame1.setContentPane(desktop);
frame1.setSize(900,700);
frame1.setVisible(true);
desktop.setBackground(Color.DARK_GRAY );
JLabel label1 = new JLabel("Main Page", SwingConstants.CENTER);
label1.setFont(new Font("SansSerif",Font.ITALIC + Font.BOLD,54));
desktop.add(label1);**
}
The JDesktop is one of the few containers that does not use a traditional layout manager.
In order for any component to be added to it, that component needs to have it's position and size set manually.
Try something like label1.setBounds(new Rectangle(new Point(10, 10), label1.getPreferredSize())) before you add it
Use a JPanel, add JLabel to it and then add JPanel to JDesktopPane

How to display an image in Java from web?

i have an URL image, and i want to display it in a panel.
How can i do it ?
One way to achieve this would be to use the URL class to grab the image from the web, create your ImageIcon object and then add it onto your JPanel,
Code is untested, but should demonstrate what you need to do.
URL img = new URL("http://www.example.com/whatever.jpg");
ImageIcon image = new ImageIcon(img);
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( label, BorderLayout.CENTER );

Categories