Path: blob/master/test/jdk/javax/swing/JSlider/6587742/bug6587742.java
41153 views
/*1* Copyright (c) 2008, 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/* @test24* @bug 658774225* @summary filling half of a JSlider's track is no longer optional26* @author Pavel Porvatov27* @run applet/manual=done bug6587742.html28*/2930import javax.swing.*;31import javax.swing.plaf.metal.DefaultMetalTheme;32import javax.swing.plaf.metal.MetalLookAndFeel;33import javax.swing.plaf.metal.MetalTheme;34import javax.swing.plaf.metal.OceanTheme;35import java.awt.event.ItemEvent;36import java.awt.event.ItemListener;3738public class bug6587742 extends JApplet {39public void init() {40TestPanel panel = new TestPanel();4142setContentPane(panel);43}4445private class TestPanel extends JPanel {46private final JComboBox cbThemes = new JComboBox();4748private TestPanel() {49// Fill cbThemes50cbThemes.addItem(new OceanTheme());51cbThemes.addItem(new DefaultMetalTheme());5253cbThemes.addItemListener(new ItemListener() {54public void itemStateChanged(ItemEvent e) {55MetalTheme theme = (MetalTheme) cbThemes.getSelectedItem();5657if (theme != null) {58MetalLookAndFeel.setCurrentTheme(theme);5960// re-install the Metal Look and Feel61try {62UIManager.setLookAndFeel(new MetalLookAndFeel());63} catch (UnsupportedLookAndFeelException e1) {64JOptionPane.showMessageDialog(TestPanel.this, "Can't change theme: " + e1.getMessage(),65"Error", JOptionPane.ERROR_MESSAGE);6667return;68}6970SwingUtilities.updateComponentTreeUI(bug6587742.this);71}72}73});7475JPanel pnVertical = new JPanel();7677pnVertical.setLayout(new BoxLayout(pnVertical, BoxLayout.Y_AXIS));7879for (int i = 0; i < 12; i++) {80int filled = i >> 2;8182pnVertical.add(createSlider(false, filled > 1 ? null : Boolean.valueOf(filled == 1), (i & 2) == 0,83(i & 1) != 0));84}8586JPanel pnHorizontal = new JPanel();8788pnHorizontal.setLayout(new BoxLayout(pnHorizontal, BoxLayout.X_AXIS));8990for (int i = 0; i < 12; i++) {91int filled = i >> 2;9293pnHorizontal.add(createSlider(true, filled > 1 ? null : Boolean.valueOf(filled == 1), (i & 2) == 0,94(i & 1) != 0));95}9697JTabbedPane tpSliders = new JTabbedPane();9899tpSliders.add("Vertical sliders", pnVertical);100tpSliders.add("Horizontal sliders", pnHorizontal);101102setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));103104add(new JLabel("Select theme:"));105add(cbThemes);106add(tpSliders);107}108}109110private static JSlider createSlider(boolean vertical, Boolean filled, boolean enabled, boolean inverted) {111JSlider result = new JSlider(vertical ? SwingConstants.VERTICAL : SwingConstants.HORIZONTAL, 0, 100, 50);112113result.setMajorTickSpacing(20);114result.setMinorTickSpacing(5);115result.setPaintTicks(true);116result.setPaintLabels(true);117result.setEnabled(enabled);118119if (filled != null) {120result.putClientProperty("JSlider.isFilled", filled);121}122123result.setInverted(inverted);124result.setToolTipText("<html>vertical = " + vertical + "<br>enabled = " + enabled + "<br>filled = " + filled +125"<br>inverted = " + inverted + "</html>");126127return result;128}129}130131132