Resize ImageIcon in JRadioButton

I just want to change the size ( diameter ) of the actual ( default ) ImageIcon my JRadioButton . I changed the font size displayed in the widgets, so it actually looks silly with such a large radio block.

 JRadioButton button = new JRadioButton("Button"); button.setFont(new Font("Lucida Grande",Font.PLAIN, 11)); 

gives me this giant button:

GIANT button !!

Do I need to create my own ImageIcon ? Or can I somehow change the default value without the hassle?

+4
source share
4 answers

There are no icons in JRadioButton. The user interface simply draws icons as needed, so you cannot use the getIcon ... () methods to get the icons and scale. Therefore, when you work around, you need to create your own icon image before you scale them.

Here you are to get you started. The Screen Image class makes it easy to create component images. Note that you will also need to create images for the rollover icons. This can be done by setting the ButtonModel.setRollover (true) method for both normal and selected switch state.

 import java.awt.*; import java.awt.image.*; import javax.swing.*; import javax.swing.border.*; public class RadioButtonScale extends JFrame { private static void createAndShowGUI() { JPanel north = new JPanel(new FlowLayout(0, 0, FlowLayout.LEFT)); JRadioButton button = new JRadioButton("Radio Button"); north.add(button); JPanel south = new JPanel(); JLabel west = new JLabel(); west.setBorder(new LineBorder(Color.GREEN)); south.add(west, BorderLayout.WEST); JLabel east = new JLabel(); east.setBorder(new LineBorder(Color.GREEN)); south.add(east, BorderLayout.EAST); // Create images of radio button icon Icon icon = UIManager.getIcon("RadioButton.icon"); Dimension preferred = button.getPreferredSize(); Insets insets = button.getInsets(); int height = preferred.height - insets.top - insets.bottom; // Rectangle r = new Rectangle(insets.left, insets.top, height, height); Rectangle r = new Rectangle(insets.left, insets.top, icon.getIconWidth(), height); west.setIcon( new ImageIcon( ScreenImage.createImage(button, r) ) ); button.setSelected(true); east.setIcon( new ImageIcon( ScreenImage.createImage(button, r) ) ); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(north, BorderLayout.NORTH); frame.add(south, BorderLayout.SOUTH); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } 
+3
source

If you are developing for the Mac platform only (based on your screen) you can use the apple "mini" client properties

 component.putClientProperty("JComponent.sizeVariant", "mini"); 

as shown on this link:

http://developer.apple.com/mac/library/technotes/tn2007/tn2196.html#SIZE_EXAMPLES

+2
source

Given ImageIcon, you can get a new, scaled instance as follows:

  ImageIcon original = ...; Image img = original.getImage(); Image scaledImage = img.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT)); ImageIcon newInstance = new ImageIcon(scaledImage); 

Please note that when you scale the image repeatedly, the quality deteriorates.

0
source

I don’t know if my actual question was wrong, but ... Why not create a gif image for the switch or increase the current one? Download it using ImageIcon g = new ImageIcon(NAME OF FILE) . Then assign it to the switch using nameOfRadioButton.setIcon(g) ;

0
source

Source: https://habr.com/ru/post/1306062/


All Articles