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

DefaultTableCellRenderer renders incorrectly when vertical alignment is TOP

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: client-libs

      Description

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
        Program has a customized JTable. Each row of the table can have a different row height. A value in a particular column in the table must be aligned at the top of the cell. The table is implemented and everything works fine except for the vertical alignment of the mentioned column. The renderer for the column is derived from DefaultTableCellRenderer and the vertical alignment is set to TOP. The issue is that the contents are pushed down by 2 pixels. For a cell with the minimum (JTable default) row height, this causes the bottom 1 pixel of the text not be displayed.

      The included program eliminates the variable row height for simplicity and still has the problem.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile/run the attached program. Click on the TOP and CENTER radio buttons to see the issue.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Text to be aligned to the very top of the cell
      ACTUAL -
      The top border for the cell seems to be widened by 2 pixels.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.Dimension;

      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      import javax.swing.ButtonGroup;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JRadioButton;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;

      import javax.swing.table.TableCellRenderer;

      public class JLabelAtTop extends JPanel implements ActionListener {

        private final static String TOP_ALIGNED = "Top";
        private final static String CENTER_ALIGNED = "Center";

        private final String columnNames [] = { "Function Name", "Class Name", "Usage Count" };
        private final Object columnData [][] = {
          { "create_object", "Class_One", new Integer (12) },
          { "createObject" , "ClassOne", new Integer (14) }
        };

        private final JTable table = new JTable (columnData, columnNames);

        private JLabelAtTop () {

          table.setPreferredScrollableViewportSize (new Dimension (400, 80));

          JScrollPane scrollPane = new JScrollPane (table);

          add (scrollPane);
          addButtons ();

        } // J L a b e l A t T o p

        private void addButtons () {

          ButtonGroup group = new ButtonGroup ();

          JRadioButton button = new JRadioButton (TOP_ALIGNED);
          button.addActionListener (this);
          group.add (button);
          add (button);

          button = new JRadioButton (CENTER_ALIGNED);
          button.addActionListener (this);
          group.add (button);
          button.setSelected (true);
          add (button);

        } // a d d B u t t o n s

        public void actionPerformed (ActionEvent event) {

          if ((event.getSource () instanceof JRadioButton) == false)
            return;

          JRadioButton button = (JRadioButton) event.getSource ();
          int verticalAlignment = TOP_ALIGNED.equals (button.getText ()) ? JLabel.TOP : JLabel.CENTER;

          for (int index = 0; index < table.getColumnCount (); index++) {
            TableCellRenderer renderer = table.getCellRenderer (0, index);
            if ((renderer instanceof JLabel) == false)
              continue;

            ((JLabel) renderer).setVerticalAlignment (verticalAlignment);
          }

          invalidate ();
          repaint ();

        } // a c t i o n P e r f o r m e d

        public static void main (String[] args) {

          JLabelAtTop test = new JLabelAtTop ();
          JFrame frame = new JFrame ();
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

          frame.getContentPane ().add (test);
          frame.pack ();
          frame.setVisible (true);

        } // m a i n

      } // J L a b e l A t T o p

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Add the following paintComponent () to the renderer:




        protected void paintComponent (Graphics graphics) {

          // The border change is a hack to get the text properly aligned vertically.
          Border border = getBorder ();
          if (border != null)
            setBorder (new EmptyBorder (-1, 1, 1, 1));

          paintComponent_ (graphics);

          setBorder (border);

         } // p a i n t C o m p o n e n t

        Attachments

          Activity

            People

            • Assignee:
              peterz Peter Zhelezniakov
              Reporter:
              ryeung Roger Yeung (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Imported:
                Indexed: