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();
}
}