1. TextField Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | import java.io.File; import java.util.List; import javafx.event.EventHandler; import javafx.scene.control.TextField; import javafx.scene.input.Clipboard; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; public class FxTextField implements FxNode { TextField text; public FxTextField(Runnable runnable) { text = new TextField(); FxDrop.setDropHandler(text, new FxRunnable() { @SuppressWarnings("unchecked") @Override public void run(Object... object) { if( object[0] instanceof List<?> ) { List<File> files = (List<File>) object[0]; text.setText( files.get(0).getPath() ); } } }); if( runnable != null ) { text.textProperty().addListener((observable, oldValue, newValue) -> { runnable.run(); }); } } public FxTextField() { this(null); } @Override public TextField get() { return text; } public void setOnEnterHandler(Runnable runnable) { text.setOnKeyPressed(new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent event) { if (event.getCode().equals( KeyCode.ENTER) ) { runnable.run(); } } }); } public void clear() { text.clear(); } public String getText() { return text.getText(); } public void setText(String value) { text.setText(value); } public void setFontSize(int size) { if( size > 0 ) { text.setStyle("-fx-font-size: " + size + ";"); //1em = 50px } } public void clearAllText() { text.clear(); } public void copyAllText() { text.selectAll(); text.copy(); text.deselect(); } public void pasteAllText() { text.selectAll(); text.paste(); text.deselect(); } public void pasteAllHtml() { final Clipboard clipboard = Clipboard.getSystemClipboard(); String data; if( clipboard.hasHtml() ) { data = clipboard.getHtml(); } else { data = clipboard.getString(); } text.setText(data); } } |