954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how do i get show a text file in a JTextArea?

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!

2concussions
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

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!

2concussions
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: