September 09, 2010, 01:33:36 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Open and read a file using SWING components.  (Read 857 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
Referrals: 13

WWW Awards
« on: March 21, 2009, 09:17:46 PM »

Code
GeSHi (java):
  1. /*--------------------------------------------------------------------------
  2. OpenFileDemo class
  3.  *****************
  4. By: deAppel http://www.engineeringserver.com
  5. Contact: info [@] engineeringserver.com
  6. Version: 20/07/2008
  7. "*****************
  8. Note: a class that opens and reads a text file.
  9. //----------------------------------------------------------------------*/
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.io.File;
  13. import java.io.FileNotFoundException;
  14. import java.util.Scanner;
  15.  
  16. import javax.swing.JFileChooser;
  17. import javax.swing.JFrame;
  18. import javax.swing.JMenu;
  19. import javax.swing.JMenuBar;
  20. import javax.swing.JMenuItem;
  21. import javax.swing.JScrollPane;
  22. import javax.swing.JTextArea;
  23.  
  24. public class OpenFileDemo extends JFrame implements ActionListener{
  25. JMenuBar mb = new JMenuBar();
  26. JMenu menu = new JMenu("File");
  27. JMenuItem openFile = new JMenuItem("Open file");
  28. JFileChooser chooseFile = new JFileChooser();
  29. JTextArea txtArea = new JTextArea();
  30. JScrollPane scrollPane = new JScrollPane(txtArea);
  31.  
  32. public OpenFileDemo(){
  33. //----------------------------------------------------------------------*/
  34. // Build the menu
  35. //----------------------------------------------------------------------*/
  36. mb.add(menu);
  37. menu.add(openFile);
  38. openFile.addActionListener(this);
  39. setJMenuBar(mb);
  40. //----------------------------------------------------------------------*/
  41. // Create the txtArea
  42. //----------------------------------------------------------------------*/
  43. txtArea.setEditable(false);
  44. scrollPane.setBounds(0,0,700,500);
  45. add(scrollPane);
  46. //----------------------------------------------------------------------*/
  47. // Gui settings
  48. //----------------------------------------------------------------------*/
  49. setLayout(null);
  50. setTitle("OpenFileDemo - www.engineeringserver.com");
  51. setSize(800,600);
  52. setResizable(false);
  53. setVisible(true);
  54. setLocationRelativeTo(null);
  55. setDefaultCloseOperation(EXIT_ON_CLOSE);
  56. }
  57.  
  58. public static void main(String[] args){
  59. new OpenFileDemo();
  60. }
  61.  
  62. public void actionPerformed(ActionEvent e) {
  63. int selected = chooseFile.showOpenDialog(this);
  64.  
  65. if (selected == JFileChooser.APPROVE_OPTION){
  66. File file = chooseFile.getSelectedFile();
  67. try {
  68. Scanner sc = new Scanner(file);
  69. while(sc.hasNextLine()){
  70. String readTheFile = sc.nextLine();
  71. txtArea.append(readTheFile);
  72. txtArea.append("\n");
  73. }
  74. } catch (FileNotFoundException FNFE) {
  75. FNFE.printStackTrace();
  76. }
  77. }
  78. }
  79. }
  80.  
Created by GeSHI 1.0.7.20
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net :: a community about Java software development.
« on: March 21, 2009, 09:17:46 PM »

 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.203 seconds with 32 queries.

Google visited last this page August 30, 2010, 07:39:44 AM