Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4126679

JRadioButtons with Icons don't size themselves correctly.

XMLWordPrintable



      Name: rk38400 Date: 04/07/98


      The problem is that JRadioButtons with Icons look pretty strange.
      The behaviour that I would expect is that the buttons should size
      themself after the icons. The way it currently works is that the
      Image is off-center, hiding the left edge of the icon, and there
      are 'insets' top, under and right making the button much bigger
      than it should be.
      The problem was there under swing-0.7 as well, but much less
      severe. I don't remember it under swing-0.6.1 .

      The code supplied demonstrates this.
      Note that the Icons are actually square, but the left edge is
      hidden by the border.

      To compile:

      Copy the code to a file called JButtonBug.java, and compile
      with:
      >javac JButtonBug.java

      Make sure swing is in your classpath.

      /Joakim Lindfors



      Code follows:

      --------------------------------------------------------
      import java.awt.*;
      import java.awt.image.*;
      import com.sun.java.swing.*;

      public class JButtonBug extends Frame{
      JPanel buttonPanel=new JPanel();

      ImageIcon[] icons=new ImageIcon[3];


      public static void main(String[] args){
      JButtonBug theApp=new JButtonBug();
      }

      public JButtonBug(){

      /*try{
      UIManager.setLookAndFeel(
      UIManager.getSystemLookAndFeelClassName());
      }
      catch(Exception e){
      System.out.println("Could not set LookAndFeel");
      }*/

      int[] redArray=new int[20*20];
      int red=Color.red.getRGB();
      for (int i=0; i<redArray.length; i++){
      redArray[i]=red;
      }
      icons[0]=new ImageIcon( createImage(
      new MemoryImageSource(20,20,redArray,0,20)));

      int[] blueArray=new int[20*20];
      int blue=Color.blue.getRGB();
      for (int i=0; i<blueArray.length; i++){
      blueArray[i]=blue;
      }
      icons[1]=new ImageIcon( createImage(
      new MemoryImageSource(20,20,blueArray,0,20)));

      int[] greenArray=new int[20*20];
      int green=Color.green.getRGB();
      for (int i=0; i<greenArray.length; i++){
      greenArray[i]=green;
      }
      icons[2]=new ImageIcon( createImage(
      new MemoryImageSource(20,20,greenArray,0,20)));


      GridBagLayout gridBag=new GridBagLayout();
          GridBagConstraints c=new GridBagConstraints();
          buttonPanel.setLayout(gridBag);

      for (int i=0; i<12; i++){
         JRadioButton button=new JRadioButton(icons[i%3]);
         button.setBorderPainted(true);
         if ((i%3)==(2)){
         c.gridwidth=GridBagConstraints.REMAINDER;
         }
         else{
         c.gridwidth=1;
         }
         gridBag.setConstraints(button,c);
         buttonPanel.add(button);
         }

      add(buttonPanel);

      pack();
      show();
      }
      }
      (Review ID: 25826)
      ======================================================================

            peterz Peter Zhelezniakov
            rkarsunw Ralph Kar (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: