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

Switching TableModels and resizing rows a problem

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.3.0
    • client-libs
    • generic, x86
    • generic, windows_95



      Name: boT120536 Date: 01/04/2001


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


      I have a program that switches JTable TableModels on the fly. My JTable also
      uses a default cell renderer, which adjusts the sizes of the rows of the
      JTable.

      If I start with a table model with 2 rows, then switch to one with more than 2,
      I still only see 2 in the table. I will never see more than 2, no matter how
      many additional ones may be in my table model.

      This is because the JTable's private value, rowModel (which holds row sizes),
      does not get reset when you do a setModel. It keeps the row sizes in an array,
      which doesn't grow when the table model changes....

      The following code illustrates this. Compile and run it, you will see table
      labeled "two-row table" with 2 rows. Click the "Switch" button below. The
      label switches to "three-row table", but there are still only 2 rows. If you
      do not set the custom renderer, or if you do not adjust the row heights in the
      renderer, the switch button functions as expected. But the renderer is useless
      if it cannot adjust the heights of the rows!!

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


      public class test extends JFrame {

         DefaultTableModel tm2;
         DefaultTableModel tm3;
         JTable table;

         public test() {

            table = new JTable();

            String[] tm2head = {"2-row header"};
            String[] tm3head = {"3-row header"};
            String[][] tm2body = {{"first row"},{"second row"}};
            String[][] tm3body = {{"first row"},{"second row"},{"third row"}};

            tm2 = new DefaultTableModel(tm2body,tm2head);
            tm3 = new DefaultTableModel(tm3body,tm3head);

            table.setModel(tm2);

            // Commenting out the below line makes this program work!!
            table.setDefaultRenderer(Object.class,new TextCellRenderer(table));

            JButton button = new JButton("switch");
            button.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                  if (table.getModel()==tm2)
                     table.setModel(tm3);
                  else
                     table.setModel(tm2);
               }
            });

            JScrollPane sp = new JScrollPane(table);

            getContentPane().add(sp,BorderLayout.CENTER);
            getContentPane().add(button,BorderLayout.SOUTH);
            pack();
            setVisible(true);

         }



         public static void main(String[] args) {
            new test();
         }



             private class TextCellRenderer extends JTextArea implements
      TableCellRenderer {

               private Hashtable rowHeights = new Hashtable();
               private Color selBackground;
               private Color selForeground;
               private Color background;
               private Color foreground;

               public TextCellRenderer(JTable table) {
                 setEditable(false);
                 setLineWrap(true);
                 setWrapStyleWord(true);
                 selBackground = table.getSelectionBackground();
                 selForeground = table.getSelectionForeground();
                 background = table.getBackground();
                 foreground = table.getForeground();
               }

               public Component getTableCellRendererComponent(JTable table,
                                                            Object value,
                                                            boolean isSelected,
                                                            boolean hasFocus,
                                                            int row,
                                                            int column) {

                 if (isSelected) {
                   setBackground(selBackground);
                   setForeground(selForeground);
                 }
                 else {
                   setBackground(background);
                   setForeground(foreground);
                 }

                 if (value instanceof String) {
                   setText((String)value);
                   // set the table's row height, if necessary
                   Integer oldHeight = (Integer)rowHeights.get(new Integer(row));
                   Integer newHeight = new Integer(getPreferredSize().height);
                   if (oldHeight==null || !oldHeight.equals(newHeight)) {
                     rowHeights.put(new Integer(row),newHeight);

                     // Commenting out the below line makes this program work!!
                     table.setRowHeight(row,newHeight.intValue());
                   }
                 }
                 else
                   setText("");
                 return this;
               }
             }
      }
      (Review ID: 109943)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: