hello. i am trying to make a text editor. i can't figure out how to get the file that i select in the JFileChooser to
show in the JTextArea. and I also can't figure out how to get the text from the JTextArea to save into a .txt file.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class mainC implements ActionListener{
public static void main(String[] agrs){
new mainC();
}
final JFileChooser fc = new JFileChooser("c:\\");
mainC(){
JFrame frame=new JFrame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(screenSize.width - 4, screenSize.height - 30);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.validate();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//******set JMenuBar
JMenuBar menuBar=new JMenuBar();
frame.setJMenuBar(menuBar);
//******Add Menus
JMenu file=new JMenu("File");
menuBar.add(file);
//******Add MenuItems
//Open File
JMenuItem open=new JMenuItem("Open File");
open.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
final int openVal = fc.showOpenDialog(fc);
switch(openVal){
case JFileChooser.APPROVE_OPTION:
//Open was clicked
JDialog currentFileF=new JDialog();
currentFileF.setVisible(true);
currentFileF.setSize(800,600);
JTextArea textArea= new JTextArea();
JScrollPane pane = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
currentFileF.add(pane);
case JFileChooser.CANCEL_OPTION:
//Cancel was clicked
case JFileChooser.ERROR_OPTION:
//Error Occurred
}
}});
//Save File
JMenuItem save=new JMenuItem("Save File");
save.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
int saveVal=fc.showSaveDialog(fc);
switch(saveVal){
case JFileChooser.APPROVE_OPTION:
//Open was clicked
case JFileChooser.CANCEL_OPTION:
//Cancel was clicked
case JFileChooser.ERROR_OPTION:
//Error Occurred
}
}});
file.add(open);
file.add(save);
file.addSeparator();
}
public void actionPerformed(ActionEvent arg0) {}
}
thank you!