File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Editor pane problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Editor pane problem" Watch "Editor pane problem" New topic
Author

Editor pane problem

Rupali Desai
Ranch Hand

Joined: May 29, 2001
Posts: 42
Could someone please tell me what is wrong with the following code.Editor pane never updates with the contents of the text file chosen.


Thanks

Rupali

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

class editor extends JFrame implements ActionListener
{

String OpenFile = null;
URL filePath = null;
JEditorPane cEditor = null;
public boolean debug = true;
Container contentPane = null;
JMenuItem Load = null;

public editor()
{

contentPane= getContentPane();
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu File = new JMenu("File");
menuBar.add(File);

Load = new JMenuItem("Load");
File.add(Load);
Load.addActionListener(this);
cEditor = new JEditorPane();
contentPane.add(new JScrollPane(cEditor),BorderLayout.CENTER);
}

public String showFileDialog()
{

JFileChooser fileDialog = new JFileChooser();
int returnval = fileDialog.showOpenDialog(contentPane);
File f = fileDialog.getCurrentDirectory();
System.out.println(f.toString());
File fileName = fileDialog.getSelectedFile();
System.out.println(fileName.toString());
String f2 = ConverToFileURI(fileName.toString());//+f.toString();
System.out.println(f2);

if((OpenFile.indexOf(".java")) != -1)
{
int index = OpenFile.indexOf(".java");
String filename = OpenFile.substring(0,index);
System.out.println(filename);

}

try
{
filePath = new URL(f2);
cEditor = new JEditorPane(filePath);
cEditor.setText(" jj");//age(filePath);

}
catch(Exception e)
{
System.out.println("error"+e);
}


return f2;

}

public String ConverToFileURI(String fileName)
{
StringBuffer temp = new StringBuffer("file:");
int index = fileName.indexOf('\\',0);
int startIndex = 0;

while(index != -1)
{
temp.append(fileName.substring(startIndex,index));
temp.append("\\\\");
startIndex = index+1;
index = fileName.indexOf('\\',startIndex);
}
temp.append(fileName.substring(startIndex,fileName.length()));
OpenFile = fileName.substring(startIndex,fileName.length());
System.out.println(temp.toString());
return temp.toString();
}

public void actionPerformed(java.awt.event.ActionEvent actionEvent)
{
String event = actionEvent.getActionCommand();
if(debug)
System.out.println("Event is: " +event);
if(event.equals("Load"))
{
showFileDialog();

}
}

public static void main(String args[])
{
editor cb = new editor();
cb.setSize(600,600);
cb.setVisible(true);
}

}
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
Hi,
the problem you can find in your method showFileDialog. There is a lot of code, which can be easier. For example, you create your own URL, but the File has already a method, which does this for you. And if you want to fill your JEditorPane with the selected file, you don't need to create a new instance.
Rupali Desai
Ranch Hand

Joined: May 29, 2001
Posts: 42
Thanks a lot that solved my problem
Rupali desai
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Editor pane problem
 
Similar Threads
Changing File Name Not Working....
Urgent Help
help me I can't work the tree well
Exception while uploading a zip file on Linux OS
ClassNotFoundException error