Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. package gui;
  2.  
  3. import java.awt.Dimension;
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12. import javax.swing.JTextArea;
  13. import javax.swing.JTree;
  14. import javax.swing.SwingUtilities;
  15.  
  16.  
  17. public class FileToFrame extends JFrame {
  18.    
  19.     private static final long serialVersionUID = 1L;
  20.  
  21.     public FileToFrame() {
  22.  
  23.         initUI();
  24.     }
  25.  
  26.     public final void initUI() {
  27.  
  28.         JPanel panel = new JPanel();
  29.  
  30.         JTextArea area = new JTextArea();
  31.         //area.append("blablabl");
  32.         area.setPreferredSize(new Dimension(100, 100));
  33.         area.setEditable(false);
  34.         area.setLineWrap(true);
  35.        
  36.         String line = null, test ="file.txt";        
  37.         FileReader citim = null;
  38.         BufferedReader br = null;      
  39.         try {
  40.             File file = new File(test);
  41.             citim = new FileReader(file);
  42.             br = new BufferedReader(citim);
  43.             while((line = br.readLine()) != null) {              
  44.                 area.append(line);                      // copy from file to JTextArea
  45.             }
  46.         } catch (Exception e) {
  47.             System.out.println("Nu gasesc fisierul "+test);
  48.         } finally {
  49.             if(br != null) {
  50.                 try {
  51.                     br.close();
  52.                 } catch (IOException e) {
  53.                     e.printStackTrace();
  54.                 }
  55.             }
  56.         }
  57.  
  58.         panel.add(area);
  59.         add(panel);
  60.         pack();
  61.  
  62.         setTitle("FlowLayout Example");
  63.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64.         setLocationRelativeTo(null);
  65.     }
  66.  
  67.     public static void main(String[] args) {        
  68.  
  69.         SwingUtilities.invokeLater(new Runnable() {
  70.  
  71.             public void run() {
  72.  
  73.                 FileToFrame ex = new FileToFrame();
  74.                 ex.setVisible(true);
  75.             }
  76.         });
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement