Source code: com/yaftp/utils/SwingStateButton.java
1 /**
2 *
3 * CopyRights Jean-Yves MENGANT 1999,2000,2001,2002
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package com.yaftp.utils;
21
22 import java.awt.* ;
23 import java.awt.event.* ;
24 import javax.swing.* ;
25 import javax.swing.border.* ;
26
27 /**
28
29 Copyright Jean-Yves MENGANT 1998,1999,2000
30
31 Map a short state list inside a JButton using icons
32
33 @author Jean-Yves MENGANT
34
35 */
36
37 public class SwingStateButton
38 extends JButton
39 {
40 private final ImageIcon _TEST1_ = new ImageIcon(
41 getClass().getResource("images/cascade.gif") ,
42 "cascade"
43 ) ;
44 private final ImageIcon _TEST2_ = new ImageIcon(
45 getClass().getResource("images/tiled.gif") ,
46 "tiled"
47 ) ;
48 private final ImageIcon _TEST3_ = new ImageIcon(
49 getClass().getResource("images/jeson.gif") ,
50 "jeson"
51 ) ;
52
53
54 private ImageIcon _icons[] = new ImageIcon[] { _TEST1_ , _TEST2_ , _TEST3_ } ;
55 private int _curState = 0 ;
56
57 public void nextState()
58 {
59 _curState++ ;
60 if ( _curState >= _icons.length )
61 _curState = 0 ;
62 SwingStateButton.this.setIcon(_icons[_curState]) ;
63 }
64
65 public int getCurrentState()
66 { return _curState ; }
67
68 public SwingStateButton( ImageIcon icons[] )
69 {
70 super(icons[0]) ;
71 _icons = icons ;
72 }
73
74 /**
75 class test only constructor
76 */
77 public SwingStateButton()
78 {
79 super() ;
80 super.setIcon(_icons[0]);
81 }
82
83
84 public static void main ( String arg[] )
85 {
86 final SwingStateButton w = new SwingStateButton() ;
87
88 JFrame f = new JFrame("Testing image icons") ;
89 f.setForeground(Color.black) ;
90 f.setBackground(Color.lightGray) ;
91
92 f.addWindowListener(
93 new WindowAdapter() {
94 public void windowClosing( WindowEvent e )
95 { System.exit(0) ; }
96 }
97 ) ;
98
99 w.addActionListener(
100 new ActionListener() {
101 public void actionPerformed( ActionEvent e )
102 { w.nextState() ; }
103 }
104 ) ;
105
106 f.getContentPane().add(BorderLayout.CENTER, w ) ;
107 f.pack() ;
108 f.setVisible(true) ;
109 }
110
111
112 }