Path: blob/master/test/jdk/javax/accessibility/JTable/JTableCellEditor.java
41152 views
/*1* Copyright (c) 2019, 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*/2223import java.awt.EventQueue;24import java.lang.reflect.InvocationTargetException;25import java.util.Locale;2627import javax.accessibility.AccessibleRole;28import javax.accessibility.AccessibleTable;29import javax.swing.JFrame;30import javax.swing.JTable;31import javax.swing.table.DefaultTableModel;32import javax.swing.table.TableModel;3334/**35* @test36* @bug 822665337* @key headful38* @summary The active cell editor should be reported as a child of the table.39* Note that the accessibility API ignores the real children of the40* table, but reports the "virtual" child per cell in the grid.41*/42public final class JTableCellEditor {4344private static final int COUNT = 3;45private static JTable table;46private static JFrame frame;4748public static void main(final String[] args)49throws InvocationTargetException, InterruptedException {50EventQueue.invokeAndWait(() -> {51frame = new JFrame();52table = new JTable(testSelectionWithFilterTable());53frame.add(table);54frame.pack();55});56EventQueue.invokeAndWait(() -> table.editCellAt(1, 1));57EventQueue.invokeAndWait(() -> {58AccessibleTable aTable = table.getAccessibleContext()59.getAccessibleTable();60int aColumns = aTable.getAccessibleColumnCount();61int aRows = aTable.getAccessibleRowCount();62// We cannot assume which component will be used as an editor of the63// table cell, but we can expect it will have the "text" role.64AccessibleRole role = aTable.getAccessibleAt(1, 1)65.getAccessibleContext()66.getAccessibleRole();67frame.dispose();68if (!role.toDisplayString(Locale.ENGLISH).equals("text")) {69throw new RuntimeException("Unexpected role: " + role);70}71if (aColumns != COUNT) {72throw new RuntimeException("Wrong columns: " + aColumns);73}74if (aRows != COUNT) {75throw new RuntimeException("Wrong rows: " + aRows);76}77});78}7980/**81* Creates a dummy table model.82*/83private static TableModel testSelectionWithFilterTable() {84DefaultTableModel model = new DefaultTableModel(0, 3);85for (int i = 0; i < COUNT; i++) {86model.addRow(new Object[]{i + "x0", i + "x1", i + "x2"});87}88return model;89}90}919293