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

Drawing bugs when using emptyBorder with button classes

XMLWordPrintable

      Name: skT88420 Date: 06/07/99


      The use of an empty border has several bugs when used
      with AbstractButton subclass buttons(ie JButton,
      JCheckBox, JRadioButton, etc). The problems range
      from not having the padding of the empty border applied
      correctly to having the focus rectangle drawn on top
      of the area covered by the empty border. The following
      table lists the specific problems while the code at
      the end can be used to demonstrate all of the problems.
      The only scenario showing totally correct behavior was
      the JButton class displayed with the Motif look and feel.
      When running the test program the LF can be set by supplying
      either "metal", "win" or "motif" as the command line argument.


      ==============================================================
                | | Empty | Btn bgd | Focus rect
      Button | Look/ | padding | painted on | is drawn
      Class | Feel | correct? | empty border? | correctly?
      ==============================================================
      JButton | Metal | Yes | Yes (WRONG) | Yes
                | Windows | Yes | Yes (WRONG) | No (WRONG)
                | Motif | Yes | No | Yes
      --------------------------------------------------------------
      JCheckBox | Metal | No (WRONG) | Yes (WRONG) | Yes
                | Windows | No (WRONG) | Yes (WRONG) | Yes
                | Motif | No (WRONG) | Yes (WRONG) | No (WRONG)
      ==============================================================

      //~~~~~~~~~~~~~~~~~~~~~~~~~~ CODE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class bug extends JFrame {

          public static void main(String args[]) {
              String METAL = "javax.swing.plaf.metal.MetalLookAndFeel";
              String MOTIF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
              String WIN = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
              String lnf = METAL;
              if (args.length == 1) {
                  if (args[0].equals("motif"))
                      lnf = MOTIF;
                  else if (args[0].startsWith("win"))
                      lnf = WIN;
              }
              try {
                  UIManager.setLookAndFeel(lnf);
              } catch (Exception e) {}

              JFrame f = new JFrame();
              f.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {System.exit(0);}
              });
              JButton btn1 = new JButton("JButton (pad top)");
              JButton btn2 = new JButton("JButton (pad left)");
              JButton btn3 = new JButton("JButton (pad bottom)");
              JButton btn4 = new JButton("JButton (pad right)");
              JCheckBox ckbox1 = new JCheckBox("JCheckBox (pad top)");
              JCheckBox ckbox2 = new JCheckBox("JCheckBox (pad left)");
              JCheckBox ckbox3 = new JCheckBox("JCheckBox (pad bottom)");
              JCheckBox ckbox4 = new JCheckBox("JCheckBox (pad right)");

              btn1.setBackground(Color.white);
              btn2.setBackground(Color.pink);
              btn3.setBackground(Color.red);
              btn4.setBackground(Color.yellow);
              ckbox1.setBackground(Color.white);
              ckbox2.setBackground(Color.pink);
              ckbox3.setBackground(Color.red);
              ckbox4.setBackground(Color.yellow);

              btn1.setBorder(BorderFactory.createCompoundBorder(
                  BorderFactory.createEmptyBorder(50,0,0,0),
                  btn1.getBorder()
              ));
              btn2.setBorder(BorderFactory.createCompoundBorder(
                  BorderFactory.createEmptyBorder(0,50,0,0),
                  btn2.getBorder()
              ));
              btn3.setBorder(BorderFactory.createCompoundBorder(
                  BorderFactory.createEmptyBorder(0,0,50,0),
                  btn3.getBorder()
              ));
              btn4.setBorder(BorderFactory.createCompoundBorder(
                  BorderFactory.createEmptyBorder(0,0,0,50),
                  btn4.getBorder()
              ));
              ckbox1.setBorder(BorderFactory.createCompoundBorder(
                  BorderFactory.createEmptyBorder(50,0,0,0),
                  ckbox1.getBorder()
              ));
              ckbox2.setBorder(BorderFactory.createCompoundBorder(
                  BorderFactory.createEmptyBorder(0,50,0,0),
                  ckbox2.getBorder()
              ));
              ckbox3.setBorder(BorderFactory.createCompoundBorder(
                  BorderFactory.createEmptyBorder(0,0,50,0),
                  ckbox3.getBorder()
              ));
              ckbox4.setBorder(BorderFactory.createCompoundBorder(
                  BorderFactory.createEmptyBorder(0,0,0,50),
                  ckbox4.getBorder()
              ));

              Container cp = f.getContentPane();
              cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));

              cp.add(btn1);
              cp.add(btn2);
              cp.add(btn3);
              cp.add(btn4);
              cp.add(ckbox1);
              cp.add(ckbox2);
              cp.add(ckbox3);
              cp.add(ckbox4);

              f.pack();
              f.setVisible(true);
          }
      }
      (Review ID: 84001)
      ======================================================================

            honkar Harshitha Onkar
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: