• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Editor pane problem

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}

}
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot that solved my problem
Rupali desai
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic