how to link Rtext to a java application?

Questions on using RText should go here.

Moderator: robert

how to link Rtext to a java application?

Postby john quilder » Tue Sep 13, 2011 9:13 pm


how do I link the editor rText to a java web application?
can this be done in just a few calls?

What I want is to allow my users to edit some code in inside the browser,
and each time the code is "saved", the text is passed back to my application (as a string, say)
and the editor remains active.

I tried using Rsyntaxtextarea, but the necessary code seems complicated, and
probably it is easier to use the full editor Rtext.
john quilder

Re: how to link Rtext to a java application?

Postby robert » Wed Sep 14, 2011 11:38 am

Hi John,

Unfortunately, RText isn't designed to run as an applet. It assumes it has access to the local file system, and modifying it to not do so would be tough.

Out of curiosity, what is tough about putting plain old RSyntaxTextArea into an applet? You should be able to do something simple to this example. Another simple resource is the RSyntaxTextAreaDemo project in SVN, that can be built to run as both an applet and a desktop app (this is the same applet that is deployed here, although that one is old and should be updated).

Let me know if you need any more help!

User avatar
Posts: 852
Joined: Sat May 10, 2008 5:16 pm

Re: how to link Rtext to a java application?

Postby john quilder » Wed Sep 14, 2011 9:54 pm


My main problem is that I cannot understand how to exchange data between
my Rsyntaxtextarea and my application. Your example does not show methods to
do this, and neither TextEditorDemo does. The behaviour I want is the following:
my user presses a button (say Ctrl-S or F2) in the editor, and this starts
a function in my application, with the text passed as a parameter/argument.
The only relevant method I found is textArea.setText("string") ;
but that's to pass the data in the other direction.

Also, it would be nice if the TextEditorDemo or this example
would allow Regexp search and replacement.

<i>What's so tough about putting plain old RSyntaxTextArea into an applet?</i>
Err, I must admit I do not know Java well at all. In fact I am writing a script in jRuby
(a java implementation of Ruby that can call Java methods), and I am using
Rsyntaxtextarea to allow the user to edit some code with syntax highlightning.

So maybe the answers to my questions are obvious...

Two minor bugs :

1. I need to use a different classpath, e.g.
java -classpath "./rsyntaxtextarea.jar:." TextEditorDemo
~$ java -classpath "./rsyntaxtextarea.jar" TextEditorDemo
Error: Could not find or load main class TextEditorDemo

2. In TextEditorDemo, I need to maka a small change:
< RSyntaxTextArea textArea = new RSyntaxTextArea();
> RSyntaxTextArea textArea = new RSyntaxTextArea(20,20);
otherwise it started the textare with a field 1x1 or something.
john quilder

Re: how to link Rtext to a java application?

Postby robert » Thu Sep 15, 2011 1:35 am

I see. Sorry if I sounded curt in my first reply, I was genuinely trying to get at what the root problem is. The examples do assume a certain level of familiarity with Swing, and the text components in particular.

RSTA inherits from javax.swing.JTextArea, and thus also from javax.swing.text.JTextComponent. The latter is the base class of all Swing text components, and thus has many of the methods for functionality common to all text components - including getting and setting text. Some helpful methods:

  • getText() - Returns all text in the text area
  • getText(int offs, int len) - Returns a piece of text in the text area
  • getSelectedText() - Returns the text selected in the editor, if any, or null if no text is selected

Will your applet contain a menu bar (JMenuBar), or a toolbar or set of buttons, or just an instance of RSyntaxTextArea? It's less intuitive what to do to add a keyboard shortcut to perform an action (grab text, etc.) if you just have an editor, but I'd be happy to guide you if you go that route (it's not hard, just not intuitive if you're a Swing newbie).

As for the first issue you found, it looks to me that on the TextEditorDemo (Example 1), it *does* tell you to add the current directory to the classpath parameter. Are you seeing this example somewhere else, with an incorrect command line?

As for the second one - I meant to fix that, but never did. Examples 5 and 6 actually do use the row/column constructor, as you do, to set the preferred size. I'll update the examples, thanks!
User avatar
Posts: 852
Joined: Sat May 10, 2008 5:16 pm

Re: how to link Rtext to a java application?

Postby john quilder » Thu Sep 15, 2011 5:26 pm

No problem at all. Thanks for suggestion to help!

yes, .getText is what I needed. Now, how do I initiate a call
of .getText in my application at the user's request?
It must be a Listener I suppose...

Regarding JMenu or buttons : I do not think it matters
much for me, though I'd prefer to also have keybinding for basic
actions (so the buttons should look like "Search. Ctrl-S.")
And I would guess I would prefer to have more features rather
than less (e.g. line numbering etc).

Speaking of updating examples: do you mind if I make a suggestion?

I think it might be useful to many people if you add
an RSyntaxTextEditor class, rather than
.demo.RSyntaxTextAreaDemoApp[let]. It seems as a trivial change;
but I think it might be immeduately useful to many people
who just want to plug a syntax-enabled basic editor into their browser apps.
I found quite a number of posts asking for such an editor
when googling for it myself.

As the name suggests, the class would start up an editor;
the editor should have a "Save" button used to pass
the edited text back to the application.

And then you'd have an example like this:


RSyntaxTextEditor textEditor = new RSyntaxTextEditor(40,70);
textEditor.setText("some string");
textEditor.start ;//-or- textEditor.setVisible(true) ;

.... // user clicked "Save" button!
... // print editedText to standard output

... // user clicked "Quit" button!
.... // print 'User killed the editor' to standart output

it *does* tell you to add the current directory to the classpath parameter.

Ops. Oh, my mistake. Sorry.
john quilder

Re: how to link Rtext to a java application?

Postby robert » Sat Sep 17, 2011 7:04 pm

While menu bars look a little funny in applets (to me at least), they're the simplest way to do what you want.

A very thorough but excellent resource is the Swing tutorial on Oracle's site. The section on JMenus/JMenuItems is here. The only thing the JMenu tutorial doesn't really hit on is using Actions in the menu items, but that's covered in its own section.

You can also check out how I implemented a menu in the RSyntaxTextAreaDemo project. The JMenuBar is constructed in the DemoRootPane class. The basic idea is that you create a JMenuItem and give it an implementation of the javax.swing.Action class. An Action embodies both a code snippet to execute when the menu item is clicked, as well as some "metadata" about the code snippet (it's name/description, to be used in components such as the menu item, a keyboard shortcut, etc.).

The only thing RSyntaxTextAreaDemoApp doesn't do is show assigning a shortcut to an action. But again, that's just another property on that Action. An example snippet (untested):

Code: Select all
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

  * An action that prints all text in a text area to the console.
public class TextToConsoleAction extends AbstractAction {

   private RSyntaxTextArea textArea;

   public void TextToConsoleAction(RSyntaxTextArea textArea) {
      this.textArea = textArea;
      putValue(NAME, "Print Text Area Contents to stdout");
      // Grab the modifier key used for menu items on this OS.
      // Uses "Ctrl" on Windows, Unix, etc., and the "control" key on the Mac.
      int ctrl = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
      putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_S, ctrl));

   public void actionPerformed(ActionEvent e) {


Code: Select all
import javax.swing.*;
import org.fife.ui.rsyntaxtextarea.*;

  * The main applet class.
public class MyApplet extends JApplet {

   // ... other stuff ...

   public JMenuBar createMenuBar() {

      JMenuBar menuBar = new JMenuBar();

      JMenu fileMenu = new JMenu("File");
      // Assumes the RSTA instance has already been created.
      menuBar.add(new JMenuItem(new TextToConsoleAction(textArea)));

      // Add other menus and menu items

      return menuBar;



For every JMenuItem in your JMenuBar with a shortcut assigned to it (via Action.ACCELERATOR_KEY), whenever the user enters that keyboard shortcut, that menu item (and thus the action associated with it) will get invoked, no matter what component in the applet has focus.

Good luck!
User avatar
Posts: 852
Joined: Sat May 10, 2008 5:16 pm

Return to Help

Who is online

Users browsing this forum: No registered users and 1 guest