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

JTable does not auto resize headers based on character widths and heights

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P5 P5
    • None
    • 1.4.1_02
    • client-libs
    • Fix Understood
    • sparc
    • solaris_9

      JTable does not do default or auto resizing sizing of JTable
      headers based on character widths/heights.
      but leaves computation of correct preferred sizes to clients.

      What is needed is a fixed cell height table/tree whose
      cell height will accomodate the font set in the look and feel.
      Same for headers.

      See 4875678 as an example of an issue where this places task on the client
      side to do this calculation.

      Workaround being used now is something like
      On the first paint() call, check a boolean flag.
      If true, get the current font, calculate the width you
       need and call the setter for it (this will trigger a
       repaint, so just set the flag to true and return from paint(


      Using a variable height table/tree is not helpful
      since variable height trees/tables do not scale -
      you have to calculate the ht of everything by iterating it.

      As to customers depending on current behavior -
      this can be implemented in a backward compatible way - if the fixed cell
       height is ever set programmatically (not by the table/tree/whatever it
      self), then honor that value (the same way preferredSize() delegates to
      the layout manager unless it's been explicitly set).

      -----------

      To be perfectly clear about what this issue is about: What we need is auto-sizing behavior for the *height* of a fixed height table or tree. We need fixed height for performance/scalability, but the row height must be calculated based on the height font in question. This is particularly important for Chinese localizations, where a larger default font size is required.

      The workaround we have in place now looks like this, in a JTable/JTree subclass:

          private boolean needCalcRowHeight=true;
          private void calcRowHeight(Graphics g) {
              //Users of themes can set an explicit row height, so check for it
              Integer i = (Integer) UIManager.get (PropUtils.KEY_ROWHEIGHT); //NOI18N
              
              int rowHeight;
              if (i != null) {
                  rowHeight = i.intValue();
              } else {
                  //Derive a row height to accomodate the font and expando icon
                  Font f = getFont();
                  FontMetrics fm = g.getFontMetrics(f);
                  rowHeight = Math.max (fm.getHeight()+3,
                      PropUtils.getSpinnerHeight());
              }
              //Clear the flag
              needCalcRowHeight = false;
              //Set row height. If displayable, this will generate a new call
              //to paint()
              setRowHeight (rowHeight);
          }

          public void paint(Graphics g) {
      if (needCalcRowHeight) {
      calcRowHeight(g);
      return;
      }
      super.paint(g);
          }

          public void setFont(Font f) {
      needCalcRowHeight=true;
      super.setFont(f);
          }

      This could be implemented in a backward compatible way by providing a constant (-2 seems a good candidate) that can be passed to setRowHeight to indicate that a fixed height based on font size is desired.

      This functionality is needed for JTree, JTable and JList.

      ###@###.### 2003-11-26

            Unassigned Unassigned
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: