Created
June 8, 2012 09:38
-
-
Save jewelsea/2894725 to your computer and use it in GitHub Desktop.
Sample JavaFX data entry control for maintaining a log of comments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.*; | |
import javafx.beans.value.*; | |
import javafx.event.EventHandler; | |
import javafx.scene.Scene; | |
import javafx.scene.control.*; | |
import javafx.scene.input.*; | |
import javafx.scene.layout.*; | |
import javafx.scene.web.WebView; | |
import javafx.stage.Stage; | |
// an editable log of entered comments. | |
// https://forums.oracle.com/forums/thread.jspa?threadID=2399060 | |
// "Go to new line when Shift+Enter is pressed in text area" | |
public class CommentLog extends Application { | |
// create some key combination matchers to override default text area key handlers. | |
final private KeyCombination CTRL_SHIFT_UP = new KeyCodeCombination(KeyCode.UP, KeyCombination.SHIFT_DOWN, KeyCombination.CONTROL_DOWN); | |
final private KeyCombination CTRL_SHIFT_DOWN = new KeyCodeCombination(KeyCode.DOWN, KeyCombination.SHIFT_DOWN, KeyCombination.CONTROL_DOWN); | |
final private KeyCombination SHIFT_ENTER = new KeyCodeCombination(KeyCode.ENTER, KeyCombination.SHIFT_DOWN); | |
final private KeyCombination ENTER = new KeyCodeCombination(KeyCode.ENTER); | |
final private KeyCombination ESC = new KeyCodeCombination(KeyCode.ESCAPE); | |
final private String INSTRUCTIONS = | |
"<body style='background: khaki; font-family: Trebuchet MS; line-height: 140%; font-size: 13px;'>" + | |
"<strong>Key assignments</strong>" + | |
"<dl>" + | |
"<dt>ENTER</dt><dd>Submit comment</dd>" + | |
"<dt>ESC</dt><dd>Cancel edit</dd>" + | |
"<dt>SHIFT + ENTER</dt><dd>New line</dd>" + | |
"<dt>CTRL + SHIFT + UP</dt><dd>Edit previous comment.</dd>" + | |
"<dt>CTRL + SHIFT + DOWN</dt><dd>Edit next comment.</dd>" + | |
"</dl>" + | |
"</body>"; | |
public static void main(String[] args) { launch(args); } | |
@Override public void start(Stage stage) { | |
stage.setTitle("Comment Log"); | |
// create some html instructions. | |
final WebView instructions = new WebView(); | |
instructions.getEngine().loadContent(INSTRUCTIONS); | |
// a comment log. | |
final ListView<String> listview = new ListView<>(); | |
// a text area for entering comments. | |
final TextArea textArea = new TextArea(); | |
textArea.setPrefRowCount(4); | |
textArea.setPromptText("Enter a comment"); | |
textArea.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() { | |
public void handle(KeyEvent event) { | |
if (ENTER.match(event)) { // on enter copy the entered text to the listview. | |
if (!textArea.getText().trim().isEmpty()) { | |
String selectedItem = listview.getSelectionModel().getSelectedItem(); | |
if (selectedItem != null) { | |
if (!selectedItem.equals(textArea.getText())) { // replace the existing selection | |
listview.getItems().set(listview.getSelectionModel().getSelectedIndex(), textArea.getText()); | |
listview.getSelectionModel().clearSelection(); | |
textArea.setText(""); | |
textArea.clear(); | |
} | |
} else { // create a new item | |
listview.getItems().add(textArea.getText()); | |
textArea.setText(""); | |
textArea.clear(); | |
} | |
event.consume(); | |
} | |
} else if (SHIFT_ENTER.match(event)) { // add a new line. | |
textArea.deleteText(textArea.getSelection()); | |
textArea.insertText(textArea.getCaretPosition(), "\n"); | |
event.consume(); | |
} else if (ESC.match(event)) { // cancel edit | |
textArea.clear(); | |
listview.getSelectionModel().clearSelection(); | |
} else if (CTRL_SHIFT_UP.match(event)) { // edit previous item | |
listview.getSelectionModel().selectPrevious(); | |
event.consume(); | |
} else if (CTRL_SHIFT_DOWN.match(event)) { // edit next item | |
listview.getSelectionModel().selectNext(); | |
event.consume(); | |
} | |
} | |
}); | |
// when the selection changes, copy it to the text area, so it can be edited. | |
listview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() { | |
@Override public void changed(ObservableValue<? extends String> prop, String before, String after) { | |
if (after != null && !after.equals(textArea.getText())) { | |
textArea.setText(after); | |
Platform.runLater(new Runnable() { | |
@Override public void run() { | |
textArea.requestFocus(); | |
} | |
}); | |
} | |
} | |
}); | |
// layout the scene. | |
VBox.setVgrow(listview, Priority.SOMETIMES); | |
textArea.setMinHeight(22); | |
instructions.setMinSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE); | |
instructions.setPrefSize(200, 232); | |
listview.setPrefHeight(125); | |
Pane layout = VBoxBuilder.create().spacing(10).children( | |
LabelBuilder.create().text("Comment").style("-fx-font-weight: bold;").build(), | |
textArea, | |
LabelBuilder.create().text("Log").style("-fx-font-weight: bold;").build(), | |
listview, | |
instructions | |
).build(); | |
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;"); | |
stage.setScene(new Scene(layout)); | |
stage.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://forums.oracle.com/forums/thread.jspa?threadID=2399060 "Go to new line when Shift+Enter is pressed in text area"