Java Tutorial/Swing/JToggleButton
Содержание
- 1 Creating JToggleButton Components
- 2 Customizing JToggleButton Look and Feel
- 3 JToggleButton is a button that has two states. Pressed and not pressed.
- 4 Listening to JToggleButton Events with a ChangeListener
- 5 Listening to JToggleButton Events with an ActionListener
- 6 Listening to JToggleButton Events with an ItemListener
- 7 The event sequence for JToggleButton: ActionListener, ItemListener and ChangeListener
- 8 This class represents the toggle buttons used in toolbars.
Creating JToggleButton Components
Eight constructors are available for JToggleButton:
public JToggleButton()
JToggleButton aToggleButton = new JToggleButton();
public JToggleButton(Icon icon)
JToggleButton aToggleButton = new JToggleButton(new DiamondIcon(Color.PINK))
public JToggleButton(Icon icon, boolean selected)
JToggleButton aToggleButton = new JToggleButton(new DiamondIcon(Color.PINK), true);
public JToggleButton(String text)
JToggleButton aToggleButton = new JToggleButton("Sicilian");
public JToggleButton(String text, boolean selected)
JToggleButton aToggleButton = new JToggleButton("Thin Crust", true);
public JToggleButton(String text, Icon icon)
JToggleButton aToggleButton = new JToggleButton("Thick Crust", new DiamondIcon(Color.PINK));
public JToggleButton(String text, Icon icon, boolean selected)
JToggleButton aToggleButton = new JToggleButton("Stuffed Crust", new DiamondIcon(Color.PINK), true);
public JToggleButton(Action action)
Action action = ...;
JToggleButton aToggleButton = new JToggleButton(action);
Customizing JToggleButton Look and Feel
Property StringObject TypeToggleButton.backgroundColorToggleButton.borderBorderToggleButton.darkShadowColorToggleButton.disabledTextColorToggleButton.focusColorToggleButton.focusInputMapObject[ ]ToggleButton.fontFontToggleButton.foregroundColorToggleButton.gradientListToggleButton.highlightColorToggleButton.lightColorToggleButton.marginInsetsToggleButton.selectColorToggleButton.shadowColorToggleButton.textIconGapIntegerToggleButton.textShiftOffsetIntegerToggleButtonUIString
JToggleButton is a button that has two states. Pressed and not pressed.
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
public class ToggleButton extends JDialog implements ActionListener {
private JToggleButton red = new JToggleButton("red");
private JToggleButton green = new JToggleButton("green");
private JToggleButton blue = new JToggleButton("blue");
private JPanel display = new JPanel();
public ToggleButton() {
JPanel bottom = new JPanel();
JPanel leftPanel = new JPanel();
red.addActionListener(this);
green.addActionListener(this);
blue.addActionListener(this);
leftPanel.add(red);
leftPanel.add(green);
leftPanel.add(blue);
bottom.add(leftPanel);
display.setBackground(Color.black);
bottom.add(display);
add(bottom);
pack();
setResizable(false);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new ToggleButton();
}
public void actionPerformed(ActionEvent e) {
Color color = display.getBackground();
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
if (e.getActionCommand() == "red") {
if (red == 0) {
red = 255;
} else {
red = 0;
}
}
if (e.getActionCommand() == "green") {
if (green == 0) {
green = 255;
} else {
green = 0;
}
}
if (e.getActionCommand() == "blue") {
if (blue == 0) {
blue = 255;
} else {
blue = 0;
}
}
Color setCol = new Color(red, green, blue);
display.setBackground(setCol);
}
}
Listening to JToggleButton Events with a ChangeListener
Listen to the changes for its armed, pressed, and selected properties
import java.awt.BorderLayout;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class JToggleButtonChangeListener {
public static void main(String args[]) {
JFrame frame = new JFrame("Selecting Toggle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToggleButton toggleButton = new JToggleButton("Toggle Button");
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
ButtonModel buttonModel = abstractButton.getModel();
boolean armed = buttonModel.isArmed();
boolean pressed = buttonModel.isPressed();
boolean selected = buttonModel.isSelected();
System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
}
};
// Attach Listeners
toggleButton.addChangeListener(changeListener);
frame.add(toggleButton, BorderLayout.NORTH);
frame.setSize(300, 125);
frame.setVisible(true);
}
}
Listening to JToggleButton Events with an ActionListener
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class JToggleButtonAction {
public static void main(String args[]) {
JFrame frame = new JFrame("Selecting Toggle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToggleButton toggleButton = new JToggleButton("Toggle Button");
// Define ActionListener
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
System.out.println("Action - selected=" + selected + "\n");
}
};
// Attach Listeners
toggleButton.addActionListener(actionListener);
frame.add(toggleButton, BorderLayout.NORTH);
frame.setSize(300, 125);
frame.setVisible(true);
}
}
Listening to JToggleButton Events with an ItemListener
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class JToggleButtonItemListener {
public static void main(String args[]) {
JFrame frame = new JFrame("Selecting Toggle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToggleButton toggleButton = new JToggleButton("Toggle Button");
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
int state = itemEvent.getStateChange();
if (state == ItemEvent.SELECTED) {
System.out.println("Selected");
} else {
System.out.println("Deselected");
}
}
};
// Attach Listeners
toggleButton.addItemListener(itemListener);
frame.add(toggleButton, BorderLayout.NORTH);
frame.setSize(300, 125);
frame.setVisible(true);
}
}
The event sequence for JToggleButton: ActionListener, ItemListener and ChangeListener
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class JToggleButtonEventListenerSequence {
public static void main(String args[]) {
JFrame frame = new JFrame("Selecting Toggle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToggleButton toggleButton = new JToggleButton("Toggle Button");
// Define ActionListener
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
System.out.println("Action - selected=" + selected + "\n");
}
};
// Define ChangeListener
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
ButtonModel buttonModel = abstractButton.getModel();
boolean armed = buttonModel.isArmed();
boolean pressed = buttonModel.isPressed();
boolean selected = buttonModel.isSelected();
System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
}
};
// Define ItemListener
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
int state = itemEvent.getStateChange();
if (state == ItemEvent.SELECTED) {
System.out.println("Selected");
} else {
System.out.println("Deselected");
}
}
};
// Attach Listeners
toggleButton.addActionListener(actionListener);
toggleButton.addChangeListener(changeListener);
toggleButton.addItemListener(itemListener);
frame.add(toggleButton, BorderLayout.NORTH);
frame.setSize(300, 125);
frame.setVisible(true);
}
}
Changed: true/false/false Changed: true/true/false Changed: true/true/true Selected Changed: true/false/true Action - selected=true Changed: false/false/true
This class represents the toggle buttons used in toolbars.
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
/**
* This class represents the toggle buttons used in toolbars.
*
* @version $Id: JToolbarButton.java 498555 2007-01-22 08:09:33Z cam $
*/
public class JToolbarToggleButton extends JToggleButton {
/**
* Creates a new toolbar button.
*/
public JToolbarToggleButton() {
initialize();
}
/**
* Creates a new toolbar button.
* @param txt The button text.
*/
public JToolbarToggleButton(String txt) {
super(txt);
initialize();
}
/**
* Initializes the button.
*/
protected void initialize() {
if (!System.getProperty("java.version").startsWith("1.3")) {
setOpaque(false);
setBackground(new java.awt.Color(0, 0, 0, 0));
}
setBorderPainted(false);
setMargin(new Insets(2, 2, 2, 2));
// Windows XP look and feel seems to have a bug due to which the
// size of the parent container changes when the border painted
// property is set. Temporary fix: disable mouseover behavior if
// installed lnf is Windows XP
if (!UIManager.getLookAndFeel().getName().equals("Windows")) {
addMouseListener(new MouseListener());
}
}
/**
* To manage the mouse interactions.
*/
protected class MouseListener extends MouseAdapter {
public void mouseEntered(MouseEvent ev) {
setBorderPainted(true);
}
public void mouseExited(MouseEvent ev) {
setBorderPainted(false);
}
}
}