import java.awt.EventQueue;
public class FileChooser extends JFrame
{
private JPanel contentPane;
String filename;
// main
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
FileChooser frame = new FileChooser();
frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public FileChooser()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// button to selct file
JButton btnNewButton = new JButton("Select file");
btnNewButton.setBounds(10, 2, 89, 23);
contentPane.add(btnNewButton);
// area to display file content
final JTextArea textArea = new JTextArea();
textArea.setBounds(10, 36, 414, 215);
contentPane.add(textArea);
// save button
final JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
try
{
FileWriter writer = new FileWriter(filename.replace(".",
"_out."));
BufferedWriter bwr = new BufferedWriter(writer);
bwr.write(textArea.getText());
bwr.close();
writer.close();
System.out.println(textArea.getText());
} catch (Exception e)
{
System.out.println("Error");
}
}
});
btnSave.setBounds(283, 2, 89, 23);
contentPane.add(btnSave);
// listen to button clicks
btnNewButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION)
{
File selectedFile = fileChooser.getSelectedFile();
filename = selectedFile.getAbsolutePath();
try
{
FileReader reader = new FileReader(filename);
BufferedReader br = new BufferedReader(reader);
textArea.read(br, null);
br.close();
System.out.println(textArea.getText());
} catch (Exception e)
{
System.out.println("Error");
}enter code here
}
}
enter code here
});
}
}
choice.getSelectedFile();
– Unfruitful