Image autofit in a JButton? - java

I am new to Java and I met a little problem.
Can someone please tell me how I can set the Image of a JButton to be auto resizable/fitable/stretchable in a JButton when I resize the whole JFrame? The JButton is in a JPanel with a GridLayout.
It will be great if someone could write a brief explanation. I need this to complete a calculator I am working on.

Add component listener to the button.
Resize the Image on Fly and update Image on the button.
Image img = icon.getImage();
Image newimg = img.getScaledInstance(NEW_WIDTH, NEW_HEIGHT, java.awt.Image.SCALE_SMOOTH);
icon.setImage(newimg);

Related

Display image on JFrame form Java NetBeans

I want to show a image on top this form, but I don't know how. I have tried many times, but I cant find any image container in Toolbox.
Screenshot of JFrame:
but i cant find any image container I
Add an ImageIcon to a JLabel.
Icon icon = new ImageIcon(...);
JLabel label = new JLabel( icon );
frame.add( label );
Read the section from the Swing tutorial on How to Use Icons for a working example.

unable to set image in JLabel

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.

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.

JButton from image

I want to make JButton from image. But when i do it like this:
private void AddMainActionsButtons(Container powZawartosci){
JPanel mainActionButtons = new JPanel();
JButton applyButton = createImageButton("obrazki/apply.png");
mainActionButtons.add(applyButton);
powZawartosci.add(mainActionButtons);
mainActionButtons.setBounds(150,530,400,90);
}
private JButton createImageButton(String imagePath){
ImageIcon icon = uploadImage(imagePath,"");
JButton button = new JButton(icon);
return button;
}
JButton looks bad, because image is inside button component, and in fact I got image on image. How can I create as big JButton as my image, and how to cover it by image in 100%. In other words: how to make my image be a Button?
You need to take the space out of the buttons by removing the border. See this answer for details.
Seen above is the single image split into 9 parts. The N/S/E/W are buttons (East is activated - shown with a red border) and the rest are labels - each containing the relevant part of the original image.
See also this answer for a version that uses simpler images with JToggleButton instances.
Have you tried setting the background to a transparent color?
This will remove the funky grey border around your image that often looks bad.

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