简体   繁体   中英

Syntax highlighting using RSyntaxTextArea library in java netbeans swing

Text is highlighted for java syntax when i am doing it in a seperate class ie TextEditorDemo , but not getting the same result when i am executing the code in action listener of a JCombo Box in swing and initialising the JtextArea msg1 (here) with the rsyntaxtextarea object -> syntax is not being highlighted.

TextEditorDemo.java

import javax.swing.*;
import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;

/**
 * A simple example showing how to use RSyntaxTextArea to add Java syntax
 * highlighting to a Swing application.<p>
 */
public class TextEditorDemo extends JFrame {

   public TextEditorDemo() {

      JPanel cp = new JPanel();

      RSyntaxTextArea syntaxTextArea = new RSyntaxTextArea(20, 60);
      syntaxTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
      // No other property of RSyntaxTextArea is allowed to use

      // You can now modify textArea object similar to any other JTextArea object to add other functionality
      JTextArea textArea = syntaxTextArea;
      JScrollPane sp = new JScrollPane(textArea);
      cp.add(sp);

      setContentPane(cp);
      setTitle("Text Editor Demo");
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pack();
      setLocationRelativeTo(null);
   }

   public static void main(String[] args) {
      // Start all Swing applications on the EDT.
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new TextEditorDemo().setVisible(true);
         }
      });
   }

}

action listener for JComboBox -

private void language1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:


        RSyntaxTextArea syntaxTextArea = new RSyntaxTextArea(6, 20);

        String lang = (String) language1.getSelectedItem();
        syntaxTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
        msg1 = syntaxTextArea;
        msg1.setVisible(true);
        System.out.println("language "+lang);
        if (lang.equals("JAVA")) {
            syntaxTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
        }
        else if(lang.equals("C")){
            syntaxTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_C);
        }
        else if(lang.equals("CSHARP")){
            syntaxTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_CSHARP);
        }
        else if(lang.equals("CSS")){
            syntaxTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_CSS);
        }

        //syntaxTextArea.setCodeFoldingEnabled(true);*/

    }
private void language1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        RSyntaxTextArea syntaxTextArea = new RSyntaxTextArea(6, 20);

        String lang = (String) language1.getSelectedItem();
        syntaxTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
        msg1 = syntaxTextArea;
        msg1.setVisible(true);

The above code does nothing. You haven't actually added the syntax area component to the frame.

Don't create a new RSyntaxArea object!

Instead you need to make the syntaxTextArea an instance variable in your class then you can reference the variable from the listener and just change the editing style property.

Or if for some reason the class doesn't allow you to dynamically change the property, then you will need to use:

sp.setViewportView(syntaxTextArea);

in you listener code. In which case the scrollpane variable will now need to be an instance variable in your class.

Either way you will need to create an instance variable that can be referenced from your ActionListener so you will need to restructure your code. Read the Swing tutorial on How to Use Text Areas . The demo code there will show you how to better structure your classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM