Buttonmodel class
Buttonmodel maintains three types of status information: whether it is pressed, whether it is "armed" (armed), and whether it is selected (selected ). They are all boolean values.
When the mouse is on the button, the mouse is pressed but the mouse is not released, at this time, the user immediately dragged the mouse outside the button and did not change this status.
Whether a button is "armed" means that the button is pressed and the mouse is still on it.
Some buttons may also be selected. In this status, you can click the button repeatedly to obtain the value true or false.
The following code is a default implementation of the status pressed. The Code of status armed and selected implementation is similar to that of status armed. The buttonmodel class should be inherited to overwrite the default state definition and implement personalized buttons.
private boolean boolPressed = false;public boolean isPressed(){ return boolPressed;}public void setPressed(boolean boolPressed){ this.boolPressed = boolPressed; fireChangeEvent(new ChangeEvent(button));}
|
The button model of the button also notifies other objects (Event Listeners) of events they are interested in. We can see from the buy on behalf below that a changeevent will be sent when the turntable of the button changes. The following code is used:
private Vector vectorChangeListeners = new Vector();public void addChangeListener(ChangeListener changelistener){ vectorChangeListeners.addElement(changelistener);}public void removeChangeListener(ChangeListener changelistener){ vectorChangeListeners.removeElement(changelistener);}protected void fireChangeEvent(ChangeEvent changeevent){ Enumeration enumeration = vectorChangeListeners.elements(); while (enumeration.hasMoreElements()) { ChangeListener changelistener =(ChangeListener)enumeration.nextElement(); changelistener.stateChanged(changeevent); }}
|
Before entering the next section, you should spend more time reading the source code of the buttonmodel class.
Buttonui class
The view/controller of the button is responsible for building the presentation layer. By default, it only draws a rectangle with the background color. Their subclasses inherit from them and overwrite the painting method, so that the buttons can have many different performances, such as motif, Windows 95, java style.
public void update(Button button, Graphics graphics){}public void paint(Button button, Graphics graphics){ Dimension dimension = button.getSize(); Color color = button.getBackground(); graphics.setColor(color); graphics.fillRect(0, 0, dimension.width, dimension.height); }
|
The buttonui class does not handle AWT events by itself. They use a Custom Event listener to translate low-level AWT events into semantic events expected by the Advanced button model. The following is the code to install/uninstall the event listener.
private static ButtonUIListener buttonuilistener = null;public void installUI(Button button){ button.addMouseListener(buttonuilistener); button.addMouseMotionListener(buttonuilistener); button.addChangeListener(buttonuilistener);}public void uninstallUI(Button button){ button.removeMouseListener(buttonuilistener); button.removeMouseMotionListener(buttonuilistener); button.removeChangeListener(buttonuilistener);}
|
View/controller is actually some method. They do not maintain any status information. Therefore, many button instances can share a buttonui instance. Buttonui identifies different buttons by adding a button reference to the parameter list.
Similarly, we hope you can spend more time looking at the buttonui class and then proceed to the next section.
Buttonuilistener class
The buttonuilistener class can help the button class to convert the mouse or keyboard input into operations on the button model. This listener class implements the mouselistener, mousemotionlistener, and changelistener interfaces and handles the following events:
public void mouseDragged(MouseEvent mouseevent){ Button button = (Button)mouseevent.getSource(); ButtonModel buttonmodel = button.getModel(); if (buttonmodel.isPressed()) { if (button.getUI().contains(button, mouseevent.getPoint())) { buttonmodel.setArmed(true); } else { buttonmodel.setArmed(false); } }}public void mousePressed(MouseEvent mouseevent){ Button button = (Button)mouseevent.getSource(); ButtonModel buttonmodel = button.getModel(); buttonmodel.setPressed(true); buttonmodel.setArmed(true);}public void mouseReleased(MouseEvent mouseevent){ Button button = (Button)mouseevent.getSource(); ButtonModel buttonmodel = button.getModel(); buttonmodel.setPressed(false); buttonmodel.setArmed(false);}public void stateChanged(ChangeEvent changeevent){ Button button = (Button)changeevent.getSource(); button.repaint();}
|
Summary
I hope you can follow the methods described above. If not, all efforts will be in vain. The benefit of this example and swing user interface components is that you don't have to spend time figuring out how they are designed and implemented at the underlying layer to easily use them. They all provide the default model and view/controller. Then, when you build your own components, you will find the power of the above ideas.