In my program when i close the save file chooser it opens another one ??? heres the code
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class JB extends JPanel implements ActionListener{
protected JTextArea textArea;
public JB() {
setLayout(new BorderLayout());
JTextArea textArea = new JTextArea();
textArea.setFont(new Font("Serif", Font.PLAIN, 14));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane areaScrollPane = new JScrollPane(textArea);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(1000, 500));
JTextPane textPane = createTextPane();
JScrollPane paneScrollPane = new JScrollPane(textPane);
paneScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
paneScrollPane.setPreferredSize(new Dimension(1000, 400));
paneScrollPane.setMinimumSize(new Dimension(10, 10));
JPanel leftPane = new JPanel(new BorderLayout());
JPanel buttons = new JPanel(new BorderLayout());
JButton save = new JButton("Save");
save.setToolTipText("Click to Save");
save.addActionListener(this);
JButton open = new JButton("Open");
open.setToolTipText("Click to Open");
save.setActionCommand("save");
open.setActionCommand("open");
save.addActionListener(this);
open.addActionListener(this);
String s = textPane.getText();
leftPane.add(areaScrollPane,
BorderLayout.CENTER);
buttons.add(save, BorderLayout.AFTER_LAST_LINE);
buttons.add(open, BorderLayout.BEFORE_FIRST_LINE);
add(leftPane, BorderLayout.LINE_START);
add(buttons, BorderLayout.AFTER_LAST_LINE);
}
public void actionPerformed(ActionEvent e) {
if("open".equals(e.getActionCommand())){
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(JB.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File ofile = fc.getSelectedFile();
try{
textArea.setText(convert(open(ofile.getAbsolutePath())));
} catch(Exception ex){System.out.println(ex);}
}
}
else if("save".equals(e.getActionCommand())){
final JFileChooser fy = new JFileChooser();
fy.setApproveButtonText("Save");
fy.setFileFilter(new FileNameExtensionFilter("dtf", "txt", "fvc"));
int returnVal = fy.showOpenDialog(JB.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File sfile = fy.getSelectedFile();
try {
Thread.sleep(100);
save(textArea.getText(), sfile.getAbsolutePath());
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
public String convert(int i){
return "" + i;
}
private JTextPane createTextPane() {
JTextPane textPane = new JTextPane();
return textPane;
}
public void save(String s, String l) throws IOException{
File f = new File(l);
FileWriter lol = new FileWriter(f);
lol.write(s);
}
public int open(String l) throws IOException{
Reader reader = new FileReader(l);
int data = reader.read();
while(data != -1) {
data = reader.read();
return data;
}
reader.close();
return 0;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("D-Text Edit");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JB());
frame.setResizable(false);
frame.pack();
frame.setLocation(500, 100);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}