1

I have run the following code in this page RsyntaxTextArea using Java and i run the program exactly the way that is been mentioned in this site.And i'm getting the output as intended. But i have tried to modify this java code to Groovy code, something like:

import groovy.swing.SwingBuilder
import javax.swing.*
import java.awt.*
swing =  new SwingBuilder()
frame = swing.frame(title : "test", defaultCloseOperation:JFrame.EXIT_ON_CLOSE, pack:true, show : true, size :[100,100])
{
        panel
        {
             RSyntaxTextArea textArea = new RSyntaxTextArea();
             textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
        }
}

And when i try to run this script as follows:

groovyc -classpath rsyntaxtextarea.jar TextEditorDemo.groovy 

I get the errors stating that:

groovy: 9: unable to resolve class RSyntaxTextArea 
 @ line 9, column 19.
        RSyntaxTextArea textArea = new RSyntaxTextArea();
                     ^

/home/anto/Groovy/Rsyntax/ST.groovy: 9: unable to resolve class RSyntaxTextArea 
 @ line 9, column 30.
        RSyntaxTextArea textArea = new RSyntaxTextArea();
                                ^

/home/anto/Groovy/Rsyntax/ST.groovy: 10: unable to resolve class RSyntaxTextArea 
 @ line 10, column 7.
         textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);

I guess i have made wrong in running the program. How i do i run the program in this case by defining the classpath too.

demongolem
  • 9,474
  • 36
  • 90
  • 105
Ant's
  • 13,545
  • 27
  • 98
  • 148

2 Answers2

3

It doesn't look like you're importing the package for RSyntaxTextArea. Have you tried adding the following imports to your program?

import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;
ataylor
  • 64,891
  • 24
  • 161
  • 189
  • it works but, i couldn't get the `RsyntaxTextArea` in my Swing. I'm getting only the frame and its title! – Ant's Jun 14 '11 at 04:55
1

This code should do what you want... You needed to add the RSyntaxTextArea into the view (using the widget method)

You also needed to add it into a JScrollPane, so that it scrolls nicely when full.

import groovy.swing.SwingBuilder
import java.awt.BorderLayout as BL
import static javax.swing.JFrame.EXIT_ON_CLOSE
import org.fife.ui.rsyntaxtextarea.*

RSyntaxTextArea textArea = new RSyntaxTextArea()
textArea.syntaxEditingStyle = SyntaxConstants.SYNTAX_STYLE_JAVA

swing =  new SwingBuilder()
frame = swing.frame(title:"test", defaultCloseOperation:EXIT_ON_CLOSE, size:[600,400], show:true ) {
  borderLayout()
  panel( constraints:BL.CENTER ) {
    borderLayout()
    scrollPane( constraints:BL.CENTER ) {
      widget textArea
    }
  }
}

edit

Without using widget, your code would need to look something like this:

import groovy.swing.SwingBuilder
import java.awt.BorderLayout as BL
import static javax.swing.JFrame.EXIT_ON_CLOSE
import org.fife.ui.rsyntaxtextarea.*

RSyntaxTextArea textArea = new RSyntaxTextArea()
textArea.syntaxEditingStyle = SyntaxConstants.SYNTAX_STYLE_JAVA

swing =  new SwingBuilder()
frame = swing.frame(title:"test", defaultCloseOperation:EXIT_ON_CLOSE, size:[600,400], show:true ) {
  borderLayout()
  panel( constraints:BL.CENTER ) {
    borderLayout()
    sp = scrollPane( constraints:BL.CENTER )
    sp.viewport.add textArea
  }
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • what is a widget? Could you explain me this? – Ant's Jun 14 '11 at 12:30
  • Why should i use widget rather than including those statements in `panel` closure? Reason's.... – Ant's Jun 14 '11 at 12:31
  • @ant Because if you don't use `widget`, it won't work. The `widget` call basically says "Add a component which isn't defined in the SwingBuilder" – tim_yates Jun 14 '11 at 12:42
  • Actually, the above comment is misleading as I believe you could store your `scrollPane` into a variable (lets say `sp`), and then later call `sp.viewport.add( textArea )` to add your textArea into it, but if you use widget, your code can be somewhat cleaner – tim_yates Jun 14 '11 at 12:50
  • @tim : So , since `RsyntaxTextArea` is not defined in `swingBuilder` i can't use it directly correct? – Ant's Jun 14 '11 at 12:58
  • @ant Not sure what you mean... You need to create a `RSyntaxTextArea` instance and then add this to the SwingBuilder. This is either done via the `widget` method, or the Swing `parent.add` method directly. – tim_yates Jun 14 '11 at 12:59
  • @tim : Can you specify some tutorial links for `SwingBuilder`, because i need to know more about it, since i'm working with `RsyntaxTextArea` to create a simple programming editor... – Ant's Jun 14 '11 at 13:08