KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > JXImagePanel


1 /*
2  * $Id: JXImagePanel.java,v 1.1 2004/07/28 21:21:11 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.awt.Dimension JavaDoc;
11 import java.awt.Image JavaDoc;
12 import java.awt.Cursor JavaDoc;
13
14 import java.io.File JavaDoc;
15
16 import java.awt.event.MouseAdapter JavaDoc;
17 import java.awt.event.MouseEvent JavaDoc;
18
19 import javax.swing.BorderFactory JavaDoc;
20 import javax.swing.Icon JavaDoc;
21 import javax.swing.ImageIcon JavaDoc;
22 import javax.swing.JFileChooser JavaDoc;
23 import javax.swing.JLabel JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25
26 import javax.swing.border.BevelBorder JavaDoc;
27
28 public class JXImagePanel extends JPanel JavaDoc {
29
30     private JLabel JavaDoc imageLabel;
31
32     private String JavaDoc TEXT = "<html><i><b>Click on the pane<br>To set the image</b></i></html>";
33
34     public JXImagePanel() {
35     setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
36
37     imageLabel = new JLabel JavaDoc(TEXT);
38     imageLabel.setPreferredSize(new Dimension JavaDoc(200,200));
39     imageLabel.addMouseListener(new MouseHandler());
40     imageLabel.setToolTipText(TEXT);
41     add(imageLabel);
42     }
43
44     public void setIcon(Icon JavaDoc icon) {
45     imageLabel.setIcon(icon);
46     if (icon != null) {
47         imageLabel.setText(null);
48     }
49     else {
50         imageLabel.setText(TEXT);
51     }
52     }
53
54     public Icon JavaDoc getIcon() {
55     return imageLabel.getIcon();
56     }
57
58     public void setImage(Image JavaDoc image) {
59     if (image != null) {
60         setIcon(new ImageIcon JavaDoc(image));
61     } else {
62         setIcon(null);
63     }
64     }
65
66     public Image JavaDoc getImage() {
67     ImageIcon JavaDoc icon = (ImageIcon JavaDoc)imageLabel.getIcon();
68     if (icon != null) {
69         return icon.getImage();
70     }
71     return null;
72     }
73
74     private class MouseHandler extends MouseAdapter JavaDoc {
75     private Cursor JavaDoc oldCursor;
76     private JFileChooser JavaDoc chooser;
77
78     public void mouseClicked(MouseEvent JavaDoc evt) {
79         if (chooser == null) {
80         chooser = new JFileChooser JavaDoc();
81         }
82         int retVal = chooser.showOpenDialog(JXImagePanel.this);
83         if (retVal == JFileChooser.APPROVE_OPTION) {
84         File JavaDoc file = chooser.getSelectedFile();
85         try {
86             setIcon(new ImageIcon JavaDoc(file.toURL()));
87         } catch (Exception JavaDoc ex) {
88         }
89         }
90     }
91
92     public void mouseEntered(MouseEvent JavaDoc evt) {
93         JLabel JavaDoc label = (JLabel JavaDoc)evt.getSource();
94         if (oldCursor == null) {
95         oldCursor = label.getCursor();
96         label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
97         }
98     }
99
100     public void mouseExited(MouseEvent JavaDoc evt) {
101         JLabel JavaDoc label = (JLabel JavaDoc)evt.getSource();
102         if (oldCursor != null) {
103         label.setCursor(oldCursor);
104         oldCursor = null;
105         }
106     }
107     }
108 }
109
110
Popular Tags