Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Opening a Text Editor JFrame using a button

Posted on 2009-05-10
4
Medium Priority
?
600 Views
Last Modified: 2013-11-23
I am trying to create a simple text editor and so on the first frame i wanted it to show nothing and then when the user clicks open i wanted a JFileChooser to come up and open up a new JFrame with the text file loaded up in the JTextArea, i was going to do a dispose() on the intial JFrame, so that the program looks "clean" when its being run by the user. Anyways, currently when i try and compile the code below it throws a following error.....

fileio.java:48: unreported exception java.io.IOException; must be caught or declared to be thrown
textEditorEditingScreen();
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.*;
 
 
 
 
 
public class fileio
{
	String openedDoc;
	public static void main (String[] args)throws Exception
	{
	fileio docEdit = new fileio();
	docEdit.textEditorInitialScreen();
//	docEdit.docEditor();
 
	}
 
public void textEditorInitialScreen() throws Exception
{
	
  	  JFrame jF1 = new JFrame("Text Editor"); // Set the window title.
 
      // Set the size of the JFrame to 1150 x 629 pixels. 
      jF1.setSize(1150, 629);
		
		
		JLayeredPane jP1 = new JLayeredPane();
		jP1.setLayout(null);
		
			
		JTextArea wordDocNew = new JTextArea(5, 10);
		wordDocNew.setLineWrap(true);
		
		
				JButton startOpen = new JButton("OPEN");
		           startOpen.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e)
						{
						textEditorEditingScreen();
						}
            });
		
		JButton startSave = new JButton("SAVE");
  			         startSave.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) 
						{
						 System.exit(0);
						}
            });
 
		JButton startClose = new JButton("QUIT");
		           startClose.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) 
						{
						 System.exit(0);
						}
            });
				
	
		
      // Post the JLabels on the JPanel
	   // jPanel1.add(pictureJLabel); // Post the BSG start screen. 
		jP1.add(wordDocNew, JLayeredPane.PALETTE_LAYER);
		jP1.add(startClose, JLayeredPane.PALETTE_LAYER); 
		jP1.add(startOpen, JLayeredPane.PALETTE_LAYER);
		jP1.add(startSave, JLayeredPane.PALETTE_LAYER);
		
		wordDocNew.setBounds(10,20,900,500);
		startOpen.setBounds(970,50,100,20);
		startSave.setBounds(970,100,100,20);
		startClose.setBounds(970,200,100,20);
 
 
 
      // Post the JPanel into the JFrame
      jF1.getContentPane().add(jP1);
		
      // Displays the JFrame
      jF1.setVisible(true);
		
 
}
 
public void textEditorEditingScreen() throws IOException
{
 
	 //FileReader dataIn = new FileReader("text.txt");
	 String filename = File.separator + "tmp";
    JFileChooser fc = new JFileChooser(new File(filename));
    // Show open dialog
    fc.showOpenDialog(null);
    File f = fc.getSelectedFile();
 
    // Show save dialog
 //   fc.showSaveDialog(null);
 //   selFile = fc.getSelectedFile();
	 //   File f = new File("first.txt");
    FileReader fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);
 
    StringBuffer sb = new StringBuffer();
    String eachLine = br.readLine();
    while (eachLine != null) {
      sb.append(eachLine);
      sb.append("\n");
      eachLine = br.readLine();
    }
 
	 
	
  	  JFrame jF2 = new JFrame("Text Editor"); // Set the window title.
 
      // Set the size of the JFrame to 1150 x 629 pixels. 
      jF2.setSize(1150, 629);
		
		
		JLayeredPane jP2 = new JLayeredPane();
		jP2.setLayout(null);
		
		
		JTextArea wordDocModify = new JTextArea(5, 10);
		wordDocModify.setLineWrap(true);
		
		
				JButton modifyOpen = new JButton("OPEN");
		           modifyOpen.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) 
						{
						
 
						}
            });
		
		JButton modifySave = new JButton("SAVE");
  			         modifySave.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) 
						{
						 System.exit(0);
						}
            });
 
		JButton modifyClose = new JButton("QUIT");
		           modifyClose.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) 
						{
						 System.exit(0);
						}
            });
				
	
		
      // Post the JLabels on the JPanel
	   // jPanel1.add(pictureJLabel); // Post the BSG start screen. 
		jP2.add(wordDocModify, JLayeredPane.PALETTE_LAYER);
		jP2.add(modifyClose, JLayeredPane.PALETTE_LAYER); 
		jP2.add(modifyOpen, JLayeredPane.PALETTE_LAYER);
		jP2.add(modifySave, JLayeredPane.PALETTE_LAYER);
		
		wordDocModify.setBounds(10,20,900,500);
	//	modifyOpen.setBounds(970,50,100,20);
		modifySave.setBounds(970,100,100,20);
		modifyClose.setBounds(970,200,100,20);
 
 
 
      // Post the JPanel into the JFrame
      jF2.getContentPane().add(jP2);
		
      // Displays the JFrame
      jF2.setVisible(true);
		
 
}
}

Open in new window

0
Comment
Question by:vnk89
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:zwei
ID: 24350578
Look at line 47 in your code. You are using textEditorEditingScreen(); without throwing or catching the exception there. To solve, replace it with:
try {
													textEditorEditingScreen();
												} catch (IOException e1) {
													e1.printStackTrace();
												}

Open in new window

0
 
LVL 7

Accepted Solution

by:
zwei earned 500 total points
ID: 24350584
Sorry for the weird indent...
try {
    textEditorEditingScreen();
} catch (IOException e1)
    e1.printStackTrace();
}

Open in new window

0
 

Author Comment

by:vnk89
ID: 24350604
You forgot a Bracket but it works!
try 
{
    textEditorEditingScreen();
} 
catch (IOException e1)
{
    e1.printStackTrace();
}

Open in new window

0
 

Author Closing Comment

by:vnk89
ID: 31579989
You ROCK!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question