[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

JTable Large Header Painting Problem

I Have a JTable with a lot of columns and set the autoresize to AUTO_RESIZE_OFF. After that, if I Scroll to the left all the headers outside the view area are not displayed correctly.

Here is a test code that I've done:

----------------------------------

import javax.swing.JFrame;

import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

public class Teste extends JFrame {
   
      /**
       * This method initializes defaultTableModel      
       *       
       * @return javax.swing.table.DefaultTableModel      
       */    
      private DefaultTableModel getDefaultTableModel() {
            if (defaultTableModel == null) {
                  defaultTableModel = new DefaultTableModel();
                  defaultTableModel.setColumnIdentifiers(new Object[]{"teste1", "teste2", "teste3", "teste4", "teste5", "teste6", "teste7", "teste8", "teste9", "teste10", "teste11", "teste12"});
            }
            return defaultTableModel;
      }
     public static void main(String[] args) {
        new Teste().show();
    }

      private javax.swing.JPanel jContentPane = null;

      private JTable jTable = null;
      private JScrollPane jScrollPane = null;
      private DefaultTableModel defaultTableModel = null;  //  @jve:decl-index=0:visual-constraint="489,104"
      /**
       * This is the default constructor
       */
      public Teste() {
            super();
            initialize();
      }
      /**
       * This method initializes this
       *
       * @return void
       */
      private void initialize() {
            this.setSize(300,200);
            this.setContentPane(getJContentPane());
      }
      /**
       * This method initializes jContentPane
       *
       * @return javax.swing.JPanel
       */
      private javax.swing.JPanel getJContentPane() {
            if(jContentPane == null) {
                  jContentPane = new javax.swing.JPanel();
                  jContentPane.setLayout(new java.awt.BorderLayout());
                  jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
            }
            return jContentPane;
      }
      /**
       * This method initializes jTable      
       *       
       * @return javax.swing.JTable      
       */    
      private JTable getJTable() {
            if (jTable == null) {
                  jTable = new JTable();
                  jTable.setModel(getDefaultTableModel());
                  jTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
                  jTable.setPreferredSize(new java.awt.Dimension(5000, 240));
                  jTable.revalidate();
                  jTable.repaint();
                  jTable.getTableHeader().revalidate();
                  jTable.getTableHeader().repaint();
                  
            }
            return jTable;
      }
      /**
       * This method initializes jScrollPane      
       *       
       * @return javax.swing.JScrollPane      
       */    
      private JScrollPane getJScrollPane() {
            if (jScrollPane == null) {
                  jScrollPane = new JScrollPane();
                  jScrollPane.setViewportView(getJTable());
            }
            return jScrollPane;
      }
  }


----------------------
anyone knows how to solve that problem?
0
rafaelri
Asked:
rafaelri
1 Solution
 
gnoonCommented:
You should set width for each column, and the total width of the columns should be equal to the width of the table.

     private JTable getJTable() {
          if (jTable == null) {
               int columnCount = 12;
               int tableWidth = 5000;
               int columnWidth = (int)tableWidth/columnCount;
               jTable = new JTable();
               jTable.setModel(getDefaultTableModel());
               jTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
               jTable.setPreferredSize(new java.awt.Dimension(tableWidth, 240));
               javax.swing.table.DefaultTableColumnModel columnModel =
                   (javax.swing.table.DefaultTableColumnModel)jTable.getTableHeader().getColumnModel();
               java.util.Enumeration columns = columnModel.getColumns();
               while(columns.hasMoreElements())
               {
                   javax.swing.table.TableColumn column =
                       (javax.swing.table.TableColumn)columns.nextElement();
                   column.setPreferredWidth(columnWidth);
               }
          }
          return jTable;
     }

For more information, see http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#doLayout().
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now