*设为首页
*加入收藏
热门关键字: JAVA
>>当前位置:Java大本营>Applet与Swing>文章内容
在SWING 的 JFileChooser中俘获返回的事件
作者: 发布时间:2008-01-20 20:50:57

JFileChoose 可以为我们的开发 GUI界面带来很大的方便,在选择对话框时只需要使用就完事了。选择好文件后一般都需要做一些事情,比如说触发一些事件来做其他功能。事实上通过研读JFileChoose,我们拥有两种手段来激发选择文件后的事件。下面以例子加以说明。对于程序员来说,demo是最好的老师。只需要拷贝本例子就完全可以运行

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class MyChoose extends JFrame {

 private static final long serialVersionUID = 1L;
 private JFileChooser jFileChooser = null;
 private JButton jButton = null;
 
 private JFileChooser getJFileChooser() {
  if (jFileChooser == null) {
   jFileChooser = new JFileChooser();
   jFileChooser.setDialogType(JFileChooser.SAVE_DIALOG);

//by linstener
   jFileChooser.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
     System.out.println("action Commmand:"+ e.getActionCommand());
     if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION))
     {
      System.out.println("select button action");//here call your function
     }
     else  if (e.getActionCommand().equals(JFileChooser.CANCEL_SELECTION))
     {
      System.out.println("cancel button action");//here call your function
     }
    }
   });
  }
  

//by return result
  / /or use below function  ,control your code
  int result = jFileChooser.showOpenDialog(this);
  System.out.println("Result......"+result);
 
  if ( JFileChooser.CANCEL_OPTION ==result)
  {
   
   System.out.println(" FileChooser.CANCEL_OPTION..."+result);//here call your function
  }
  else if ( JFileChooser.APPROVE_OPTION ==result)
  {
   System.out.println(" JFileChooser.APPROVE_OPTION.."+result);//here call your function
   
  }
  else if ( JFileChooser.ERROR_OPTION ==result)
  {
   System.out.println(" JFileChooser.ERROR_OPTION..."+result);//here call your function
   
  }
   
  return jFileChooser;
 }

 /**
  * This method initializes jButton 
  *  
  * @return javax.swing.JButton 
  */
 private JButton getJButton() {
  if (jButton == null) {
   jButton = new JButton("Open ChoiceDialog");
//   jButton.setBounds(new Rectangle(15, 97, 34, 10));
   jButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent e) {
     getJFileChooser();
    }
   });
  }
  return jButton;
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
   public void run() {
    MyChoose thisClass = new MyChoose();
    thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    thisClass.setVisible(true);
   }
  });
 }

 /**
  * This is the default constructor
  */
 public MyChoose() {
  super();
  initialize();
 }

 /**
  * This method initializes this
  *
  * @return void
  */
 private void initialize() {
  this.setSize(300, 200);
//  this.setLayout(null);
//  this.add(getJButton(), null);
  this.setLayout( new BorderLayout() );
  this.add("Center",getJButton());
 }

 


· SWT树节点上弹出菜单的实现 (02-05)
·基于 JSF+Spring + JPA 构建敏捷的Web(02-01)
·SWT中设置TableViewer和TreeViewer为双(01-19)
·Spring之特殊字符转义和方法入参检测工(01-16)
·spring2.5 + hibernate3.2x 标注(annot(01-13)
·spring2.5 + hibernate3.2x 标注(annot(01-13)
·SWT中的FormLayout (01-10)
·Spring基础(01-10)
·Spring 让 LOB 数据操作简单化 (12-26)
·Swing中使用Html标签 (12-26)
·Swing中使用Html标签 (12-26)
·JavaSwing中的对话框(12-24)
复制地址发给您的好友: [推荐文章] [返回顶部] [关闭窗口]
版权所有 Java大本营 1999-2007 转载请注明出处