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

JTable-AUTO_RESIZE_ LAST_COLUMN works only for fixed table width

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • client-libs

      Name: sv35042 Date: 10/08/2002


      FULL PRODUCT VERSION :
      java version "1.4.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)


      FULL OPERATING SYSTEM VERSION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      I extended JTable to a class fitting my needs.
      When setting the resize behaviour with
      this.setAutoResizeMode( AUTO_RESIZE_LAST_COLUMN );
      everything seemed fine.
      My table consist of 4 columns, of which the last works as a
      buffer and does not have any content.
      While resizing the first 3 columns results in de/increasing
      the last (as expected), resizing the whole table works not!
      If the table is resized, ALL colums seem to resize too,
      instead of only the last one, so the buffer is no worth
      anymore!
      My guess is: that's a bug! If it's not maybe something like
      this.setAutoResizeMode( AUTO_RESIZE_LAST_COLUMN_ALWAYS );
      would be usefull.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. create FolderTable in a JScrollPane
      2. resize call colums expect last
      3. resize JScrollPane (or the frame around)


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      I tried to achieve the same behaviour as the "Windows
      Explorer" (on WindowsXP) in the table on the right with its
      file/folders, but it does not work as expected.
      When reszing individual colums, everything works fine!
      While resizing the table/pane does not AUTO RESIZE LAST
      COLUMN, it does AUTO RESIZE ALL COLUMNS.

      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      //file: FolderTable
      import javax.swing.JTable;
      import javax.swing.ListSelectionModel;
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import java.awt.Dimension;

      public class FolderTable extends JTable {

      public FolderTable() {
      super(new FolderTableModel());
      this.setCellSelectionEnabled(true);
      this.setShowHorizontalLines( false );
      this.setShowVerticalLines( false );
      this.setIntercellSpacing( new Dimension( 0, 2 ) );
      this.setSelectionMode(
      ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
      this.setAutoResizeMode( AUTO_RESIZE_LAST_COLUMN );
      this.getColumn("Filename").setPreferredWidth(200);
      this.getColumn("Size").setPreferredWidth(80);
      }

      public static void main(String[] args) {
      FolderTable ft = new FolderTable();
      JScrollPane jsp = new JScrollPane(ft);
      JFrame jf = new JFrame();
      jf.getContentPane().add(jsp);
      jf.pack();
      jf.setVisible(true);
      }
      }
      ////////////////////////////////////////////////////////////////
      //file: FolderTable
      import javax.swing.*;
      import javax.swing.table.*;
      import java.io.*;
      import java.util.*;
      import java.util.Date;
      import java.text.SimpleDateFormat;
      import java.text.NumberFormat;

      public class FolderTableModel extends AbstractTableModel {
      protected File directory;
      protected ArrayList children;
      protected Object dirIcon;
      protected Object fileIcon;
      protected int rowCount;
      protected SimpleDateFormat sdf;
      protected NumberFormat nf;


      public String columns[] = { "Filename", "Size", "Type", "Modified", ""};

      public FolderTableModel() {
      this(new File("C:\\"));
      }

      public FolderTableModel(File dir) {
      children = new ArrayList();
      sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
      nf = NumberFormat.getInstance();
      nf.setMaximumFractionDigits(2);
      nf.setMinimumFractionDigits(0);
      nf.setMinimumIntegerDigits(1);
      setDirectory(dir);
      }

      public void setDirectory(File dir ) {
      if (dir == null)
      return;

      if (dir != directory) {
      File[] files = dir.listFiles();
      rowCount = files.length;
      for(int i=0; i<rowCount; i++) {
      children.add(files[i]);
      }

      directory = dir;
      fireTableDataChanged();
      }
      }

      public void getPath() {
      }
      public int getRowCount() {
      return children != null ? rowCount : 0;
      }
      public int getColumnCount() {
      return columns.length;
      }

      public String getColumnName(int column) {
      return columns[column];
      }

      public boolean isCellEditable(int nRow, int nCol) {
      return false;
      }

      public Object getValueAt(int row, int column){
      File file = (File) children.toArray()[row];
      switch ( column ) {
      case 0:
      return file.getName();
      case 1:
      if (!file.isDirectory()) {
      double l = file.length();
      String Size ="";
      if (l < 1099511627776.0) {
      if ( (0 < l) && (l < 1024) )
      l = 1;
      else
      l /= 1024.0;
      nf.setMaximumFractionDigits(0);
      Size = " KB ";
      } else {
      l /= 1099511627776.0;
      Size = " GB ";
      nf.setMaximumFractionDigits(2);
      }
      return nf.format(l) + Size;
      } else
      return "";
      case 2:
      if (file.isDirectory())
      return " Folder";
      String s = file.getName();
      if (s.endsWith("zip"))
      return " zip folder";
      else
      return " unknown";
      case 3:
      return sdf.format( new Date( file.lastModified
      () ) );
      default:
      return "";
      }
      }

      public Class getColumnClass( int column ) {
      if ( column == 0 ) {
      return getValueAt( 0, column).getClass();
      } else {
      return super.getColumnClass( column );
      }
      }
      }


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

      CUSTOMER WORKAROUND :
      this.setAutoResizeMode( AUTO_RESIZE_OFF );
      works somehow, but the last column does not adjust to the
      actual table size and does not look fine either :-(
      (Review ID: 143775)
      ======================================================================

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: