Create multiline textbox with scrollbar in JavaFX
Currently I just played around with JavaFx. For the program I had build I had to use a multiline textbox with scrollbar. After some work here is the result:
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
*TextboxMultline.fx
*
*Created on Nov 4, 2010, 13:55:56 PM
*/
package de.steffakasid.ui;
import javafx.scene.Group;
import javafx.scene.control.ScrollBar;
import javafx.scene.effect.InnerShadow;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextOrigin;
public class TextboxMultiline extends Group {
public var width: Float;
public var height: Float;
public var backgroundColor: Color;
public var foregroundColor: Color;
public var textContent: String;
public var wrappingWidth: Float;
public var font: Font;
public var enableHorizontalScroll : Boolean = false;
public var enableVerticalScroll : Boolean = false;
public var enableAutoScroll: Boolean = false;
var clipper: Rectangle = Rectangle {
width: bind width - 10,
height: bind height - 10,
y: bind (text.translateY * - 1) + 5;
x: bind (text.translateX * - 1) + 5;
};
var text: Text = Text {
x: bind scrollBarVertical.width,
y: 5,
font: bind font,
fill: bind foregroundColor,
textOrigin : TextOrigin.TOP,
translateY: bind 0 - scrollBarVertical.value,
translateX: bind 0 - scrollBarHorizontal.value,
clip: clipper,
wrappingWidth: bind wrappingWidth;
};
var textBackground: Rectangle = Rectangle {
arcHeight : 10,
arcWidth : 10,
width: bind width,
height: bind height,
fill: bind backgroundColor,
effect: InnerShadow {
choke: 0.01,
offsetX: 0,
offsetY: 0,
radius: 5,
color: javafx.scene.paint.Color.BLACK,
}
};
var scrollBarVertical: ScrollBar = ScrollBar {
height: bind height
blockIncrement: 10
clickToPosition: true
min: 0
max: bind if(text.layoutBounds.height > height) text.layoutBounds.height - height else 1 ,
vertical: true
visible: bind if((height - 10) < text.layoutBounds.height) true else false;
}
var scrollBarHorizontal: ScrollBar = ScrollBar {
layoutY: bind height - 1,
width: bind width,
blockIncrement: 1,
clickToPosition: false,
min: 0,
max: bind if(text.layoutBounds.width > (width - 10)) text.layoutBounds.width - (width - 20) else 1,
vertical: false,
visible: bind if(text.layoutBounds.width > (width - 20)) true else false,
}
init {
if(null == foregroundColor) {
foregroundColor = Color.BLACK;
}
if(null == backgroundColor) {
backgroundColor = Color.WHITE;
}
if(null != textContent) {
text.content += textContent;
}
this.content = [
textBackground,
text
];
if(enableVerticalScroll == true) {
insert scrollBarVertical into this.content
}
if(enableHorizontalScroll == true) {
insert scrollBarHorizontal into this.content
}
}
public function setText(textContent: String) {
text.content += textContent;
if(enableAutoScroll == true) {
scrollBarVertical.value = scrollBarVertical.max;
}
println("Text bounds: {text.layoutBounds}");
println("Scrollbar max: {scrollBarVertical.max}");
}
};