0

In this code i can open text file and display it on windows panel..but when go to save it will save...but saved file cant find in saved directory please help me...am new for java...Thanks in advance

import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Read extends JFrame
{
    private JTextField  filename    = new JTextField(), dir = new JTextField();
    private JButton     open        = new JButton( "Open" ), save = new JButton( "Save" );
    public JPanel       window_panel;                                                       // address_panel,
    public JLabel       address_label;
    public JTextField   address_tf;
    public JEditorPane  window_pane, tree_pane, attr_pane;
    public JScrollPane  window_scroll, tree_scroll, attr_scroll;
    TextArea            t1, t2;
    JPanel              pane;
    public JButton      address_b, browse;
    public JLabel       l, m;
    JFrame              f;

    public Read() throws IOException
    {

        f = new JFrame( "Web browser" );
        f.setSize( 1000, 700 );

        pane = new JPanel();
        pane.setVisible( true );
        pane.setLayout( null );
        f.setContentPane( pane );

        address_label = new JLabel( " Path: ", SwingConstants.CENTER );
        address_label.setBounds( 10, 10, 70, 30 );
        pane.add( address_label );

        address_tf = new JTextField( "", 25 );
        address_tf.setBounds( 80, 10, 250, 30 );
        pane.add( address_tf );

        browse = new JButton( "Browse" );
        browse.setBounds( 340, 10, 140, 30 );
        browse.addActionListener( new Open() );
        pane.add( browse );

        save = new JButton( "Save" );
        save.setBounds( 500, 10, 150, 30 );
        save.addActionListener( new Save() );
        pane.add( save );

        window_pane = new JEditorPane();
        window_pane.setBounds( 10, 50, 970, 600 );
        pane.add( window_pane );

        f.setVisible( true );
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    }

    class Open implements ActionListener
    {
        public void actionPerformed( ActionEvent ae )
        {
            JFileChooser fc = new JFileChooser();

            int result = fc.showOpenDialog( null );
            if ( result == JFileChooser.APPROVE_OPTION )
            {
                File file = fc.getSelectedFile();
                String sname = file.getAbsolutePath();
                address_tf.setText( sname );
                String ext = getFileExtension( sname );
                try
                {

                    window_pane.setPage( file.toURI().toURL() );
                    System.out.println( file.toURI().toURL() );
                }
                catch ( MalformedURLException e )
                {
                    e.printStackTrace();

                }
                catch ( IOException e )
                {
                    e.printStackTrace();

                }
            }
        }
    }

    class Save implements ActionListener
    {
        public void actionPerformed( ActionEvent e )
        {
            JFileChooser sv = new JFileChooser();
            // Demonstrate "Save" dialog:
            int result = sv.showSaveDialog( Read.this );
            if ( result == JFileChooser.APPROVE_OPTION )
            {
                filename.setText( sv.getSelectedFile().getName() );
                dir.setText( sv.getCurrentDirectory().toString() );
            }

        }
    }

    public String getFileExtension( String filename )
    {
        if ( filename == null )
        {
            return null;
        }
        int lastUnixPos = filename.lastIndexOf( '/' );
        int lastWindowsPos = filename.lastIndexOf( '\\' );
        int indexOfLastSeparator = Math.max( lastUnixPos, lastWindowsPos );
        int extensionPos = filename.lastIndexOf( '.' );
        int lastSeparator = indexOfLastSeparator;
        int indexOfExtension = lastSeparator > extensionPos ? -1 : extensionPos;
        int index = indexOfExtension;
        if ( index == -1 )
        {
            return "";
        }
        else
        {
            return filename.substring( index + 1 );
        }
    }

    public static void main( String args[] ) throws IOException
    {

        Read wb = new Read();

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Your code does not contain the code to actually create a file. You should look at [Java Tutorial: Basic I/O](http://docs.oracle.com/javase/tutorial/essential/io/) for examples. – Oleg Estekhin Apr 30 '14 at 07:40
  • The only thing you do in your `Save` `ActionListener` is to set the `filename` and `dir` text fields in your UI, you should make a call to actually write the file as @OlegEstekhin suggested. – Patru Apr 30 '14 at 07:43
  • 1
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Apr 30 '14 at 07:49

2 Answers2

0

In your actionPerformed method, you do not actually save the file. You just store its name and folder to variables.

Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
0

Using JfileChooser you can get the path. And after that you can copy what you want to that location.

here is the sample code

   jFileChooser1.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);

    jFileChooser1.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);

    final int fd = jFileChooser1.showDialog(null, "Select folder to save.");

    if (fd == JFileChooser.APPROVE_OPTION) {

        /*you can write the code in here */

        String setBackUpFolderName = setBackUpFolderName(iivo);
        try {
            saveDataToFileExcel(setBackUpFolderName, iivo);
        } catch (Exception ex) {
            Logger.getLogger(ExcelTest.class.getName()).log(Level.SEVERE, null, ex);
        }

    } else {
        // this.dispose();
    }
User1100
  • 45
  • 4
MadukaJ
  • 722
  • 6
  • 22