1
2 package samplecode;
3
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import java.io.File;
8
9
10 /**
11 * <p>Title: Extract icon </p>
12 * <p>Description: This program shows how to extract a file icon </p>
13 * and display it on a label component.
14 * <p>Company: Developer's super Page.com</p>
15 */
16
17
18 public class SampleFrame extends JFrame {
19 JPanel contentPane;
20 BorderLayout borderLayout1 = new BorderLayout();
21 JPanel jpnlMain = new JPanel();
22 JButton jButton1 = new JButton();
23 JLabel jLabel1 = new JLabel();
24
25 //Construct the frame
26 public SampleFrame() {
27 enableEvents(AWTEvent.WINDOW_EVENT_MASK);
28 try {
29 jbInit();
30 }
31 catch(Exception e) {
32 e.printStackTrace();
33 }
34 }
35
36 //Component initialization
37 private void jbInit() throws Exception {
38 contentPane = (JPanel) this.getContentPane();
39 contentPane.setLayout(borderLayout1);
40 this.setSize(new Dimension(400, 300));
41 this.setTitle("Extract Icon");
42 this.addHierarchyBoundsListener(new
43 SampleFrame_this_hierarchyBoundsAdapter(this));
44 jpnlMain.setLayout(null);
45 contentPane.setMinimumSize(new Dimension(300, 300));
46 contentPane.setPreferredSize(new Dimension(300, 256));
47 jpnlMain.setDebugGraphicsOptions(0);
48 jpnlMain.setMinimumSize(new Dimension(400, 256));
49 jpnlMain.setPreferredSize(new Dimension(400, 256));
50 jButton1.setBounds(new Rectangle(149, 140, 88, 50));
51 jButton1.setText("jButton1");
52 jButton1.addActionListener(new SampleFrame_jButton1_actionAdapter(this));
53 jLabel1.setText("jLabel1");
54 jLabel1.setBounds(new Rectangle(145, 65, 116, 44));
55 contentPane.add(jpnlMain, BorderLayout.CENTER);
56 jpnlMain.add(jButton1, null);
57 jpnlMain.add(jLabel1, null);
58 }
59
60 //Overridden so we can exit when window is closed
61 protected void processWindowEvent(WindowEvent e) {
62 super.processWindowEvent(e);
63 if (e.getID() == WindowEvent.WINDOW_CLOSING) {
64 System.exit(0);
65 }
66 }
67
68
69 void jButton1_actionPerformed(ActionEvent e) {
70 //this function extract an icon from a file and sets it to a label
71 JFileChooser chooser = new JFileChooser();
72 // Create a File instance of the file
73 File file = new File("c:\\test.txt");
74 // Get the icon
75 Icon icon = chooser.getIcon(file);
76 // Some sample code to display the icon in a window
77 jLabel1.setIcon(icon);
78 //set the name of the file to the text of the label component
79 jLabel1.setText(file.getName());
80
81 }
82
83 }
84
85 class SampleFrame_this_hierarchyBoundsAdapter extends
86 java.awt.event.HierarchyBoundsAdapter {
87 SampleFrame adaptee;
88
89 SampleFrame_this_hierarchyBoundsAdapter(SampleFrame adaptee) {
90 this.adaptee = adaptee;
91 }
92
93 }
94
95 class SampleFrame_jButton1_actionAdapter implements java.awt.event.ActionListener {
96 SampleFrame adaptee;
97
98 SampleFrame_jButton1_actionAdapter(SampleFrame adaptee) {
99 this.adaptee = adaptee;
100 }
101 public void actionPerformed(ActionEvent e) {
102 adaptee.jButton1_actionPerformed(e);
103 }
104 }
105
|