unable to set image in JLabel - java

I designed one simple Form.
In which i put one panel as contentPane one JLabel as lblPanel.
now i am trying to set image in JLabel.
I am having WindowBuilder so i can directly set image using setting the icon property of JLabel.
But when i try this it shows the Image only as its original size which is natural we must have to manually set image size to fed the JLabel whole.
SO, here is the Code that is generated by the WindowBuilder when i set the Image using the icon properties of JLabel.
lblPanel.setIcon(new ImageIcon(Admin_Form.class.getResource("/Icons/MouthSmile.jpg")));
Now My Problem is i want to set the size of image as the size of JLabel so there is any direct way to do it using WindowBuilder or just Modifying the above line?
I also tried the following way in which i taken two Imageicon and one Image.
Here is the code for that that i have been tried.
private Image img;
private ImageIcon imgicon;
private ImageIcon newimgicon;
imgicon = new ImageIcon("/Icons/MouthSmile.jpg");
img = imgicon.getImage();
newimgicon = new ImageIcon(img.getScaledInstance(lblPanel.getWidth(),lblPanel.getHeight(), Image.SCALE_SMOOTH));
lblPanel.setIcon(newimgicon);
But when i remove the code that is generated by the Window Builder and use only mine the image is not displayed.
I also seen the way to do it using BufferedImage but i think there will be no difference wheather i will use BufferedImage to resize or ImageIcon and Image.

If you are trying to get the width and height of lblPanel before it is added to your Container and before your JFrame is visible, the width and height will both be 0.

You can resize the image you are choosing to use which will make the JLabel the size you desire. That is the simplest way of going about doing it.

Related

How to resize Swing JButon's icon

I have a icon that i want to use on a JButton and somewhere else, using other dimensions. So is there any way i can resize (like button.setIconSize(124, 124); the same icon while not having 2 of them?
ImageIcon imageIcon = new ImageIcon("picture_Path"); //unscaled image
Image image = imageIcon.getImage();
Image newimg = image.getScaledInstance(newWidth, newHeight, java.awt.Image.SCALE_SMOOTH); // resize it here
imageIcon = new ImageIcon(newimg); // transform it back
so it would go something like
JButton.setIcon(imageIcon(...));
Icons are painted at their actual size, so if you want different sized icons then you need to create multiple icons. The preferred size of the component you add the Icon to will always be based on the size of the Icon.
If you want the ability to have an Icon dynamically scale as the size of its parent component resizes then you can use the Stretch Icon.

How to set the size of my application the same as an image

I uploaded an image of my java application below.
Problem:
I'm currently trying to make a login screen with a premade image as the background. I highlighted the actual size the background image which should also be the size of the application window. You can see that the login field is also out of place because its corresponding to the application window.
This is probably because I'm using
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
to set the size of the window.
My question is, how do I make the size of my application border the image?
How to set the size of my application the same as an image
Add your image to a JLabel using an ImageIcon. Then add the label to the frame and pack() the frame.
You can then set the layout manager of the JLabel and add other components to the label.
If your panel is not yet visible:
Image im = ...; // your image
JPanel jp = ...;// panel where you draw your image
jp.setPreferredSize(new Dimension(im.getWidth(null), im.getHeight(null)));

Large Image on JButton not showing

is there a way to show an image larger than the small imageIcon?
I have a 64x64 png image that I am trying to show on a button (take up the whole button).
but when I use
ImageIcon icon = new ImageIcon("somePath");
JButton btnBuildStructure = new JButton(icon);
the image that is shown is only the 16x16 in the top left corner of the image I want shown
how do I do this?
I want it to look like this
The JButton should naturally size to an optimal preferred size that shows the entire icon. If this is not happening, then you are likely restricting the size of the JButton artificially. The solution: use the layout managers well, call pack() after adding all components and before setting the GUI visible, avoid null layouts, call revalidate() and repaint() on containers if you change any components, or their sizes or preferred sizes (such as if you change an icon while a program runs).

Draw image in Java GUI

I am trying to display an image in a JFrame GUI in Java. I have successfully loaded the image from a resource file and been able to display the image in a JOptionPane. This was achieved using a JLabel containing a Image Icon in the constructor. When trying to add this image to a JPanel nothing is displayed.
JLabel imgLabel1 = new JLabel(new ImageIcon(tsr.getTileImage(1,1)),JLabel.CENTER);
jpnDisplay.add(imgLabel1);
tsr is my custom code for getting a subimage from a tileset. The image returned is of type BufferedImage.
One thing I did notice is if I display the image in a JOptionPane then add it to the JPanel the image is displayed. I am unsure why this is.
JLabel imgLabel1 = new JLabel(new ImageIcon(tsr.getTileImage(1,1)),JLabel.CENTER);
JOptionPane.showMessageDialog(null, imgLabel1,"Label",-1);
jpnDisplay.add(imgLabel1);`enter code here
--EDIT--
After playing around with my code, I have discovered my issue was not with the way I was trying to display the images, but that for some reason my JFrame was not repainting unless a JOptionPane was shown before the JFrame was shown. It also only paints the same instance that was shown in the JOptionPane. Any other images to be painted get ignored. The reason is unclear.
You must subclass your JPanel and override the redraw method to draw you image.

How to add an Image to Form in java

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.
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();
Don't know about JDeveloper but in code you have following possibilities:
Create an ImageIcon of the image then set that to a jLabel and add jLabel to your frame.
Override paintComponents() of your frame to draw image using Graphics in it. {Not sure about this}
Override paintComponent() of some panel or any other component to draw image using Graphics in it and then add that component to frame..
You can use Labels as Sanjay says.
also using layered pane you can use as background image.
You can try doing it this way:
ImageIcon image = new ImageIcon(getClass().getResource("imageName.png"));
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

Categories

Resources