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);
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); }
JEditor
to listen to the modifications of the underlying document by JEditor.addEditorStateListener(EditorStateListener): JEditor
is first setdefaults.setPopup(new DefaultEditorPopup()); // create CodeEditor JEditor ta = new JEditor(defaults);will show:
true
for the CodeEditorDefaults.hasPopup value will also show the DefaultEditorPopup
. For example, the following code will also allow to show the same Popup: defaults.hasPopup = true; // create CodeEditor JEditor ta = new JEditor(defaults);
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