Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JSlider/6348946/bug6348946.java
38855 views
/*1* Copyright (c) 2007, 2011, 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* @bug 634894626* @summary Tests that JSlider's thumb moves in the right direction27* when it is used as a JTable cell editor.28* @author Mikhail Lapshin29*/3031import java.awt.*;32import java.awt.event.InputEvent;33import javax.swing.*;34import javax.swing.event.*;35import javax.swing.table.*;3637public class bug6348946 {3839private static JFrame frame;4041private static JPanel panel;42private static Robot robot;4344private static volatile boolean passed = false;4546public static void main(String[] args) throws Exception {47robot = new Robot();48robot.setAutoDelay(10);4950String lf = "javax.swing.plaf.metal.MetalLookAndFeel";51UIManager.setLookAndFeel(lf);5253try {54SwingUtilities.invokeAndWait(new Runnable() {55public void run() {56setupUI();57}58});59robot.waitForIdle();60clickOnSlider();61robot.waitForIdle();62checkResult();63} finally {64stopEDT();65}66}6768private static void setupUI() {69frame = new JFrame();7071panel = new JPanel();72panel.setLayout(new BorderLayout());73panel.add(new ParameterTable(), BorderLayout.CENTER);74frame.getContentPane().add(panel);7576frame.pack();77frame.setLocationRelativeTo(null);78frame.setVisible(true);79}8081private static void clickOnSlider() throws Exception {82Rectangle rect = getPanelRectangle();8384double clickX = rect.getX() + rect.getWidth() / 4;85double clickY = rect.getY() + rect.getHeight() / 2;86robot.mouseMove((int) clickX, (int) clickY);8788robot.mousePress(InputEvent.BUTTON1_MASK);89robot.mouseRelease(InputEvent.BUTTON1_MASK);90}9192private static void checkResult(){93if (passed) {94System.out.println("Test passed");95} else {96throw new RuntimeException("The thumb moved " +97"to the right instead of the left!");98}99}100101private static void stopEDT() {102SwingUtilities.invokeLater(new Runnable() {103public void run() {104frame.dispose();105}106});107}108109private static class ParameterTable extends JTable {110public ParameterTable() {111super(new Object[][]{{5}}, new String[]{"Value"});112getColumnModel().getColumn(0).setCellRenderer(new Renderer());113getColumnModel().getColumn(0).setCellEditor(new Editor());114}115}116117private static class Renderer implements TableCellRenderer {118private JSlider slider = new JSlider(0, 10);119120public Component getTableCellRendererComponent(JTable table,121Object value,122boolean isSelected,123boolean hasFocus,124int row, int col) {125int val = (Integer) value;126slider.setValue(val);127return slider;128}129}130131private static class Editor extends AbstractCellEditor implements TableCellEditor {132private JSlider slider = new JSlider(0, 10);133134public Component getTableCellEditorComponent(JTable table, Object value,135boolean isSelected,136int row, int col) {137int val = (Integer) value;138slider.setValue(val);139return slider;140}141142public Editor() {143slider.addChangeListener(new ChangeListener() {144public void stateChanged(ChangeEvent e) {145if (!slider.getValueIsAdjusting()) {146passed = slider.getValue() <= 5;147}148}149});150}151152public Object getCellEditorValue() {153return slider.getValue();154}155}156157private static Rectangle getPanelRectangle() throws Exception{158final Rectangle[] result = new Rectangle[1];159160SwingUtilities.invokeAndWait(new Runnable() {161@Override162public void run() {163result[0] = new Rectangle(panel.getLocationOnScreen(), panel.getSize());164}165});166167return result[0];168}169}170171172