0

I have an SWT composite within which I am trying to embed an Swing JEditorPane which should have Python support in it. Im using jSyntaxPane for this. I am able to read the complete .py file and display it in the editor. However the syntax/colour highlighting is not being shown in the editor.

Below is the code:

public static void main(String args[]) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Editor With Python Supprot");
    shell.setLayout(new FillLayout());

    Composite rtfComposite = new Composite(shell, SWT.EMBEDDED);

    java.awt.Frame frame = SWT_AWT.new_Frame(rtfComposite);
    java.awt.Panel panel = new java.awt.Panel(new java.awt.FlowLayout());
    frame.add(panel);

    //DefaultSyntaxKit.initKit();
    //PythonSyntaxKit.initKit();

    StyledEditorKit sek = new StyledEditorKit();
    //PythonSyntaxKit psk = new PythonSyntaxKit();
    //RTFEditorKit rtf = new RTFEditorKit();        

    JEditorPane editor = new JEditorPane();
    JScrollPane scrPane = new JScrollPane(editor);
    editor.setEditorKit(sek);
    //editor.setContentType("text/python");     
    editor.setBackground(Color.white);

    JScrollPane scroller = new JScrollPane();
    scroller.getViewport().add(editor);
    panel.add(scroller);

    try {

        FileInputStream fi = new FileInputStream("C:\\STEP_SAMPLE.py");
        sek.read(fi, editor.getDocument(), 0);

    } catch (FileNotFoundException e) {
        System.out.println("File not found");
    } catch (IOException e) {
        System.out.println("I/O error");
    } catch (BadLocationException e) {
    }

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

When I try to initialise the DefaultSyntaxKit or the PythonSyntaxKit I don't get anything in the UI. I also tried to do "editor.setContentType("text/python")" but it didnt work.

Kindly help. Thanks.

Lishus
  • 15
  • 7

1 Answers1

0

Found the reason from another link from StackOverflow. JEditorPane syntax highlighting using jsyntaxpane

Code changes:

public static void main(String args[]) throws IOException 
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Editor With Python Supprot");
    shell.setLayout(new FillLayout());

    Composite rtfComposite = new Composite(shell, SWT.EMBEDDED);

    java.awt.Frame frame = SWT_AWT.new_Frame(rtfComposite);
    java.awt.Panel panel = new java.awt.Panel(new java.awt.FlowLayout());
    frame.add(panel);

    jsyntaxpane.DefaultSyntaxKit.initKit();
    StyledEditorKit sek = new StyledEditorKit();


    JEditorPane editor = new JEditorPane();
    JScrollPane scrPane = new JScrollPane(editor);
    editor.setEditorKit(sek);
    editor.setContentType("text/python");   
    editor.setBackground(Color.white);

    JScrollPane scroller = new JScrollPane();
    scroller.getViewport().add(editor);
    panel.add(scroller);

    FileReader reader;
    BufferedReader br = null;
    try 
    {
        reader = new FileReader("C:\\STEP_SAMPLE_Latest.py");
        br = new BufferedReader(reader);
        editor.read(br, 0);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    br.close();
    editor.requestFocus();

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Lishus
  • 15
  • 7