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/*
25   @test
26  @key headful
27   @bug 4473075
28   @summary JTable header rendering problem (after setting preferred size)
29   @author Semyon Sadetsky
30*/
31
32import javax.swing.*;
33import javax.swing.table.DefaultTableModel;
34import java.awt.*;
35import java.awt.event.InputEvent;
36
37public class bug4473075 {
38    public static final int USER_HEADER_HEIGHT = 40;
39    private static JTable table;
40    private static JScrollPane scpScroll;
41    private static Point point;
42    private static JFrame frame;
43
44    public static void main(String[] args) throws Exception {
45        Robot robot = new Robot();
46        robot.setAutoDelay(20);
47        SwingUtilities.invokeAndWait(new Runnable() {
48            public void run() {
49                frame = new JFrame();
50                frame.setUndecorated(true);
51                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
52                table = new JTable();
53                String t = "a cell text";
54                table.setModel(new DefaultTableModel(
55                        new Object[][]{new Object[]{t, t, t, t, t}},
56                        new Object[]{t, t, t, t, t}));
57                table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
58                scpScroll = new JScrollPane(table);
59
60                // Manually set preferred size of header...
61                Dimension preferredSize = new Dimension(table.getSize().width,
62                        USER_HEADER_HEIGHT);
63                table.getTableHeader().setPreferredSize(preferredSize);
64
65                frame.setContentPane(scpScroll);
66                frame.setSize(250, 480);
67                frame.setLocationRelativeTo(null);
68                frame.setVisible(true);
69                point = scpScroll.getHorizontalScrollBar()
70                        .getLocationOnScreen();
71            }
72        });
73        robot.waitForIdle();
74
75        robot.mouseMove(point.x + 100, point.y + 5);
76        robot.mousePress(InputEvent.BUTTON1_MASK);
77        robot.mouseMove(point.x + 150, point.y + 5);
78        robot.mouseRelease(InputEvent.BUTTON1_MASK);
79
80        int headerH = table.getTableHeader().getHeight();
81        if (headerH != USER_HEADER_HEIGHT) {
82            throw new RuntimeException("TableHeader height was not set: "
83                    + headerH + " !=" + USER_HEADER_HEIGHT);
84        }
85
86        double tableX = table.getX();
87        int headerX = table.getTableHeader().getX();
88        if (tableX != headerX) {
89            throw new RuntimeException("TableHeader X position is wrong: "
90                    + tableX + " !=" + headerX);
91        }
92
93        double tableW = table.getWidth();
94        int headerW = table.getTableHeader().getWidth();
95        if (tableW != headerW) {
96            throw new RuntimeException("TableHeader width is wrong: "
97                    + tableW + " !=" + headerW);
98        }
99
100        SwingUtilities.invokeLater(new Runnable() {
101            @Override
102            public void run() {
103                frame.dispose();
104            }
105        });
106        System.out.println("ok");
107    }
108}
109