views:

219

answers:

3

I simply want to change the size (diameter) of the actual (default) ImageIcon of my JRadioButton. I've changed the size of the font displayed in the widget, so it really looks silly with such a large radiobutton.

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

gives me this giant button:

GIANT button!!

Do I really have to create my own ImageIcon? Or can I somehow scale the default one, without too much of a hassle?

A: 

Given an ImageIcon, you can derive 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);

Note however, that repeatedly scaling an image will degrade the quality.

Devon_C_Miller
I get a nullpointer exception when calling getImage() of the ImageIcon. My JRadioButton returns an Icon by calling getIcon(), and not an ImageIcon, so I cast it, which might have something to do with it?EDIT: The Icon of my JRadioButtons seems to be null, for some reason...
Frederik Wordenskjold
+2  A: 

If you are developing only for Mac Platform (based on your screen-shot) You may consider using apple's client properties "mini"

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

as shown at this link:

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

ring bearer
+1, good hint. However, it should work on all platforms.
Frederik Wordenskjold
A: 

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.

Here is something to get you started. The Screen Image 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.

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();
            }
        });
    }
}
camickr