JEditor editor = new JEditor();By default the JEditor component will use a default text syntax, but you can use another one by calling JEditor.setTokenMarker(TokenMarker). TokenMarkers can be created directly or using the SyntaxMapper class[1] .
JEditor editor = new JEditor(); SyntaxMapper mapper = SyntaxMapper.getInstance(); TokenMarker tk = mapper.createTokenMarker("java"); editor.setTokenMarker(tk);
JEditor editor = new JEditor(); editor.setSyntax("xml");The editor manage its own scrolling, so you don't need to add the editor in a
JScrollPane
. However it is possible to add the editor in a JScrollPane in a Swing hierarchy, but it will not work correctly in all cases.
private void openFile(File file) { JEditor editor = new JEditor(); // get the appropriate syntax SyntaxMapper mapper = SyntaxMapper.getInstance(); Syntax syntax = mapper.getSyntaxForExtension(file); TokenMarker tk = syntax.createTokenMarker(); editor.setTokenMarker(tk); // set the editor text content editor.setText(file); }You can also use the JEditor.setSyntax(String) method. For example:
private void openFile(File file) { JEditor editor = new JEditor(); // get the appropriate syntax editor.setSyntax("xml"); // set the editor text content editor.setText(file); }
JEditor
to listen to the modifications of the underlying document by JEditor.addEditorStateListener(EditorStateListener):JEditor
is first setCodeEditorDefaults JEditor(defaults) = new CodeEditorDefaults(); defaults.setPopup(new DefaultEditorPopup()); JEditor ta = new JEditor(defaults);will show:
CodeEditorDefaults defaults = new CodeEditorDefaults(); defaults.setStyle(Token.KEYWORD1, Color.BLUE, false, true); defaults.setStyle(Token.KEYWORD2, Color.BLUE, false, true); defaults.setStyle(Token.KEYWORD3, Color.BLUE, false, true); defaults.setStyle(Token.COMMENT1, Color.DARK_GRAY, false, false); defaults.setStyle(Token.COMMENT2, Color.DARK_GRAY, false, false); defaults.eolMarkers = false; defaults.paintInvalid = false; // create CodeEditor JEditor ta = new JEditor(defaults);
CodeEditorHighlighter highlighter = new CodeEditorHighlighter(); highlighter.append(1, Color.GREEN); // 1st line in Green highlighter.append(3, Color.RED); // 3rd line in Red // create CodeEditor JEditor ta = new JEditor(defaults); // sets the Highlighter ta.setHighlighter(highlighter);We will have the following result:
Copyright 2016-2019 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence