Solved

Opening a Text Editor JFrame using a button

Posted on 2009-05-10
4
590 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 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

932 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now