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

Cell renderers don't support components that use scrollRectToVisible

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P2
    • Resolution: Fixed
    • Affects Version/s: 1.0
    • Fix Version/s: 1.0.2
    • Component/s: client-libs
    • Labels:
      None
    • Subcomponent:
    • Resolved In Build:
      1.0.2
    • CPU:
      x86
    • OS:
      windows_nt

      Description

      JTextArea cannot be used as a renderer in JTable - scrolltoVisiblie technology
      fails in this context.
      -------------------------------------------------------------------------
      Please see the following code to reproduce this bug.

      The test took JTextArea as the CellRenderer of the first column. The table didn't show right.

      import java.awt.*;
      import java.awt.event.*;
      import com.sun.java.swing.*;
      import com.sun.java.swing.border.*;
      import com.sun.java.swing.table.*;

      public class TableCellRenderTest extends JFrame
      {
          static public void main( String[] args )
          {
              TableCellRenderTest t = new TableCellRenderTest();
              t.pack();
              t.show();
          }

          String[][] data =
          {
              { "one", "two", "three" },
              { "four", "five", "six" },
              { "seven", "eight", "nine" },
              { "ten", "eleven", "twelve" },
              { "thirteen", "fourteen", "fifteen"},
              { "sixteen", "seventeen", "eighteen" },
              { "ninteen", "twenty", "twenty one" },
              { "twenty two", "twenty three", "twenty four" }
          };

          String[] colnames = { "column 1", "column 2", "column 3" };

          JTable table = new JTable( data, colnames );

          static private String clear = "Clear";
          static private String repaint = "Force Repaint";

          public TableCellRenderTest()
          {
              JScrollPane sp = JTable.createScrollPaneForTable( table );

              JPanel topPanel = new JPanel();
              topPanel.setLayout( new BorderLayout() );
              topPanel.setBorder( new EmptyBorder( 10, 10, 10, 10 ) );
              table.setBackground( topPanel.getBackground() );

              JPanel buttonPanel = new JPanel();

              topPanel.add( sp, BorderLayout.CENTER );
              topPanel.add( buttonPanel, BorderLayout.SOUTH );

              getContentPane().add( topPanel );

      //CellRender
      TableCellRenderer myRenderer = new CustomCellRenderer();
      table.getColumn("column 1").setCellRenderer(myRenderer);
          }

        class CustomCellRenderer
          // extends JLabel
          extends JTextArea
          //extends JTextField
          implements TableCellRenderer
        {

          public Component getTableCellRendererComponent( JTable table,
      Object value, boolean isSelected,
      boolean hasFocus,
      int row, int column )
          {
            setText( "Row=" + row );
            return this;
          }
        }
      }

      patience.chu@Eng 1998-03-16


      From: Atul Dravid <###@###.###>
      Sender: ###@###.###
      To: ###@###.###
      Subject: The JTextArea as cell renderer is having problem....
      Date: Thu, 02 Apr 1998 17:34:29 -0800

      Hi ,
      I am trying to user JTextArea as TableCellRenderer and seems that it
      load the table fine at first time..... But some event occurs and the
      table gets reshuffled... The first column vanishes and the scrollpane
      displays one column less than total number of columns....


      Following is the source code....


      Am I doing something wronge.....?


      Let me know...


      Thanks in advance....


      import com.sun.java.swing.*;
      import com.sun.java.swing.table.*;
      import com.sun.java.swing.event.*;
      import com.sun.java.swing.border.*;

      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;
      import java.net.*;

      /*
       * @version 1.25 01/31/98
       * @author Philip Milne
       * @author Steve Wilson
       */
      public class TablePanel extends JPanel
      {
          JTable tableView;
          JScrollPane scrollpane;
          Dimension origin = new Dimension(0, 0);

          JCheckBox isColumnReorderingAllowedCheckBox;
          JCheckBox showHorizontalLinesCheckBox;
          JCheckBox showVerticalLinesCheckBox;

          JCheckBox isColumnSelectionAllowedCheckBox;
          JCheckBox isRowSelectionAllowedCheckBox;
          JCheckBox isRowAndColumnSelectionAllowedCheckBox;

          JLabel interCellSpacingLabel;
          JLabel rowHeightLabel;

          JSlider interCellSpacingSlider;
          JSlider rowHeightSlider;

          JComponent selectionModeButtons;
          JComponent resizeModeButtons;

          JPanel mainPanel;
          JPanel controlPanel;
          JScrollPane tableAggregate;
          public static void main(String[] args) {
             JFrame jf = new JFrame();
             jf.getContentPane().add(new TablePanel());
             jf.show();
          }
          public TablePanel() {
              super();
              
              setLayout(new FlowLayout());
              //mainPanel = this;

              // Create the table.
              tableAggregate = createTable();
              add(tableAggregate);
              //mainPanel.repaint();
          }


          private ImageIcon loadIcon(String name, String description) {
              String path = "images/ImageClub/food/" + name;
              return loadImageIcon(path, description);
          }
        public ImageIcon loadImageIcon(String filename, String description) {
            return new ImageIcon(filename, description);
        }


          public JScrollPane createTable() {

              // final
              final String[] names = {"Text Area", "Last Name", "Favorite
      Color",
                                      "Favorite Sport", "Favorite Number",
      "Favorite Food", "First Name"};

              ImageIcon burger = loadIcon("burger.gif","burger");
              ImageIcon fries = loadIcon("fries.gif","fries");
              ImageIcon softdrink = loadIcon("softdrink.gif","soft drink");
              ImageIcon hotdog = loadIcon("hotdog.gif","hot dog");
              ImageIcon pizza = loadIcon("pizza.gif","pizza");
              ImageIcon icecream = loadIcon("icecream.gif","ice cream");
              ImageIcon pie = loadIcon("pie.gif","pie");
              ImageIcon cake = loadIcon("cake.gif","cake");
              ImageIcon donut = loadIcon("donut.gif","donut");
              ImageIcon treat = loadIcon("treat.gif","treat");
              ImageIcon grapes = loadIcon("grapes.gif","grapes");
              ImageIcon banana = loadIcon("banana.gif","banana");
              ImageIcon watermelon = loadIcon("watermelon.gif","watermelon");
              ImageIcon cantaloupe = loadIcon("cantaloupe.gif","cantaloupe");
              ImageIcon peach = loadIcon("peach.gif","peach");
              ImageIcon broccoli = loadIcon("broccoli.gif","broccoli");
              ImageIcon carrot = loadIcon("carrot.gif","carrot");
              ImageIcon peas = loadIcon("peas.gif","peas");
              ImageIcon corn = loadIcon("corn.gif","corn");
              ImageIcon radish = loadIcon("radish.gif","radish");
       
              
              // Create the dummy data (a few rows of names)
              final Object[][] data = {
                {"Mark", "Andrews", Color.red, "Baseball", new
      Integer(2), broccoli, "Text2"},
                {"Tom", "Ball", Color.blue, "Football", new
      Integer(99), burger, "Text3"},
                {"Alan", "Chung", Color.green, "Baseball", new
      Integer(838), cake, "Text4"},
                {"Jeff", "Dinkins", Color.magenta, "Football", new
      Integer(8), cantaloupe, "Text5"},
                {"Amy", "Fowler", Color.yellow, "Hockey", new
      Integer(3), carrot, "Text6"},
                {"Brian", "Gerhold", Color.green, "Rugby", new
      Integer(7), corn, "Text7"},
                {"James", "Gosling", Color.pink, "Tennis", new
      Integer(21), donut, "Text8"},
                {"David", "Karlton", Color.red, "Baseball", new
      Integer(1), fries, "Text9"},
                {"Dave", "Kloba", Color.yellow, "Football", new
      Integer(14), grapes, "Text10"},
                {"Peter", "Korn", new Color(100, 100, 255), "Scuba
      Diving", new Integer(12), broccoli, "Text11"},
                {"Dana", "Miller", Color.blue, "Ice Skating", new
      Integer(8), banana, "Text12"},
                {"Phil", "Milne", Color.magenta, "Rugby", new
      Integer(3), banana, "Text13"},
                {"Dave", "Moore", Color.green, "Tennis", new
      Integer(88), peach, "Text14"},
                {"Hans", "Muller", Color.magenta, "Baseball", new
      Integer(5), peas, "Text15"},
                {"Rick", "Levenson", Color.blue, "Football", new
      Integer(2), pie, "Text16"},
                {"Tim", "Prinzing", Color.blue, "Baseball", new
      Integer(22), pizza, "Text17"},
                {"Chester", "Rose", Color.black, "Hockey", new
      Integer(0), radish, "Text18"},
                {"Chris", "Ryan", Color.black, "None", new Integer(6),
      softdrink, "Text19"},
                {"Ray", "Ryan", Color.gray, "Football", new
      Integer(77), treat, "Text20"},
              };

              // Create a model of the data.
              TableModel dataModel = new AbstractTableModel() {
                  public int getColumnCount() { return names.length; }
                  public int getRowCount() { return data.length;}
                  public Object getValueAt(int row, int col)
                  {
                    //if (data[row][col] instanceof JTextArea)
                      //return ((JTextArea)data[row][col]).getText();
                    return data[row][col];
                  }
                  public String getColumnName(int column) {return
      names[column];}
                  public Class getColumnClass(int c)
                  {
                     if (c == 0)
                        return JTextArea.class;
                     return getValueAt(0, c).getClass();
                  }
                  public boolean isCellEditable(int row, int col) {return
      getColumnClass(col) == String.class; }
                  public void setValueAt(Object aValue, int row, int column)
                  {
                    data[row][column] = aValue;
                    //System.err.println(aValue);
                    ////System.err.println("This is when selected " );
                    //System.err.println(aValue);
                  }
               };


              // Create the table
              tableView = new JTable(dataModel);

              // Show colors by rendering them in their own color.
              DefaultTableCellRenderer colorRenderer = new
      DefaultTableCellRenderer() {
                  public void setValue(Object value) {
                      if (value instanceof Color) {
                          Color c = (Color)value;
                          setForeground(c);
                          setText(c.getRed() + ", " + c.getGreen() + ", " +
      c.getBlue());
                      }
                  }

              };

              colorRenderer.setHorizontalAlignment(JLabel.RIGHT);
              tableView.getColumn("Favorite
      Color").setCellRenderer(colorRenderer);

              tableView.getColumn("Text Area").setCellRenderer(new
      TextAreaRenderer());

              tableView.setRowHeight(20);
              
              scrollpane = JTable.createScrollPaneForTable(tableView);
              //tableView.resizeAndRedraw();
              return scrollpane;
          }
      }
      class TextAreaRenderer extends JTextArea implements TableCellRenderer
      {
          public TextAreaRenderer() { }

          public Component getTableCellRendererComponent(JTable table, Object
      value,
              boolean isSelected, boolean hasFocus, int rowIndex, int column)
          {
              if (isSelected)
              {
                //System.err.println("This is when selected " + value);
              }
              this.setRows(3);
              //if (!((String)value).equals(this.getText()))
                //this.setText((String)value);
              setValue(value);
              return this;
          }
          public void setValue(Object value)
          {
              setText((value == null) ? "" : value.toString());
          }
      }


        Attachments

          Activity

            People

            Assignee:
            sswingtrsunw Swingtraq Swingtraq (Inactive)
            Reporter:
            sswingtrsunw Swingtraq Swingtraq (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: