Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,16 @@ public static void main(String[] args) {
@Override
public void start(Stage primaryStage) {
CodeArea codeArea = new CodeArea();
codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
String stylesheet = JavaKeywords.class.getResource("java-keywords.css").toExternalForm();

codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea,stylesheet));
codeArea.textProperty().addListener((obs, oldText, newText) -> {
codeArea.setStyleSpans(0, computeHighlighting(newText));
});
codeArea.replaceText(0, 0, sampleCode);

Scene scene = new Scene(new StackPane(codeArea), 600, 400);
scene.getStylesheets().add(JavaKeywords.class.getResource("java-keywords.css").toExternalForm());
scene.getStylesheets().add(stylesheet);
primaryStage.setScene(scene);
primaryStage.setTitle("Java Keywords Demo");
primaryStage.show();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
.keyword {
-fx-fill: brown;
-fx-fill: #8a6;
-fx-font-weight: bold;
}
.default {
-fx-fill: #666;
}
.lineno {
-fx-font-family: monospace;
-fx-background-color: #eee;
-fx-text-fill: #666;
-fx-padding: 0 5px;
}
.code-area {
-fx-font-family: monospace;
-fx-background-color: #eee;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.fxmisc.richtext;

import java.util.function.IntFunction;
import java.util.function.Supplier;

import javafx.scene.Node;
import javafx.scene.control.Label;
Expand All @@ -12,21 +13,27 @@ public class LineNumberFactory implements IntFunction<Node> {

private static final String STYLESHEET = LineNumberFactory.class.getResource("lineno.css").toExternalForm();

public static IntFunction<Node> get(StyledTextArea<?> area,String customStylesheet) {
return new LineNumberFactory(area,customStylesheet);
}
public static IntFunction<Node> get(StyledTextArea<?> area) {
return new LineNumberFactory(area);
return new LineNumberFactory(area,STYLESHEET);
}

private final EventStream<Integer> nParagraphs;

private LineNumberFactory(StyledTextArea<?> area) {
private LineNumberFactory(StyledTextArea<?> area,String Stylesheet) {
nParagraphs = EventStreams.sizeOf(area.getParagraphs());
this.Stylesheet = Stylesheet;
}

private final String Stylesheet;

@Override
public Node apply(int idx) {
Label lineNo = new Label();
lineNo.getStyleClass().add("lineno");
lineNo.getStylesheets().add(STYLESHEET);
lineNo.getStylesheets().add(Stylesheet);

// When removed from the scene, stay subscribed to never(), which is
// a fake subscription that consumes no resources, instead of staying
Expand Down