7 Replies Latest reply: Apr 5, 2014 4:19 AM by Gnarf RSS

    Strange JPanel/JScrollPane repaint error

    Gnarf

      Hi all,

       

      I'm working on a program that displays a holiday planer. It displays the days in a nested table that is located in a scroll pane. As the inner table has some rows, the row height is more than usual.

       

      When scrolling there is a strange repaint problem: After schrolling two ore three times down and then again scrolling up the table isn't drawn any more (or just the lower part is). I have reduced my source code (and build a new project form it) that shows the error. Please paste the source code into one java file (Main.java) and run it.

       

      I use eclipse and Java 7 (1.7.0.25) on a windows system (windows 8).

       

      package main;

       

      import java.awt.EventQueue;

       

      public class Main

      {

       

          private JFrame    frame;

          private JTable table;

       

          /**

           * Launch the application.

           */

          public static void main(String[] args)

          {

              EventQueue.invokeLater(new Runnable()

              {

                  public void run()

                  {

                      try

                      {

                          Main window = new Main();

                          window.frame.setVisible(true);

                      }

                      catch (Exception e)

                      {

                          e.printStackTrace();

                      }

                  }

              });

          }

       

          /**

           * Create the application.

           */

          public Main()

          {

              initialize();

          }

       

          /**

           * Initialize the contents of the frame.

           */

          private void initialize()

          {

              frame = new JFrame();

              frame.setBounds(100, 100, 450, 300);

              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

             

              JScrollPane scrollPane = new JScrollPane();

              frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

             

              table = new JTable( new TestModel() )

              {

       

                  /* (non-Javadoc)

                   * @see javax.swing.JTable#getRowHeight(int)

                   */

                  @Override

                  public int getRowHeight(int row)

                  {

                      int nRowHeigth = super.getRowHeight(row);

                     

                      // Remove this line, and the problems are gone!

                      nRowHeigth *= 3;

                      return nRowHeigth;

                  }

       

                 

              };

             

              scrollPane.setViewportView(table);

          }

         

          class TestModel extends DefaultTableModel

          {

              /* (non-Javadoc)

               * @see javax.swing.JTable#getRowCount()

               */

              @Override

              public int getRowCount()

              {

       

                  return 50;

              }

       

              /* (non-Javadoc)

               * @see javax.swing.JTable#getColumnCount()

               */

              @Override

              public int getColumnCount()

              {

                  return 2;

              }

       

              /* (non-Javadoc)

               * @see javax.swing.JTable#getColumnClass(int)

               */

              @Override

              public Class<?> getColumnClass(int column)

              {

                  return String.class;

              }

       

              /* (non-Javadoc)

               * @see javax.swing.JTable#getValueAt(int, int)

               */

              @Override

              public Object getValueAt(int row, int column)

              {

                  String strValue = "";

                 

                  if( column == 0 )

                      strValue = "Row ";

                  else

                      strValue = "Data ";

                 

                  NumberFormat nf = NumberFormat.getIntegerInstance();

                  nf.setMinimumIntegerDigits(3);

                  strValue += nf.format(row);

                 

                  return strValue;

              }

             

          }

         

       

      }

       

       

      Can anyone please tell me how to work around this - hm - feature? This may also by a problem of windows or even windows 8, I don't know.

        • 1. Re: Strange JPanel/JScrollPane repaint error
          PhHein

          Moved from JP

          • 2. Re: Strange JPanel/JScrollPane repaint error
            TPD-Opitz

            Why don't you use the appropriate setter method of JTable?

            JTable jTable = new JTable(new TestModel());
            jTable.setRowHeight(jTable.getRowHeight()*3);
            JOptionPane.showMessageDialog(null, new JScrollPane(jTable));
            

            bye

            TPD

            • 3. Re: Strange JPanel/JScrollPane repaint error
              Gnarf

              In the real program the height is determined from the inner table. The factor 3 that I use in the example is just for demonstration purposes.

               

               

              The data the inner tables display may dynamically change. This is handled by the table model which will fire the approbiate notifications. This is exactly how these situations are ment to be handled (and thus, all components involved should be able to take the approbiate steps).

               

               

              The fundamental question is: Why doesn't JScrollPane handle the bigger row height correctly? Or, hopefully, what did I miss to overwrite/set in order to make it work? Any help appreciated!

               

               

              Thanx for taking a look at this!

              • 4. Re: Strange JPanel/JScrollPane repaint error
                Gnarf

                Sorry for selecting the wrong forum and thank you for correction.

                • 5. Re: Strange JPanel/JScrollPane repaint error
                  TPD-Opitz

                  Did you consider to replace the outer table by a JList applying a custom ListCellRenderer?

                   

                  [Edit] did you see this?

                   

                  bye

                  TPD

                  • 6. Re: Strange JPanel/JScrollPane repaint error
                    Gnarf

                    Replacing the outer table with a JList seems to be the wrong way to me (maybe only because I have never worked with JList and currently have no real idea how to do it). To me, JTable is the natural choice.

                     

                     

                    The link you gave seems familiar to me. I did not read through it in detail, as the table is created with an array as argument (which I can't in the real application). I will work my way through it.

                     

                     

                    To return to the minimal example: Breaking JScrollPane by simply returning a different row height is that simple the architects simply can't have been overseeing. I believe I have been missing something - a small but essential piece. May anybody can give me a hint how to make the minimal example work (apart from restoring the row height, of course)?

                    • 7. Re: Strange JPanel/JScrollPane repaint error
                      Gnarf

                      The problem is solved: By overloading getRowHeight() - the one without the row parameter - to return a different height scrolling now works. As in the original program all rows will have the same height this solution works.

                       

                      But: If the rows have different heights one would have a little nasty problem ...

                       

                      Thanks for all of you helping me!

                      Gnarf