Path: blob/master/test/jdk/javax/swing/JSlider/6348946/bug6348946.java
41153 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* @key headful26* @bug 634894627* @summary Tests that JSlider's thumb moves in the right direction28* when it is used as a JTable cell editor.29* @author Mikhail Lapshin30*/3132import java.awt.*;33import java.awt.event.InputEvent;34import javax.swing.*;35import javax.swing.event.*;36import javax.swing.table.*;3738public class bug6348946 {3940private static JFrame frame;4142private static JPanel panel;43private static Robot robot;4445private static volatile boolean passed = false;4647public static void main(String[] args) throws Exception {48robot = new Robot();49robot.setAutoDelay(10);5051String lf = "javax.swing.plaf.metal.MetalLookAndFeel";52UIManager.setLookAndFeel(lf);5354try {55SwingUtilities.invokeAndWait(new Runnable() {56public void run() {57setupUI();58}59});60robot.waitForIdle();61clickOnSlider();62robot.waitForIdle();63checkResult();64} finally {65stopEDT();66}67}6869private static void setupUI() {70frame = new JFrame();7172panel = new JPanel();73panel.setLayout(new BorderLayout());74panel.add(new ParameterTable(), BorderLayout.CENTER);75frame.getContentPane().add(panel);7677frame.pack();78frame.setLocationRelativeTo(null);79frame.setVisible(true);80}8182private static void clickOnSlider() throws Exception {83Rectangle rect = getPanelRectangle();8485double clickX = rect.getX() + rect.getWidth() / 4;86double clickY = rect.getY() + rect.getHeight() / 2;87robot.mouseMove((int) clickX, (int) clickY);8889robot.mousePress(InputEvent.BUTTON1_MASK);90robot.mouseRelease(InputEvent.BUTTON1_MASK);91}9293private static void checkResult(){94if (passed) {95System.out.println("Test passed");96} else {97throw new RuntimeException("The thumb moved " +98"to the right instead of the left!");99}100}101102private static void stopEDT() {103SwingUtilities.invokeLater(new Runnable() {104public void run() {105frame.dispose();106}107});108}109110private static class ParameterTable extends JTable {111public ParameterTable() {112super(new Object[][]{{5}}, new String[]{"Value"});113getColumnModel().getColumn(0).setCellRenderer(new Renderer());114getColumnModel().getColumn(0).setCellEditor(new Editor());115}116}117118private static class Renderer implements TableCellRenderer {119private JSlider slider = new JSlider(0, 10);120121public Component getTableCellRendererComponent(JTable table,122Object value,123boolean isSelected,124boolean hasFocus,125int row, int col) {126int val = (Integer) value;127slider.setValue(val);128return slider;129}130}131132private static class Editor extends AbstractCellEditor implements TableCellEditor {133private JSlider slider = new JSlider(0, 10);134135public Component getTableCellEditorComponent(JTable table, Object value,136boolean isSelected,137int row, int col) {138int val = (Integer) value;139slider.setValue(val);140return slider;141}142143public Editor() {144slider.addChangeListener(new ChangeListener() {145public void stateChanged(ChangeEvent e) {146if (!slider.getValueIsAdjusting()) {147passed = slider.getValue() <= 5;148}149}150});151}152153public Object getCellEditorValue() {154return slider.getValue();155}156}157158private static Rectangle getPanelRectangle() throws Exception{159final Rectangle[] result = new Rectangle[1];160161SwingUtilities.invokeAndWait(new Runnable() {162@Override163public void run() {164result[0] = new Rectangle(panel.getLocationOnScreen(), panel.getSize());165}166});167168return result[0];169}170}171172173