Solved

Opening a Text Editor JFrame using a button

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
countAbc challenge 9 49
what is the difference between "sudo su" and "su - root" 6 72
Java Timer (static) 9 12
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

706 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

19 Experts available now in Live!

Get 1:1 Help Now