View Javadoc

1   /*
2    * 
3    * 
4    */
5   package oscon2006.swing;
6   
7   import java.awt.event.ActionEvent;
8   import java.io.File;
9   import java.io.FileOutputStream;
10  import javax.swing.*;
11  
12  public class OpenAction extends AbstractAction
13  {
14  	
15  	private final JTable table;
16  	
17  	public OpenAction(JTable t)
18  	{
19  		super("Open...");
20  		this.table = t;
21  	}
22  	
23  	public void actionPerformed(ActionEvent e)
24  	{
25  		JFileChooser chooser = new JFileChooser("/demo");
26  		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
27  		chooser.setFileFilter(new XlsFileFilter());
28  		chooser.showOpenDialog(null);
29  		File f = chooser.getSelectedFile();
30  		if (f != null)
31  		{
32  			try
33  			{
34  				WorkbookTableModel m = new WorkbookTableModel(f);
35  				table.setModel(m);
36  			}
37  			catch (Exception ex)
38  			{
39  				ex.printStackTrace();
40  				JOptionPane.showMessageDialog(null, 
41  								"An error occurred while opening the file.", 
42  								"Error", 
43  								JOptionPane.ERROR_MESSAGE);	
44  			}
45  		}
46  			
47  	}
48  
49  	
50  }