Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Editor pane problem

 
Rupali Desai
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot that solved my problem
Rupali desai
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic