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