Here my code am trying to open text file in window_pane it will open when i click on save it will ask destination to save file if i save example like Read.txt it will save...when i go and check in that folder Read.txt file won't found...what is the error please help me..???
import javax.swing.*; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; import java.io.*; import java.net.MalformedURLException; import java.awt.*; import java.awt.event.*; 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; //private Go go = new Go(); 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("hi"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } class Save implements ActionListener { public void actionPerformed(ActionEvent e) { JFileChooser c = new JFileChooser(); // Demonstrate "Save" dialog: int rVal = c.showSaveDialog(Read.this); if (rVal == JFileChooser.APPROVE_OPTION) { filename.setText(c.getSelectedFile().getName()); dir.setText(c.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(); } }