Results 1 to 3 of 3
  1. #1
    Saran185 is offline Member
    Join Date
    Jan 2011
    Posts
    36
    Rep Power
    0

    Unhappy file opening in textarea

    hi...

    I have created two text fields in the jframe.using file chooser i have to select a text file from the system and disply the contents on textarea 1. i have written the code for it. but not getting the output which i require. the file chooser is not working.
    pls help me..
    i dont get any errors when i compile it.
    Java Code:
    
    
    package vspmt1;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import javax.swing.Action;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    import javax.swing.event.AncestorListener;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.text.DefaultEditorKit;
    import javax.swing.*;
    import javax.swing.JScrollPane;
    import javax.swing.BoundedRangeModel;
    public class Main implements MouseListener {
    
    JFrame frm;
    JTextArea textArea1;
    JTextArea textArea2;
    JScrollPane sp;
    JScrollPane sp2;
    Action selectWord;
    private FileFilter fJavaFilter;
    private File fFile;
    String strLine;
        public static void main(String[] args)
        {
        Main highlight = new Main();
        }
    
        public Main()
        {    
    
           frm = new JFrame();
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
            JMenuBar menubar = new JMenuBar();
    
        JMenu m = new JMenu("File");
        m.add(new JSeparator());
        JMenu editmenu = new JMenu("Play");
        editmenu.add(new JSeparator());
        JMenuItem fileItem1 = new JMenuItem("New");
        JMenuItem fileItem2 = new JMenuItem("Open");
        JMenuItem fileItem3 = new JMenuItem("Close");
        fileItem3.add(new JSeparator());
        JMenuItem fileItem4 = new JMenuItem("Save");
       // m.add(fileItem1);
       m.add(fileItem2);
        //m.add(fileItem3);
        //m.add(fileItem4);
        menubar.add(m);
        menubar.add(editmenu);
    
    textArea1 = new JTextArea(20,80);
    		 textArea2 = new JTextArea(10,80);
    
             sp = new JScrollPane(textArea1);
     sp2 = new JScrollPane(textArea2);
    
    frm.setLayout(new FlowLayout());
    
        frm.setJMenuBar(menubar);
    	frm.getContentPane().add(sp);
    frm.getContentPane().add(sp2);
        frm.setSize(1000,700);
        frm.setVisible(true);
    selectWord =getAction(DefaultEditorKit.selectWordAction);
    textArea1.addMouseListener(this);
    
    
    
        }
    
    	private Action getAction(String name)
    {
    Action action = null;
    Action[] actions = textArea1.getActions();
    for (int i = 0; i < actions.length; i++)
    {
    if (name.equals( actions[i].getValue(Action.NAME).toString() ) )
    {
    action = actions[i];
    break;
    }
    }
    return action;
    }
    
    public void mouseClicked(MouseEvent e)
    {
    if ( SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1)
    {
    selectWord.actionPerformed( null );
    }
    }
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    private JMenuItem makeMenuItem(String name) {
    JMenuItem m = new JMenuItem(name);
    m.addActionListener(new filemenu());
    return m;}
    class filemenu implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    boolean status = false;
    String command = e.getActionCommand();
    if (command.equals("Open")) {
    status = openFile();
    if (!status)
    JOptionPane.showMessageDialog(null, "Error opening file!",
    "File Open Error", JOptionPane.ERROR_MESSAGE);
    }
    }// actionPerformed end
    }// class end filemenu
    boolean openFile() {
    JFileChooser fc = new JFileChooser();
    fc.setDialogTitle("Open File");
    // Choose only files, not directories
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    // Start in current directory
    fc.setCurrentDirectory(new File("."));
    // Set filter for Java source files.
    fc.setFileFilter(fJavaFilter);
    // Now open chooser
    int result = fc.showOpenDialog(frm);
    if (result == JFileChooser.CANCEL_OPTION) {
    return true;
    }
    else if (result == JFileChooser.APPROVE_OPTION) {
    fFile = fc.getSelectedFile();
    try {
    FileInputStream fstream = new FileInputStream(fFile);
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(
    new InputStreamReader(in));
    
    // Read File by char
    char[] buffer = new char[4096];
    int len=0;
                     while((len = br.read(buffer)) != -1) { // Read a batch of chars
                    String s = new String(buffer, 0, len); // Convert to a string
                    textArea1.setText(s);
    				sp = new JScrollPane(textArea1);
    				frm.add(sp);
    
                }
    
    
    
    
    
    
    
    
        } // try end
    catch (Exception e) {// Catch exception if any
    System.err.println("Error: xyz" + e.getMessage());
    }
    System.out.println("end of open file");
    }// elseif approve option
    else {
    return false;
    }
    return true;
    }
    
    
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Suggest you boil the code down to an SSCCE
    the file chooser is not working.
    What about it doesn't work? Does it show? Can you select files? Can you retrieve files?

  3. #3
    Saran185 is offline Member
    Join Date
    Jan 2011
    Posts
    36
    Rep Power
    0

    Default filechooser is not opening

    the filechooser is not opening when i click "open" from file menu.

Similar Threads

  1. Opening a file
    By rdjava in forum Advanced Java
    Replies: 0
    Last Post: 01-30-2011, 08:34 PM
  2. Help opening and reading a file
    By viperlasson in forum New To Java
    Replies: 0
    Last Post: 09-21-2010, 07:32 AM
  3. Opening mp3 file ( not reading )
    By Char in forum New To Java
    Replies: 15
    Last Post: 08-29-2010, 07:09 AM
  4. Opening .html file
    By Prajin in forum New To Java
    Replies: 7
    Last Post: 08-06-2010, 07:20 AM
  5. Replies: 1
    Last Post: 11-13-2008, 07:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •