Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are no icons added to a JRadioButton. The UI just paints Icons as needed so you can't use the getIcon...() methods to get the icons and scale then. So as a work around you need to create your own image of the icons first before you scale them.</p> <p>Here is something to get you started. The <a href="http://tips4java.wordpress.com/2008/10/13/screen-image/" rel="nofollow noreferrer">Screen Image</a> class makes creating images of a component easy. Note you will also need to create images for the "rollover" icons. This can be done by setting the ButtonModel.setRollover(true) method for both the normal and selected states of the radio button.</p> <pre><code>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(); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload