How to resize Swing JButon's icon - java

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.

Related

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).

Load image to JScrollPane

I'm trying to create a simple map that will create a circle on a certain coordinate. I want to use an image with it's fixed size as the map and add it to a JScrollPane. My problem here is that I dont know how to add a scrollable image.
Well you need a way to display that image. Is the image embedded in a component?
For example you draw the image in a JPanel
The JPanel can then be added to JScrollPane. When you open your window you open it to the size you want and the scrollbars will be there.
Add your image to a JLabel, then add that JLabel to your JScrollPane. If the size of the image is greater than the size of your JScrollPane, the scrollbars will appear. For example:
final Image image = ...
final JLabel imageLabel = new JLabel(new ImageIcon(image));
final JScrollPane scroll = new JScrollPane(imageLabel);
container.add(scroll, ...);

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.

JToolBar buttons with different size images-NetBeans

I'm having problem in my JToolBar. I'm using images of different size.
Tool bar is not looking good. How can I use different size images in the JToolbar buttons?
How can I show the button label below each image?
How can all the buttons be aligned from the top, left corner?
For example,
java.net.URL imageURL2 = cldr.getResource("Images/report2.jpg");
ImageIcon aceOfDiamonds1 = new ImageIcon(imageURL2);
btnReport = new JButton(aceOfDiamonds1);
btnReport.setMaximumSize(new Dimension(49, 43));
btnReport.addActionListener(this);
jToolBar1.add(btnReport);
I'm using images of different size.
Resize them. Either once at time of build (recommended), or at run-time.
And I want to show the button label below each image
This is close.
newJButton(String,Icon);
And i want to show the button label below to the each image?
With the setVerticalTextPosition and setHorizontalTextPosition methods of JButton:
// Place text below icon
button.setVerticalTextPosition(SwingConstants.BOTTOM);
button.setHorizontalTextPosition(SwingConstants.CENTER);
How to show all this buttons align from the left top corner
As Andrew Thompson said, the images all have to be the same size.

Categories

Resources