Monday, 3 November 2014

Write a java program to select the name of a text file and display it in the text field. Display the content of the file in a text area when the user clicks on View.(Use FileChooser).




Name : Shreyal Mandot

Title : Assignment 7a1. Write a java program to select the name of a text file and display it in the text field. Display the content of the file in a text area when the user clicks on View.(Use FileChooser).



import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class ass7a1 extends JFrame implements ActionListener
{
     
private JLabel l1;
private JButton b1,b2;
    private JTextField t1;
    private JTextArea a1;
    private JPanel p1;
    private JScrollBar s1;
private JFileChooser jc;

    public ass7a1(String s)
    {
 
    super(s);
             
        l1=new JLabel("File Name");
     
        b1=new JButton("Browse");
        b1.addActionListener(this);
     
        b2=new JButton("View");
        b2.addActionListener(this);
             
        jc=new JFileChooser();
     
        t1=new JTextField(20);
     
        a1=new JTextArea(10,20);
     
        p1=new JPanel();
     
        s1=new JScrollBar(JScrollBar.VERTICAL);

        p1.setLayout(new GridLayout(2,2));
        p1.add(l1);p1.add(t1);
            p1.add(b1);p1.add(b2);
            a1.add(s1);

        setLayout(new BorderLayout());
            add(p1,BorderLayout.NORTH);
            add(a1,BorderLayout.CENTER);
            add(s1,BorderLayout.EAST);
     
}

    public void actionPerformed(ActionEvent e)
    {
 
    if(e.getSource() == b1)
        {
     
        JFrame f=new JFrame();
                     
            jc.setCurrentDirectory(new File("."));
            jc.showOpenDialog(f);
                     
            File f1=jc.getSelectedFile();
                     
            String nm=f1.getPath();

t1.setText(f1.getName());
             
        }
             
        else if(e.getSource()== b2)
        {
             
        }
     
}

    public static void main(String arg[])
    {
             
    ass7a1 s=new ass7a1("File Descripter");

        s.setSize(400,200);
        s.setVisible(true);
        s.setLocation(400,400);
        s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    }

}

No comments:

Post a Comment