Skip to content

Commit e2a48ed

Browse files
committed
Added setPlaceholder( Node, Pos )
1 parent d73b4ab commit e2a48ed

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Collections;
99
import java.util.List;
1010
import java.util.Locale;
11+
import java.util.Objects;
1112
import java.util.Optional;
1213
import java.util.function.BiConsumer;
1314
import java.util.function.BiFunction;
@@ -22,7 +23,6 @@
2223
import javafx.application.ConditionalFeature;
2324
import javafx.application.Platform;
2425
import javafx.beans.NamedArg;
25-
import javafx.beans.binding.Bindings;
2626
import javafx.beans.property.BooleanProperty;
2727
import javafx.beans.property.DoubleProperty;
2828
import javafx.beans.property.ObjectProperty;
@@ -31,7 +31,6 @@
3131
import javafx.beans.property.SimpleObjectProperty;
3232
import javafx.beans.value.ObservableValue;
3333
import javafx.collections.FXCollections;
34-
import javafx.collections.ListChangeListener.Change;
3534
import javafx.collections.ObservableSet;
3635
import javafx.css.CssMetaData;
3736
import javafx.css.PseudoClass;
@@ -44,6 +43,7 @@
4443
import javafx.geometry.Bounds;
4544
import javafx.geometry.Insets;
4645
import javafx.geometry.Point2D;
46+
import javafx.geometry.Pos;
4747
import javafx.scene.Node;
4848
import javafx.scene.control.ContextMenu;
4949
import javafx.scene.control.IndexRange;
@@ -57,8 +57,6 @@
5757
import javafx.scene.layout.Region;
5858
import javafx.scene.paint.Color;
5959
import javafx.scene.paint.Paint;
60-
import javafx.scene.shape.LineTo;
61-
import javafx.scene.shape.PathElement;
6260
import javafx.scene.text.TextFlow;
6361

6462
import org.fxmisc.flowless.Cell;
@@ -394,10 +392,12 @@ public Node getParagraphGraphic( int parNdx ) {
394392
* This Node is shown to the user, centered over the area, when the area has no text content.
395393
* <br>To customize the placeholder's layout override {@link #configurePlaceholder( Node )}
396394
*/
397-
public final void setPlaceholder(Node value) { placeHolderProp.set(value); }
395+
public final void setPlaceholder(Node value) { setPlaceholder(value,Pos.CENTER); }
396+
public void setPlaceholder(Node value, Pos where) { placeHolderProp.set(value); placeHolderPos = Objects.requireNonNull(where); }
398397
private ObjectProperty<Node> placeHolderProp = new SimpleObjectProperty<>(this, "placeHolder", null);
399398
public final ObjectProperty<Node> placeholderProperty() { return placeHolderProp; }
400399
public final Node getPlaceholder() { return placeHolderProp.get(); }
400+
private Pos placeHolderPos = Pos.CENTER;
401401

402402
private ObjectProperty<ContextMenu> contextMenu = new SimpleObjectProperty<>(null);
403403
@Override public final ObjectProperty<ContextMenu> contextMenuObjectProperty() { return contextMenu; }
@@ -904,6 +904,7 @@ protected void handleInputMethodEvent( InputMethodEvent event )
904904
}
905905

906906
private Node placeholder;
907+
private boolean positionPlaceholder = false;
907908

908909
private void displayPlaceHolder( boolean show, Node newNode )
909910
{
@@ -929,15 +930,7 @@ private void displayPlaceHolder( boolean show, Node newNode )
929930
*/
930931
protected void configurePlaceholder( Node placeholder )
931932
{
932-
placeholder.layoutYProperty().bind( Bindings.createDoubleBinding( () ->
933-
(getHeight() - placeholder.getLayoutBounds().getHeight()) / 2,
934-
heightProperty(), placeholder.layoutBoundsProperty() )
935-
);
936-
937-
placeholder.layoutXProperty().bind( Bindings.createDoubleBinding( () ->
938-
(getWidth() - placeholder.getLayoutBounds().getWidth()) / 2,
939-
widthProperty(), placeholder.layoutBoundsProperty() )
940-
);
933+
positionPlaceholder = true;
941934
}
942935

943936
/* ********************************************************************** *
@@ -1699,8 +1692,13 @@ protected void layoutChildren() {
16991692
});
17001693

17011694
Node holder = placeholder;
1702-
if (holder != null && holder.isResizable() && holder.isManaged()) {
1703-
holder.autosize();
1695+
if (holder != null && holder.isManaged()) {
1696+
if (holder.isResizable()) holder.autosize();
1697+
if (positionPlaceholder) Region.positionInArea
1698+
(
1699+
holder, getLayoutX(), getLayoutY(), getWidth(), getHeight(), getBaselineOffset(),
1700+
ins, placeHolderPos.getHpos(), placeHolderPos.getVpos(), isSnapToPixel()
1701+
);
17041702
}
17051703
}
17061704

richtextfx/src/main/java/org/fxmisc/richtext/StyledTextField.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import javafx.css.StyleableObjectProperty;
2020
import javafx.event.ActionEvent;
2121
import javafx.event.EventHandler;
22+
import javafx.geometry.Pos;
2223
import javafx.scene.AccessibleRole;
2324
import javafx.scene.Group;
2425
import javafx.scene.Node;
@@ -213,9 +214,11 @@ public String getName() {
213214
* <p>The Text will be aligned according to the text fields alignment setting and have a default
214215
* text fill of GRAY unless you have changed it by any means, e.g. with CSS "-fx-prompt-text-fill"
215216
*/
217+
public final void setPromptText( Text value ) { placeholderProperty().set( value ); }
216218
public final ObjectProperty<? super Text> promptTextProperty() { return placeholderProperty(); }
217219
public final Text getPromptText() { return getPlaceholder() instanceof Text ? (Text) getPlaceholder() : null; }
218-
public final void setPromptText( Text value ) { setPlaceholder( value ); }
220+
/** setPlaceholder is not supported by StyledTextField, use setPromptText instead */
221+
@Override public void setPlaceholder( Node value, Pos where ) { throw new UnsupportedOperationException("Use setPromptText instead"); }
219222
@Override protected void configurePlaceholder( Node placeholder )
220223
{
221224
placeholder.layoutYProperty().bind( Bindings.createDoubleBinding( () ->

0 commit comments

Comments
 (0)