I'm looking for a simple way to make this. I can't find it anywhere and I've already tried the API.
So I have a sentence:
Don't have an account? Click here
I want to make the "here" word blue and clickable - into a button. How can achieve this? Do I just make an invisible button behind the position of the text?
If you are using a JTextPane you can use the insertComponent() method to insert a new JLabel that is of the same font as the JTextPane font and you can customize the JLabel the way you want like setting the cursor to hand cursor thereby giving it a clickable look and feel. Save this answer. Show activity on this post.
Creating a Text FieldLabel label1 = new Label("Name:"); TextField textField = new TextField (); HBox hb = new HBox(); hb. getChildren(). addAll(label1, textField); hb. setSpacing(10);
Creating a Hyperlink Hyperlink link = new Hyperlink(); link. setText("http://example.com"); link. setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { System. out.
You can change the text of a label using its setText() method. This can be done while the application is running. Here is an example of setting the text of a JavaFX Label: label.
Solution
Use a TextFlow (Java 8):
TextFlow flow = new TextFlow(
new Text("Don't have an account? "), new Hyperlink("Click here")
);
Use a FlowPane (Java 7):
FlowPane flow = new FlowPane();
flow.getChildren().addAll(
new Text("Don't have an account? "), new Hyperlink("Click here")
);
Sample
Here is a complete, executable example (Java 8):
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.text.*;
import javafx.stage.Modality;
import javafx.stage.*;
public class TextLink extends Application {
@Override
public void start(final Stage primaryStage) throws Exception {
Stage accountCreation = buildAccountCreationStage(primaryStage);
Hyperlink createAccount = buildCreateAccountLink(primaryStage, accountCreation);
TextFlow flow = new TextFlow(
new Text("Don't have an account? "), createAccount
);
flow.setPadding(new Insets(10));
primaryStage.setScene(new Scene(new Group(flow)));
primaryStage.show();
}
private Hyperlink buildCreateAccountLink(Stage primaryStage, Stage accountCreation) {
Hyperlink createAccount = new Hyperlink("Click here");
createAccount.setOnAction(event -> {
accountCreation.setX(primaryStage.getX());
accountCreation.setY(primaryStage.getY() + primaryStage.getHeight());
accountCreation.show();
});
return createAccount;
}
private Stage buildAccountCreationStage(Stage primaryStage) {
Stage accountCreation = new Stage(StageStyle.UTILITY);
accountCreation.initModality(Modality.WINDOW_MODAL);
accountCreation.initOwner(primaryStage);
accountCreation.setTitle("Create Account");
accountCreation.setScene(new Scene(new Label("<Account Creation Form Goes Here>"), 250, 50));
return accountCreation;
}
public static void main(String[] args) { launch(args); }
}
The default link highlight is kind of strange with the dashed border around it (if you wish you can style it with css to get something nicer and more similar to links on the web; i.e. just different colored text to indicate visited and unvisited links).
Aside
For you particular message, you should just make the "Don't have an account" text a hyperlink and get rid of the "Click here" text (as recommended by the w3c web standards body).
Related
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With