-1

I have a TextField in my program that will have data entered by the user, but I also have a variable value somewhere else that I need to permanently display at the end of my TextField. It cannot disappear when the user enters any data in the TextField. Can anyone give me a good implementation? Thanks.

[UserInput                (miles)]

**Above is an example of what I am talking about. "Miles" needs to always be in the TextField while the UserInput is changing.

EDIT: "Implementation" was a bad choice of words. Let me rephrase, I can set up the field myself, but I am having trouble finding a way to set permanent text in a textfield. Just wondering if anyone knows an easy way.

Alex Johnson
  • 504
  • 5
  • 15
  • "Can anyone give me a good implementation?" This sentence almost perfectly embodies an incorrect way to ask an SO question. We won't write your code for you. Post what you've tried and what the problem is, then we'll be glad to help. – Kon Mar 07 '14 at 15:01
  • My problem isn't what I've tried. I know how to create a TextField. I know how to set it up to get user input. I know how to set text to a textfield. I don't know the specific way how to set permanent text. I don't need everything fleshed out, just asking about the permanent text. – Alex Johnson Mar 07 '14 at 15:03
  • Why not simply put a `Label` with the permanent text right beside your `TextField`? – Alexis Leclerc Mar 07 '14 at 15:05
  • @AlexisLeclerc I normally would do that, but I specifically need the value to be in the field. – Alex Johnson Mar 07 '14 at 15:07
  • @AlexJohnson Why is that? I mean, if it's because you need the permanent string in your retrieved user input, you can simply append the label's content to the user input wherever you need it. – Alexis Leclerc Mar 07 '14 at 15:10
  • @AlexisLeclerc No, it is not to append to the user input. It is essentially a label within the textfield. If the label was displaying, say length, I need to it be able to change to miles, feet, inches, etc. The label is purely to indicate specifics for the field. – Alex Johnson Mar 07 '14 at 15:14
  • 1
    http://stackoverflow.com/questions/7421337/limited-selection-in-a-jtextfield-jtextcomponent Take a look here, I guess that's similar to what you need. However, as said, I think you should keep it simple, and just add a label after the field. – Hugo Sousa Mar 07 '14 at 15:16
  • @HugoSousa Alright, thanks. That might be close enough to help me. – Alex Johnson Mar 07 '14 at 15:25

2 Answers2

0

You could put a transparent textfield over a label and bind the 2 together. Something like this but with better styling.

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Text extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField txtUser = new TextField();
        txtUser.setStyle("-fx-background-color: transparent;-fx-border-color:blue;");
        Label txtBG = new Label(" (miles)");
        Label labelUser = new Label();
        labelUser.textProperty().bind(txtUser.textProperty());
        Label labelAll = new Label();
        labelAll.textProperty().bind(Bindings.concat(
                labelUser.textProperty())
                .concat(txtBG.textProperty()));

        StackPane sp = new StackPane();
        sp.getChildren().addAll(txtBG, txtUser);
        sp.setPrefSize(100, 12);

        VBox root = new VBox();
        root.getChildren().addAll(sp,labelUser,labelAll);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("transparent text test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

I would use a HBox instead of a stack pane but it's one way to satisfy the requirement that "miles" is 'inside' the texfield's borders.

brian
  • 10,619
  • 4
  • 21
  • 79
0

This is a small example doing what you want ! I have used the focus property of textfield to add and remove miles from it !

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TextBinding extends Application {

@Override
public void start(Stage primaryStage) {
    final TextField user = new TextField();
    TextField demo = new TextField();
    user.setStyle("-fx-background-color: transparent;-fx-border-color:blue;");
    user.focusedProperty().addListener(new ChangeListener<Boolean>()
            {
                @Override
                public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)
                {
                    if (newPropertyValue)
                    {
                        user.setText(user.getText().replace(" miles", ""));
                    }
                    else
                    {
                        user.setText(user.getText().concat(" miles"));
                    }
                }
            });
    VBox root = new VBox();
    root.getChildren().addAll(user,demo);
    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("transparent text test");
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String args[]) {
    launch(args);
}
}
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176