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

JTable does not properly layout its content

XMLWordPrintable

    • b27
    • generic, x86
    • generic, windows_xp

      Name: gm110360 Date: 09/28/2004


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

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP 2001
      Build 5.1.2600

      A DESCRIPTION OF THE PROBLEM :
      When
      1.JTable is added to a JPanel with GridBagLayout
      2. maxWidth property of table columns are set and
      3. component orientation is left tot right

      Table header is laid out right-to-left but table content are laid out left-to-right

      ACTUAL -
      Table header is right to left but table content is left to right

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      import javax.swing.*;
      import javax.swing.table.AbstractTableModel;
      import java.util.List;
      import java.util.ArrayList;
      import java.awt.*;

      public class TestTable {
          public final static int COL_FIRSRNAME = 0;
          public final static int COL_LASTNAME = 1;
          public final static int COL_SALARY = 2;

          static final Class[] classes = {
              String.class,
              String.class,
              Float.class,
          };

          String[] cols = {
              "First name",
              "Last name",
              "Salary",
          };
          List data = new ArrayList();
          JTable table;

          public TestTable() {
              data.add(new Data("Reza", "Khayami", 10000f));
              data.add(new Data("Shahram", "Javidnia", 10000f));
              data.add(new Data("Danial", "Toofani", 10000f));
              table = new JTable(new Model());
              table.getColumnModel().getColumn(COL_FIRSRNAME).setMaxWidth(90);
              table.getColumnModel().getColumn(COL_LASTNAME).setMaxWidth(90);
              table.getColumnModel().getColumn(COL_SALARY).setMaxWidth(90);
          }

          class Data {
              String firstname;
              String lastname;
              float salary;

              public Data(String firstname, String lastname, float salary) {
                  this.firstname = firstname;
                  this.lastname = lastname;
                  this.salary = salary;
              }
          }

          class Model extends AbstractTableModel {

              public int getColumnCount() {
                  return cols.length;
              }

              public int getRowCount() {
                  return data.size();
              }

              public Object getValueAt(int rowIndex, int columnIndex) {
                  Data item = (Data) data.get(rowIndex);
                  switch (columnIndex) {
                      case COL_FIRSRNAME:
                          return item.firstname;
                      case COL_LASTNAME:
                          return item.lastname;
                      case COL_SALARY:
                          return new Float(item.salary);
                  }
                  return null;
              }

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

              public Class getColumnClass(int columnIndex) {
                  return classes[columnIndex];
              }
          }

          public static void main(String[] args) {
              JFrame frame = new JFrame("Test JTable");
              JPanel panel = new JPanel(new GridBagLayout());
              frame.setContentPane(panel);
              panel.add(new JScrollPane(new TestTable().table),
                      new GridBagConstraints(0,0,-1,-1,1.0,1.0,GridBagConstraints.PAGE_START, GridBagConstraints.BOTH, new Insets(2,2,2,2),0,0 ));
              frame.pack();
              frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              frame.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
              frame.setVisible(true);
          }
      }

      ---------- END SOURCE ----------
      (Incident Review ID: 315728)
      ======================================================================

            tr Tejesh R
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: