001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb.demo;
022:
023: import java.awt.*;
024: import java.awt.event.*;
025: import java.io.File;
026: import javax.swing.*;
027:
028: /**
029: * Demo for file dialog
030: * @author TiongHiang Lee (thlee@onemindsoft.org)
031: * @version $Id: FileDialogExample.java,v 1.2 2006/08/06 03:32:38 thlee Exp $ $Name: BR_1_x $
032: */
033: public class FileDialogExample extends JFrame {
034:
035: /** the content area **/
036: private JPanel _contentArea = new JPanel();
037:
038: private JDialog _chooserDialog;
039:
040: /**
041: * Constructor
042: */
043: public FileDialogExample() {
044: _contentArea = new JPanel();
045: _contentArea.setLayout(new BorderLayout());
046: _contentArea.add(new JLabel("No content"));
047: //the buttons
048: JPanel buttons = new JPanel();
049: buttons.setLayout(new FlowLayout());
050: JButton b1 = new JButton("Load image with FileDialog");
051: b1.addActionListener(new ActionListener() {
052:
053: /**
054: * {@inheritDoc}
055: */
056: public void actionPerformed(ActionEvent e) {
057: loadFileWithFileDialog();
058: }
059: });
060: JButton b2 = new JButton("Load image with JFileChooser");
061: b2.addActionListener(new ActionListener() {
062:
063: /**
064: * {@inheritDoc}
065: */
066: public void actionPerformed(ActionEvent e) {
067: loadFileWithJFileChooser();
068: }
069: });
070: buttons.add(b1);
071: buttons.add(b2);
072: getContentPane().setLayout(new BorderLayout());
073: getContentPane().add(BorderLayout.NORTH, buttons);
074: getContentPane().add(BorderLayout.CENTER, _contentArea);
075: }
076:
077: /**
078: * Load a file
079: */
080: protected void loadFileWithFileDialog() {
081: FileDialog dialog = new FileDialog(this ,
082: "Choose a jpg, gif file");
083: dialog.setModal(false);
084: dialog.addComponentListener(new ComponentAdapter() {
085:
086: public void componentHidden(ComponentEvent e) {
087: FileDialog d = (FileDialog) e.getSource();
088: if (d.getFile() != null) {
089: viewFile(new File(d.getDirectory(), d.getFile()));
090: }
091: d.dispose();
092: }
093: });
094: dialog.show();
095: }
096:
097: /**
098: * Load a file
099: */
100: protected void loadFileWithJFileChooser() {
101: JFileChooser chooser = new JFileChooser();
102: chooser.addActionListener(new ActionListener() {
103:
104: public void actionPerformed(ActionEvent e) {
105: JFileChooser chooser = (JFileChooser) e.getSource();
106: String state = (String) e.getActionCommand();
107: File file = chooser.getSelectedFile();
108: if (file != null
109: && state.equals(JFileChooser.APPROVE_SELECTION)) {
110: viewFile(file);
111: } else if (state.equals(JFileChooser.CANCEL_SELECTION)) {
112: //do nothing
113: }
114: _chooserDialog.dispose();
115: }
116: });
117: _chooserDialog = new JDialog(this , "Choose an image file...",
118: false);
119: _chooserDialog.getContentPane().add(chooser);
120: _chooserDialog.pack();
121: _chooserDialog.show();
122: }
123:
124: /**
125: * View the file
126: * @param dir
127: * @param file
128: */
129: public void viewFile(File f) {
130: if (f != null) {
131: _contentArea.removeAll();
132: _contentArea.add(new JLabel(new ImageIcon(f
133: .getAbsolutePath())));
134: pack();
135: }
136: }
137:
138: /**
139: * The main program
140: * @param args the arguments
141: */
142: public static void main(String[] args) {
143: FileDialogExample e = new FileDialogExample();
144: e.pack();
145: e.show();
146: }
147: }
|