How to add an Image to Form in java
Asked Answered
B

4

4

I am designing a form in java using JDeveloper. I am new to JDeveloper. In JDeveloper tool I didn't found any option to directly add image to form like .Net. And I don't know how to add image to form manually. is there any other way to solve it out. So please help me to solve it.

Brena answered 16/12, 2011 at 6:26 Comment(0)
P
7

As simple as this :

image = ImageIO.read(new File(path));
JLabel picLabel = new JLabel(new ImageIcon(image));

Yayy! Now your image is a swing component ! add it to a frame or panel or anything like you usually do! Probably need a repainting too , like

jpanel.add(picLabel);
jpanel.repaint(); 
Pilferage answered 16/12, 2011 at 6:43 Comment(1)
+1 This is easy and flexible; see also ImageApp for a different approach.Farthingale
G
2

Don't know about JDeveloper but in code you have following possibilities:

  1. Create an ImageIcon of the image then set that to a jLabel and add jLabel to your frame.
  2. Override paintComponents() of your frame to draw image using Graphics in it. {Not sure about this}
  3. Override paintComponent() of some panel or any other component to draw image using Graphics in it and then add that component to frame..
Guard answered 16/12, 2011 at 6:32 Comment(2)
"2. Override paintComponent() of your frame" JFrame has no paintComponent() method.Petrick
@AndrewThompson: I think its paintComponents() for JFrame. But still not sure.Guard
A
2

You can use Labels as Sanjay says.

also using layered pane you can use as background image.

Anandrous answered 16/12, 2011 at 6:57 Comment(0)
P
0

You can try doing it this way:

  1. ImageIcon image = new ImageIcon(getClass().getResource("imageName.png"));
  2. JLabel lblImage = new JLabel(image);

line 1 of the code will get the image ensure that the image is in the same folder you are saving your work

Prudish answered 30/4, 2017 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.