Path: blob/master/test/jdk/javax/swing/JSlider/4987336/bug4987336.java
41153 views
/*1* Copyright (c) 2007, 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*/2223/* @test24@bug 498733625@summary JSlider doesn't show label's animated icon.26@author Pavel Porvatov27@run applet/manual=done bug4987336.html28*/2930import javax.swing.*;31import javax.swing.border.TitledBorder;32import java.awt.event.ActionEvent;33import java.awt.event.ActionListener;34import java.util.Hashtable;3536public class bug4987336 extends JApplet {37private static final String IMAGE_RES = "box.gif";3839private static final String ANIM_IMAGE_RES = "duke.gif";4041public void init() {42JPanel pnLafs = new JPanel();43pnLafs.setLayout(new BoxLayout(pnLafs, BoxLayout.Y_AXIS));4445ButtonGroup group = new ButtonGroup();4647pnLafs.setBorder(new TitledBorder("Available Lafs"));4849for (UIManager.LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels()) {50LafRadioButton comp = new LafRadioButton(lafInfo);5152pnLafs.add(comp);53group.add(comp);54}5556JPanel pnContent = new JPanel();5758pnContent.setLayout(new BoxLayout(pnContent, BoxLayout.Y_AXIS));5960pnContent.add(pnLafs);61pnContent.add(createSlider(true, IMAGE_RES, IMAGE_RES, ANIM_IMAGE_RES, ANIM_IMAGE_RES));62pnContent.add(createSlider(false, IMAGE_RES, IMAGE_RES, ANIM_IMAGE_RES, ANIM_IMAGE_RES));63pnContent.add(createSlider(true, ANIM_IMAGE_RES, null, IMAGE_RES, IMAGE_RES));64pnContent.add(createSlider(false, ANIM_IMAGE_RES, null, IMAGE_RES, IMAGE_RES));6566getContentPane().add(new JScrollPane(pnContent));67}6869private static JSlider createSlider(boolean enabled,70String firstEnabledImage, String firstDisabledImage,71String secondEnabledImage, String secondDisabledImage) {72Hashtable<Integer, JComponent> dictionary = new Hashtable<Integer, JComponent>();7374dictionary.put(0, createLabel(firstEnabledImage, firstDisabledImage));75dictionary.put(1, createLabel(secondEnabledImage, secondDisabledImage));7677JSlider result = new JSlider(0, 1);7879result.setLabelTable(dictionary);80result.setPaintLabels(true);81result.setEnabled(enabled);8283return result;84}8586private static JLabel createLabel(String enabledImage, String disabledImage) {87ImageIcon enabledIcon = enabledImage == null ? null :88new ImageIcon(bug4987336.class.getResource(enabledImage));8990ImageIcon disabledIcon = disabledImage == null ? null :91new ImageIcon(bug4987336.class.getResource(disabledImage));9293JLabel result = new JLabel(enabledImage == null && disabledImage == null ? "No image" : "Image",94enabledIcon, SwingConstants.LEFT);9596result.setDisabledIcon(disabledIcon);9798return result;99}100101private class LafRadioButton extends JRadioButton {102public LafRadioButton(final UIManager.LookAndFeelInfo lafInfo) {103super(lafInfo.getName(), lafInfo.getName().equals(UIManager.getLookAndFeel().getName()));104105addActionListener(new ActionListener() {106public void actionPerformed(ActionEvent e) {107try {108UIManager.setLookAndFeel(lafInfo.getClassName());109110SwingUtilities.updateComponentTreeUI(bug4987336.this);111} catch (Exception ex) {112// Ignore such errors113System.out.println("Cannot set LAF " + lafInfo.getName());114}115}116});117}118}119}120121122