Path: blob/master/test/jdk/javax/swing/JTable/TestClearSel.java
41149 views
/*1* Copyright (c) 2018, 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*/22/*23* @test24* @key headful25* @bug 820270226* @summary Verifies if Jtable clear selction causes disappearance of a row.27* @run main/manual TestClearSel28*/29import java.awt.BorderLayout;30import java.awt.Component;31import java.awt.FlowLayout;32import java.awt.event.MouseAdapter;33import java.awt.event.MouseEvent;34import java.util.concurrent.CountDownLatch;35import java.util.concurrent.TimeUnit;36import javax.swing.JButton;37import javax.swing.JDialog;38import javax.swing.JFrame;39import javax.swing.JPanel;40import javax.swing.JTable;41import javax.swing.JTextArea;42import javax.swing.SwingUtilities;43import javax.swing.table.DefaultTableModel;44import javax.swing.table.TableModel;4546public class TestClearSel {4748static DefaultTableModel model;4950public static void main(String[] args) throws Exception {51final CountDownLatch latch = new CountDownLatch(1);5253ClearSelTest test = new ClearSelTest(latch);54Thread T1 = new Thread(test);55T1.start();5657// wait for latch to complete58boolean ret = false;59try {60ret = latch.await(60, TimeUnit.SECONDS);61} catch (InterruptedException ie) {62throw ie;63}64if (!ret) {65test.dispose();66throw new RuntimeException(" User has not executed the test");67}6869if (test.testResult == false) {70throw new RuntimeException("Some text were not rendered properly"71+ " during painting of Jtable rows ");72}73}74}7576class ClearSelTest implements Runnable {77static JFrame f;78static JDialog dialog;79static DefaultTableModel model;80public boolean testResult = false;81private final CountDownLatch latch;82private static String[] rows = new String[]{83"Row1", "Row2", "Row3", "Row4", "Row5",84"Row6", "Row7", "Row8", "Row9", "Row10"};8586public ClearSelTest(CountDownLatch latch) throws Exception {87this.latch = latch;88}8990@Override91public void run() {92try {93SwingUtilities.invokeAndWait(() -> {94createUI();95clearSelTest();96});97} catch (Exception ex) {98if (f != null) {99f.dispose();100}101latch.countDown();102throw new RuntimeException("createUI Failed: " + ex.getMessage());103}104105}106107public void dispose() {108dialog.dispose();109f.dispose();110}111112private static void clearSelTest() {113final DefaultTableModel model = new DefaultTableModel();114model.addColumn("Test", rows);115final JTable table = new JTable(model);116table.setRowHeight(25);117118final MouseAdapter adapt = new MouseAdapter() {119120@Override121public void mouseMoved(final MouseEvent pE) {122final int row = table.rowAtPoint(pE.getPoint());123if (row > -1) {124table.setRowSelectionInterval(row, row);125} else {126table.clearSelection();127}128}129130@Override131public void mouseEntered(final MouseEvent pE) {132final int row = table.rowAtPoint(pE.getPoint());133if (row > -1) {134table.setRowSelectionInterval(row, row);135} else {136table.clearSelection();137}138}139140@Override141public void mouseExited(final MouseEvent pE) {142table.clearSelection();143}144};145table.addMouseListener(adapt);146table.addMouseMotionListener(adapt);147148f = new JFrame();149f.setSize(300, 300);150f.setLocationRelativeTo(null);151f.add(table);152f.setVisible(true);153}154155156private final void createUI() {157String description158= " INSTRUCTIONS:\n"159+ " A JTable will be shown.\n"160+ " Move mouse over different row to select the row.\n "161+ " Please verify if row text disappear "162+ " if mouse is moved out of table.\n"163+ " If any moment any part of the rows will not be\n "164+ " painted properly and if some text are missing in JTable,\n "165+ " then press fail else press pass";166167dialog = new JDialog();168dialog.setTitle("textselectionTest");169JTextArea textArea = new JTextArea(description);170textArea.setEditable(false);171final JButton passButton = new JButton("PASS");172passButton.addActionListener((e) -> {173testResult = true;174dispose();175latch.countDown();176});177final JButton failButton = new JButton("FAIL");178failButton.addActionListener((e) -> {179testResult = false;180dispose();181latch.countDown();182});183JPanel mainPanel = new JPanel(new BorderLayout());184mainPanel.add(textArea, BorderLayout.CENTER);185JPanel buttonPanel = new JPanel(new FlowLayout());186buttonPanel.add(passButton);187buttonPanel.add(failButton);188mainPanel.add(buttonPanel, BorderLayout.SOUTH);189dialog.add(mainPanel);190dialog.pack();191dialog.setVisible(true);192}193}194195196