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?
rafaelriAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.