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

Repaint problem when using JTable.setRowHeight(int, int)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.4.0
    • client-libs



      Name: ddT132432 Date: 10/01/2001


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
      Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)

      When JTable.setRowHeight(int, int) is used most UI components (of the table
      frame) will not repaint, the content pane of a JDialog is not realized (painted)
      and JMenuItems do not work properly.

      The following demo will utilize a TableCellRenderer (based on a JTextArea) to
      automatically ajust the row Height for each individual row (based on the size of
      the text).

      Another point to mention is that to duplicate the repaint problem you
      must cover the demo with another frame, then remove the frame to show that
      the HEADER of the table is (where the frame used to be) is not repainting.
      Also when you resize the frame (horizontally), the header sometimes will
      resize wrong (size will not match the column width).

      This problem occurs in JDk1.4 as well.

      *************************
      TableCellRenderer class
      *************************

      import javax.swing.*;
      import javax.swing.table.*;
      import javax.swing.border.*;
      import java.awt.*;

      import javax.swing.JTextArea;



      /**
       * MultiLineTableCellRenderer.java
       *
       * Created: Thu Aug 16 17:49:32 2001
       *
       * @author Luis Vega
       * @version 1.0
       */

      public class MultiLineTableCellRenderer extends JTextArea
        implements TableCellRenderer
      {
        private Color background;
        private Color foreground;
        private Color selectedBackground;
        private Color selectedForeground;
        private Font font;
        
        private int minRowHeight;

        public MultiLineTableCellRenderer( int minHeight)
        {
          super();
          super.setOpaque( true );
          super.setLineWrap( true );
          super.setWrapStyleWord( true );

          minRowHeight = minHeight;
        }

        public void setCellFont( Font f )
        {
          font = f;
        }

        public void setCellBackground( Color bg )
        {
          background = bg;
        }

        public void setSelectedCellBackground( Color selBG )
        {
          selectedBackground = selBG;
        }

        public void setCellForeground( Color fg )
        {
          foreground = fg;
        }

        public void setSelectedCellForeground( Color selFG )
        {
          selectedForeground = selFG;
        }
        
        public Component getTableCellRendererComponent( JTable table, Object value,
      boolean isSelected,
      boolean hasFocus,
      int row, int column )
        {
          if(!isSelected)
          {
            super.setForeground( (null != foreground) ? foreground :
      table.getForeground() );
            super.setBackground( (null != background) ? background :
      table.getBackground() );
          }
          else
          {
            super.setForeground( (null != selectedForeground) ? selectedForeground :
      table.getSelectionForeground() );
            super.setBackground( (null != selectedBackground) ? selectedBackground :
      table.getSelectionBackground() );
          }

          super.setFont( (null != font) ? font : table.getFont() );

      // if (hasFocus)
      // {
      // setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
      // if (table.isCellEditable(row, column))
      // {
      // super.setForeground( UIManager.getColor("Table.focusCellForeground"));
      // super.setBackground( UIManager.getColor("Table.focusCellBackground"));
      // }
      // }
      // else
          {
            super.setBorder(new EmptyBorder(1, 2, 1, 2));
          }

          super.setText((value == null) ? "" : value.toString());

          int preferredHeight = super.getPreferredSize().height;

          table.setRowHeight( row, ( (preferredHeight > minRowHeight ) ?
      preferredHeight : minRowHeight ) );

          return this;
        }


      }// MultiLineTableCellRenderer


      ********************
      Demo class
      ********************

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.table.*;

      /**
       * MultiLineTableDemo.java
       *
       *
       * Created: Fri Aug 17 11:24:00 2001
       *
       * @author Luis Vega
       * @version
       */

      public class MultiLineTableDemo extends JFrame
      {
        private static final String[] HEADERS = {"Character", "Number", "Long Text"};

        private static final String[][] DATA =
        {
          { "A", "1", "This is a test of the multiline option of JTable" },
          { "B", "2", "I would also advise caution here\n - Java wraps the window manager, and isn't set up for interacting at the level you describe.\n\t No doubt it could be done using JNI, but I would expect it would beintricate,\n\t\t messy and labour intensive." },
          { "C", "3", "I'm unaware of anything along the lines you're looking for. However, that doesn't mean that such an item doesn't exist." }
        };


        private JTable table;

        public MultiLineTableDemo ()
        {
          super("Demo");
          super.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

          table = new JTable( DATA, HEADERS );

          table.setAutoResizeMode( table.AUTO_RESIZE_LAST_COLUMN );
          table.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
          table.setRowSelectionAllowed( true );

          TableColumnModel columnModel = table.getColumnModel();
          TableColumn tableColumn = null;

          int columnCount = table.getColumnCount();
          for(int j=0; j<columnCount; j++)
          {
            tableColumn = columnModel.getColumn( j );
            tableColumn.setMinWidth( 0 );

            if( 2 == j )
            {
      tableColumn.setPreferredWidth( 150 );
      tableColumn.setCellRenderer( new MultiLineTableCellRenderer(0) );
            }
            else
            {
      tableColumn.setPreferredWidth( 50 );
            }
          }

          JScrollPane sp = new JScrollPane(table);
          
          JPanel panel = (JPanel)super.getContentPane();
          panel.setLayout (new BorderLayout());
          panel.add(sp, BorderLayout.CENTER);

          super.pack();
          super.show();
        }
        
        public static void main(String[] args)
        {
          new MultiLineTableDemo();
        }
        
      } // MultiLineTableDemo
      (Review ID: 132591)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            ddressersunw Daniel Dresser (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: