Java Drawing DrawTop

Language

JP  US  UK

 

Button

 H. Jyounishi, Tokyo Japan
 

Frame (Index), No frame                 version:0.3(latest)  



Summary:This page describes defining the JToggleButton(ButtonOfToggle). In addition, defines accelerator dedicated button (ButtonOfAccelerators) whose size is 0 in width and height, so it can't be seen on the tool bar.

Relevant major classes of Java SE: java.awt.*, java.awt.font.*, java.awt.event.*, javax.swing.* etc.

Classes on this page: ButtonOfToggle, ButtonOfToggleAction, ButtonOfAccelerators, AcceleratorsAction
Special remark: Problems of setting accelerators
1. Class ButtonOfToggle return=>page top

public class ButtonOfToggle extends JToggleButton implements ItemListener
 

Field

Description

action

ButtonOfToggelAction action=new ButtonOfToggelAction();

The ActionListener of this button.

raisedBorder

protected Border raisedBorder=new BevelBorder(BevelBorder.RAISED);

The border of unselected button.

loweredBorder

protected Border loweredBorder=new BevelBorder(BevelBorder.LOWERED);

The border of selected button.

imageEnabled

ImageIcon imageEnabled

The image on the enabled button.
=>itemStateChanged

imageDisabled

ImageIcon imageDisabled

The image on the disabled button.


Method

Description

Constructor

public ButtonOfToggle(String commandName, String tip, String[] accelerators)

Creates a button with the specified text (commandName) on the button.

Parameters:

commandName - The command name.
tip - The text to be displayed in a tool tip.
accelerators - The array of the strings representing accelerators.

Processing:

Calls the following methods.

this.setActionCommand(commandName);
this.addActionListener(this.action);
this.setName(commandName);
this.setText(commandName);
this.setToolTipText(tip);
this.setStandardButtonStyle();
this.setAccelerators(accelerators);
this.addItemListener(this);

Constructor

public ButtonOfToggle(String commandName, boolean setText, ImageIcon imageEnabled, ImageIcon imageDisabled, String tip, String[] accelerators)

Creates a button with the specified imageIcon on the button. Display the text (commandName) with the icon on the button, if the setText is true.

Parameters:

commandName - The command name.
setText - If true, displays the commandName on the button.
imageEnabled - The ImageIcon to be displayed on the enabled button.
imageDisabled - The ImageIcon to be displayed on the disabled button.
tip - The text to be displayed in a tool tip.
accelerators - The array of the strings representing accelerators.

Processing:

Calls the following methods.

super(imageDisabled, false);
this.setActionCommand(commandName);
this.addActionListener(this.action);
this.setName(commandName);
this.imageEnabled=imageEnabled;
this.imageDisabled=imageDisabled;
if(setText) {
this.setText(commandName);
this.setHorizontalTextPosition(SwingConstants.LEFT);
}
this.setToolTipText(tip);
this.setStandardButtonStyle();
this.setAccelerators(accelerators);
this.addItemListener(this);

setStandardButtonStyle

public void setStandardButtonStyle()

Calls the following methods.

this.setBorder(this.raisedBorder);
this.setIconTextGap(0);
this.setHorizontalTextPosition(SwingConstants.CENTER );
this.setVerticalTextPosition(SwingConstants.BOTTOM );
this.setHorizontalAlignment(CENTER);
this.setFont(MenuConstants.MenuFont);
this.setBackground(null);
this.setForeground(Color.BLACK);

setAccelerators

public void setAccelerators(String[] accelerators)

Sets the accelerators to this button in the following manner.

int size=0;
if(accelerators!=null) size=accelerators.length;
for(int i=0;i<size;i++){
KeyStroke stroke = KeyStroke.getKeyStroke(accelerators[i]);
InputMap inputMap = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(stroke, accelerators[i]);
ActionMap actionMap = this.getActionMap();
actionMap.put(accelerators[i], this.action);
}

=>Problems of setting accelerators

setEnabled

public void setEnabled(boolean enable)

If the enable is true, sets the imageEnabled to this button, otherwise sets the imageDisabled.

Example: undo, redo button.
The current state of the left button is enable and that of the right button is disable.

 

itemStateChanged

public void itemStateChanged(ItemEvent e)

If the button is selected, displays the loweredBorder, otherwise the raisedBorder.

Example: open button

raisedBorderloweredBorder

createButton

(static)

public static ButtonOfToggle createButton(String commandName, int width, String tip, String[] accelerators)

Parameters:
commandName - The command name.
width - The button width.
If the width<0, then the button width is determined by using the TextLayout.
tip - The text to be displayed in a tool tip.
accelerators - The array of the strings representing accelerators.
If the accelerators equals null, then an accelerator isn't set to this button.

Processing:

Creates a new button object by the first Constructor.

createButton

(static)

public static ButtonOfToggle createButton(String commandName, boolean setText, String imageName, String tip)

Parameters:
commandName - The command name.
setText - If true, displays the commandName at the right side of the ImageIcon on the button.
imageName - The image name if the ImageIcon.
The imageName doesn't need to include the file path.
tip - The text to be displayed in a tool tip.

Processing:

Creates a new button object by the second Constructor. The button displays the image specified by the imageName on the button.

createButton

(static)

public static ButtonOfToggle createButton(String commandName, String imageEnabled, String imageDisabled, String tip)

Parameters:
commandName - The command name.
setText - If true, displays the commandName at the right side of the ImageIcon on the button.
imageEnabled - The image name on the enabled button.
imageDisabled - The image name on the disabled button.
tip - The text to be displayed in a tool tip.

Processing:

Creates a new button object by the second Constructor. The button displays the image specified by the imageEnabled or the imageDisabled on the button without text.



2. ButtonOfToggleAction return=>page top

class ButtonOfToggelAction extends AbstractAction

Method

Description

actionPerformed

public void actionPerformed(ActionEvent e)

∙ The action command

The action command can be got by the following line.
String commandName=e.getActionCommand();
If the action is caused by pressing keyboard accelerator, then the commandName (return value of the getActionCommand) is an ASCII control code. The string representation of that such as "ctrl h" or "DELETE" can be got by the Util.getASCIIControlString method.
On the other hand, if the action is caused by clicking this button, the commandName is a normal string which was passed to the constructor of the ButtonOfToggle. in this case, the Util.getASCIIControlString method returns "".

∙ Generates the command

Generates a Command object according to the action command and calls the exec method of the ExecCommand with the Command object.



3. Class ButtonOfAccelerators return=>page top

public class ButtonOfAccelerators extends JButton

This button is defined for processing keyboard accelerators such as ctrl+x, ctrl+c, ctrl+v, ctrl+a, Delete key etc. The button size of this object is 0 in width and height, so the button can't be seen on the tool bar, however it works like an accelerator transponder.
ctrl+x, ctrl+c, ctrl+v: Press the x/c/v key with holding down the Crtl key.

Method

Description

Constructor

public ButtonOfAccelerators(String commandName, String[] accelerators)

Parenmeters:

commandName - The command name.

accelerators - The array of the strings representing accelerators.
"ctrl x", "ctrl c", "ctrl v", "ctrl h"(Back space), "DELETE" etc.

=>Problems of setting accelerators

Processing:

this.setName(commandName);
this.setActionCommand(commandName);

setAccelerators

public void setAccelerators(String accelerators)

Parenmeters:

accelerators - The array of the strings representing accelerators.
"ctrl X", "ctrl C", "ctrl V", "ctrl H"(Back space), "DELETE" etc.

Processing:

Sets the accelerators to this button by using javax.swing.KeyStroke and javax.swing.InputMap and javax.swing.ActionMap as follow.

KeyStroke stroke = KeyStroke.getKeyStroke(accelerators[i]);
InputMap inputMap = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(stroke, accelerators[i]);
ActionMap actionMap = this.getActionMap();
actionMap.put(accelerators[i], action);

=>Problems of setting accelerators

createEditAccelerators

(static)

public static ButtonOfAccelerators createEditAccelerators(String commandName)

Parameter:
commandName - The command name.

Processing:

Creates a new ButtonOfAccelerators object and returns it.

The following parameter is passed to the constructor.

String[] accelerators={"ctrl x", "ctrl c", "ctrl v", "ctrl a", "delete"};



4. AcceleratorsAction return=>page top

class AcceleratorsAction extends AbstractAction

Method

Description

actionPerformed

public void actionPerformed(ActionEvent e)

∙ The action command

The action command can be got by the following line.
String commandName=e.getActionCommand();
Here the commandName consists of an ASCII control code that is usually undefined character in Java. The string representation of the ASCII control code such as "ctrl X", "ctrl C", "ctrl V", "ctrl A" or "delete". can be got by the Util.getASCIIControlString method.

∙ Generates the command

Generates a Command object according to the string representation of the ASCII control code and calls the exec method of the ExecCommand with the Command object.



>: Problems of setting accelerators return=>page top

sets the accelerators such as ctrl c(copy), ctrl x(cut), ctrl v(paste), typed \b(backspace) etc. to a button.

Problem

Description

(a)Setting

=> "Drag Picture Demo" http://www.java2s.com/Code/Java/Swing-JFC/DragPictureDemo.htm

Use the javax.swing.InputMap and javax.swing.ActionMap.

(b)TabbedPane

problem
In this application, the three TabbedPane - Home, Shape, Help - are used to shows the menus such as buttons, pull down menus and so on.

For example, the accelerators are not defined on the menus of Home tab, so the accelerators can't be used in the Home tab.

Solution
Place the special button (ButtonOfAccelerators) on the Home tab. The size of the button is 0 in width and height, so it can't be seen , but it works as accelerator receiver.

(c)Action command

problem

If the accelerators are set to the button in the manner described in (a), then the ActionEvent.getActionCommand method returns the ASCII control code such as ctrl C(0x03), ctrl X(0x18) or son on. So the ASCII code check method is needed in the actionPerformed method.

Solution

=> ButtonOfToggleAction.actionPerFomed, AcceleratorsAction.actionPerFomed, Util.getASCIIControlString method.



Copyright (c) 2009-2013
All other trademarks are property of their respective owners.