LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-26-2004, 12:31 AM   #1
decadent
LQ Newbie
 
Registered: Mar 2004
Posts: 5

Rep: Reputation: 0
Java: How to load text file into the JEditorPane?


Code:
//How to load text file into the JEditorPane?
//Could you write me a code please?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;

public class qwerty{
	public static void main(String[] args){
		JFrame mainFrame = new MyWindow();
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.show();
	}
}
class MyWindow extends JFrame{
	private JFileChooser chooser;
	public MyWindow(){
		setSize(400, 300);
		final JEditorPane editorPane = new JEditorPane();
		chooser = new JFileChooser();
		Container contentPane = getContentPane();
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu fileMenu = new JMenu("file");
		menuBar.add(fileMenu);

		JMenuItem openItem = new JMenuItem("open");
		fileMenu.add(openItem);
		openItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				int r=chooser.showOpenDialog(null);
				if(r==JFileChooser.APPROVE_OPTION){
					String name = chooser.getSelectedFile().getPath();
					System.out.println("Question: how to load selected file\n" + name + "\ninto the editorPane?");                
					/* dont work
					URL h = qwerty.class.getResource(name);
					try {
						editorPane.setPage(h);
					} catch (IOException e) {
						e.printStackTrace();
					}*/
                		}
			}
		});
	contentPane.add(editorPane);
	}
}
 
Old 03-26-2004, 01:29 AM   #2
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
I didn't test this code (can't do that right now), but I think this should work:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;

public class qwerty{
	public static void main(String[] args){
		JFrame mainFrame = new MyWindow();
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.show();
	}
}
class MyWindow extends JFrame{
	private JFileChooser chooser;
	public MyWindow(){
		setSize(400, 300);
		final JEditorPane editorPane = new JEditorPane();
		chooser = new JFileChooser();
		Container contentPane = getContentPane();
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu fileMenu = new JMenu("file");
		menuBar.add(fileMenu);

		JMenuItem openItem = new JMenuItem("open");
		fileMenu.add(openItem);
		openItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				int r=chooser.showOpenDialog(null);
				if(r==JFileChooser.APPROVE_OPTION){
					String filePath = chooser.getSelectedFile().getPath();
					try {
						FileInputStream fr = new FileInputStream(filePath);
						InputStreamReader isr = new InputStreamReader(fr, "UTF-8");
						BufferedReader reader = new BufferedReader(isr);
						StringBuffer buffer = new StringBuffer();

						String line = null;
						while ((line = reader.readLine()) != null) {
							buffer.append(line);
						}

						reader.close();

						editorPane.setText(buffer.toString());
					} catch (IOException e) {
						e.printStackTrace();
					}
                		}
			}
		});
	contentPane.add(editorPane);
	}
}
 
Old 03-26-2004, 01:44 AM   #3
decadent
LQ Newbie
 
Registered: Mar 2004
Posts: 5

Original Poster
Rep: Reputation: 0
thank you
 
Old 03-26-2004, 02:27 AM   #4
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
or you could go like this

try{

editorPane.setContentType("text/html"); //assuming it's an html document

Reader fileRead=new FileReader(name); // name is the file you want to read

editorPane.read(fileRead,null);

} catch(Exception e) { }
 
Old 03-26-2004, 03:15 AM   #5
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
And looking at it a wee bit further this should do it.

try{

URL h=new URL("file://"+name);
editorPane.setPage(h);

}catch(Exception e) { }
 
Old 04-01-2004, 01:36 AM   #6
decadent
LQ Newbie
 
Registered: Mar 2004
Posts: 5

Original Poster
Rep: Reputation: 0
thank you very much
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving a text file as a variable and reading it every second in java script mrobertson Programming 4 03-26-2007 08:25 PM
Cannot load .so file from java library path whalesurf Linux - Software 1 07-24-2006 11:43 AM
Which light text editor can copy text from file and paste in browser? davidas Linux - Software 9 03-06-2006 11:28 AM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Can't enter text in certain Java text fields TheBelush Linux - Software 4 04-27-2005 05:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration