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!

Recommended Answers

All 2 Replies

you need to set the value of the text of the textarea, and you need to add the textarea to the panel.

I also suggest you to declare your textarea on class scope, that way you will be able to (re)set the value of the text in the textarea.

you need to set the value of the text of the textarea, and you need to add the textarea to the panel.

I also suggest you to declare your textarea on class scope, that way you will be able to (re)set the value of the text in the textarea.

thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.