Path: blob/master/test/jdk/javax/swing/JToggleButton/4128979/bug4128979.java
41153 views
/*1* Copyright (c) 1998, 2021, 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 412897925@requires (os.family == "windows")26@summary Tests that background changes correctly in WinLF for JToggleButton when pressed27@key headful28@run applet/manual=yesno bug4128979.html29*/3031import javax.swing.BorderFactory;32import javax.swing.BoxLayout;33import javax.swing.Icon;34import javax.swing.JApplet;35import javax.swing.JFrame;36import javax.swing.JLabel;37import javax.swing.JOptionPane;38import javax.swing.JPanel;39import javax.swing.JToggleButton;40import javax.swing.JToolBar;41import javax.swing.UIManager;42import javax.swing.UnsupportedLookAndFeelException;43import javax.swing.WindowConstants;44import java.awt.BorderLayout;45import java.awt.Color;46import java.awt.Component;47import java.awt.ComponentOrientation;48import java.awt.Container;49import java.awt.FlowLayout;50import java.awt.Graphics;5152public class bug4128979 extends JApplet {5354public static void main(String[] args) {55JApplet applet = new bug4128979();56applet.init();57applet.start();5859JFrame frame = new JFrame("Test window");60frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);61frame.setLayout(new BorderLayout());62frame.add(applet, BorderLayout.CENTER);63frame.setSize(600, 240);64frame.setVisible(true);65}6667public void init() {68try {69UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");70} catch (UnsupportedLookAndFeelException e) {71JOptionPane.showMessageDialog(this,72"This test requires Windows look and feel, so just press Pass\n as "+73" this look and feel is unsupported on this platform.",74"Unsupported LF", JOptionPane.ERROR_MESSAGE);75return;76} catch (Exception e) {77throw new RuntimeException("Couldn't set look and feel");78}7980setLayout(new FlowLayout());81JPanel p = new JPanel();82p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));8384JPanel p1 = new JPanel();85addButtons(p1);86p.add(p1);8788JToolBar tb1 = new JToolBar();89addButtons(tb1);90p.add(tb1);9192JToolBar tb2 = new JToolBar();93tb2.setRollover(true);94addButtons(tb2);95p.add(tb2);9697JLabel label = new JLabel("ToggleButton.highlight color: ");98label.setComponentOrientation( ComponentOrientation.RIGHT_TO_LEFT);99label.setIcon(new Icon() {100public void paintIcon(Component c, Graphics g, int x, int y) {101g.setColor(UIManager.getColor("ToggleButton.highlight"));102g.fillRect(x, y, 49, 49);103g.setColor(new Color(~UIManager.getColor("ToggleButton.highlight").getRGB()));104g.drawRect(x, y, 49, 49);105}106107public int getIconWidth() {108return 50;109}110111public int getIconHeight() {112return 50;113}114});115add(p);116add(label);117}118119static void addButtons(Container c) {120c.setLayout(new FlowLayout());121122c.add(new JToggleButton("DefaultBorder"));123124JToggleButton cbut = new JToggleButton("DefaultBorder");125cbut.setBackground(Color.red);126c.add(cbut);127cbut = new JToggleButton("DefaultBorder");128cbut.setBackground(Color.green);129c.add(cbut);130cbut = new JToggleButton("DefaultBorder");131cbut.setBackground(Color.blue);132c.add(cbut);133134JToggleButton but3 = new JToggleButton("LineBorder");135but3.setBorder(BorderFactory.createLineBorder(Color.red));136c.add(but3);137138JToggleButton but4 = new JToggleButton("null border");139but4.setBorder(null);140c.add(but4);141}142}143144145