1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @bug 4473075
  26    @summary JTable header rendering problem (after setting preferred size)
  27    @author Semyon Sadetsky
  28 */
  29 
  30 import javax.swing.*;
  31 import javax.swing.table.DefaultTableModel;
  32 import java.awt.*;
  33 import java.awt.event.InputEvent;
  34 
  35 public class bug4473075 {
  36     public static final int USER_HEADER_HEIGHT = 40;
  37     private static JTable table;
  38     private static JScrollPane scpScroll;
  39     private static Point point;
  40     private static JFrame frame;
  41 
  42     public static void main(String[] args) throws Exception {
  43         Robot robot = new Robot();
  44         robot.setAutoDelay(20);
  45         SwingUtilities.invokeAndWait(new Runnable() {
  46             public void run() {
  47                 frame = new JFrame();
  48                 frame.setUndecorated(true);
  49                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50                 table = new JTable();
  51                 String t = "a cell text";
  52                 table.setModel(new DefaultTableModel(
  53                         new Object[][]{new Object[]{t, t, t, t, t}},
  54                         new Object[]{t, t, t, t, t}));
  55                 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  56                 scpScroll = new JScrollPane(table);
  57 
  58                 // Manually set preferred size of header...
  59                 Dimension preferredSize = new Dimension(table.getSize().width,
  60                         USER_HEADER_HEIGHT);
  61                 table.getTableHeader().setPreferredSize(preferredSize);
  62 
  63                 frame.setContentPane(scpScroll);
  64                 frame.setSize(250, 480);
  65                 frame.setLocationRelativeTo(null);
  66                 frame.setVisible(true);
  67                 point = scpScroll.getHorizontalScrollBar()
  68                         .getLocationOnScreen();
  69             }
  70         });
  71         robot.waitForIdle();
  72 
  73         robot.mouseMove(point.x + 100, point.y + 5);
  74         robot.mousePress(InputEvent.BUTTON1_MASK);
  75         robot.mouseMove(point.x + 150, point.y + 5);
  76         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  77 
  78         int headerH = table.getTableHeader().getHeight();
  79         if (headerH != USER_HEADER_HEIGHT) {
  80             throw new RuntimeException("TableHeader height was not set: "
  81                     + headerH + " !=" + USER_HEADER_HEIGHT);
  82         }
  83 
  84         double tableX = table.getX();
  85         int headerX = table.getTableHeader().getX();
  86         if (tableX != headerX) {
  87             throw new RuntimeException("TableHeader X position is wrong: "
  88                     + tableX + " !=" + headerX);
  89         }
  90 
  91         double tableW = table.getWidth();
  92         int headerW = table.getTableHeader().getWidth();
  93         if (tableW != headerW) {
  94             throw new RuntimeException("TableHeader width is wrong: "
  95                     + tableW + " !=" + headerW);
  96         }
  97 
  98         SwingUtilities.invokeLater(new Runnable() {
  99             @Override
 100             public void run() {
 101                 frame.dispose();
 102             }
 103         });
 104         System.out.println("ok");
 105     }
 106 }