
At first, I was using the ImageIcon class to initialize my icons, but as soon as I added these icons to the labels, the labels no longer changed size with. I'm using a jpanel with an 8 by 8 grid layout of jlabels to which I'm adding images to represent a chess board. scaledAndCenteredImage = new (_width, _height, .TRANSLUCENT) ĭimension scaledDimension = getScaledDimension( new Dimension(_icon.getImage().getWidth(null), _icon.getImage(). Swing/AWT/SWT :: Getting Icons To Change Size With Window Resize. And in same way go to foreground option and choose font color whatever you want just describe and mention in bellow image. Set jLabel Foreground Color and jLabel Font Settings: For font setting just go to font option of Properties window and choose font name, size and type. Rescale the image such that it maintains its aspect ratio but fits within the IconLabel size Auto Resizing Controls in jFrame in Java Swing (2). Public IconLabel(Image image, int width, int height) Public IconLabel(ImageIcon icon, int width, int height) In this JLabel-extending class, the real tricks are to override getPreferredSize and the paintComponent methods, where I do the image resizing. The getScaledDimension is taken from that URL, and it does a better job of explaining how it resizes an image and maintains its aspect ratio. That way, you can pre-determine how big of a space you want your image to take without compromising its aspect ratio. For added bonus (and for satisfying my OCD), I have pieced together some logic that also scales the image to within a certain size, maintaining its aspect ratio (i.e., I used ). You may not need the whole IconLabel class, but it does implement the resize technique mentioned above. JLabel(String text, int horizontalAlignment) Here a JLabel is created with a vertically centered text.


The horizontal alignment can be specified via the third parameter.

In the project I am working on, I have been using a modified JLabel to use a resized icon. JLabel(String text, Icon icon, int horizontalAlignment) This constructor can be used to set both a text and an icon for the JLabel object. In this situation, it looks like you do actually need a resized image. All of the posts I have read elsewhere recommend using the BufferedImage/Graphics.drawImage approach. I have been using a modified JLabel to use a resized icon. Now recreate the IconImage with the new buffered image: Does anyone know if there is a way to resize an ImageIcon when placing it on a Button so that.
NETBEANS JLABEL ICON RESIZE CODE
(The code above may be incorrect - check the docs) G.drawImage(img, 0, 0, WIDTH, HEIGHT, null) Then blit the icon image to the buffered image, and resize it as you do so: Now create a buffered image the same size as the image:īufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB) Then use getImage() to grab the image from the ImageIcon: Make sure the reference you create is an ImageIcon reference. ImageIcon icon = new ImageIcon("whatever.jpg") However, as in your case, it's not always what we want to have happen. That manager will automatically resize components at will to fit what it thinks is best. The layout that NetBeans uses with the GUI editor is called the GroupLayout layout manager. You load your image straight into the ImageIcon using the constructor that takes a file name as an argument like: Ah, you're using the GUI editor in NetBeans.

package Searson wrote:Ok, here's one way of doing it. To create a JLabel with an image icon we can either pass an ImageIcon as a second parameter to the JLabel constructor or use the tIcon() method to set the icon.
