Copiable Label/TextField/LabeledText in JavaFX - javafx

I just want to create copiable label in JavaFX.
I have tried to create TextField that have no background, have no focus border and default background color, but I have no success.
I have found a lot of questions how to remove focus background from control but all of that looks like "hacks".
Is there are any standard solution to implement copyable text?

You can create a TextField without the border and background color with css:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class CopyableLabel extends Application {
#Override
public void start(Stage primaryStage) {
TextField copyable = new TextField("Copy this");
copyable.setEditable(false);
copyable.getStyleClass().add("copyable-label");
TextField tf2 = new TextField();
VBox root = new VBox();
root.getChildren().addAll(copyable, tf2);
Scene scene = new Scene(root, 250, 150);
scene.getStylesheets().add(getClass().getResource("copyable-text.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
and
copyable-text.css:
.copyable-label, .copyable-label:focused {
-fx-background-color: transparent ;
-fx-background-insets: 0px ;
}

This is the solution I used, where there is a small button besides the label to be able to copy the text:
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import org.controlsfx.glyphfont.FontAwesome;
import org.controlsfx.glyphfont.Glyph;
import java.util.Locale;
public class CopiableLabel extends Label
{
public CopiableLabel()
{
addCopyButton();
}
public CopiableLabel(String text)
{
super(text);
addCopyButton();
}
public CopiableLabel(String text, Node graphic)
{
super(text, graphic);
}
private void addCopyButton()
{
Button button = new Button();
button.visibleProperty().bind(textProperty().isEmpty().not());
button.managedProperty().bind(textProperty().isEmpty().not());
button.setFocusTraversable(false);
button.setPadding(new Insets(0.0, 4.0, 0.0, 4.0));
button.setOnAction(actionEvent -> AppUtils.copyToClipboard(getText()));
Glyph clipboardIcon = AppUtils.createFontAwesomeIcon(FontAwesome.Glyph.CLIPBOARD);
clipboardIcon.setFontSize(8.0);
button.setGraphic(clipboardIcon);
setGraphic(button);
setContentDisplay(ContentDisplay.RIGHT);
}
}

Related

Mouse does not change to pointer on button hover CSS

I am trying to set the mouse to a pointer when a hover event is detected but the cursor does not change. If anyone can tell me what I am doing wrong I would be so grateful.
package replit;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.geometry.Pos;
public class Main extends Application
{
#Override
public void start(Stage primaryStage) {
VBox vbox = new VBox();
vbox.setSpacing(20);
button button = new Button("Click");
button.setAlignment(Pos.CENTER);
vbox.getChildren().add(button);
Scene scene = new Scene(vbox, 300, 200);
scene.getStylesheets().add("Style.css");
primaryStage.setTitle("A Simple Scene!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The CSS
.button:hover{
-fx-cursor: pointer;
}

Editable ComboBox cleared when i pressed space or arrow buttons - JavaFX

I am making a Editable ComboBox which show any values contain the input and it works fine.
But the problem is in the input field, whenever i typed Space or arrow buttons the input field keep reseting.
I tried setonKeyPressed and setonKeyTyped too but it isn't solve the problem.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
ObservableList<String> fruit = FXCollections.observableArrayList();
fruit.add("apple");
fruit.add("orange");
fruit.add("banana");
ComboBox myComboBox = new ComboBox();
myComboBox.setEditable(true);
myComboBox.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent keyEvent) {
myComboBox.getItems().clear();
myComboBox.show();
String input = myComboBox.getEditor().getText();
for ( int i = 0 ; i < fruit.size(); i++) {
if(fruit.get(i).contains(input)) { //Check if the list contains the input
myComboBox.getItems().add(fruit.get(i));
}
}
}
});
HBox hbox = new HBox(myComboBox);
primaryStage.setScene(new Scene(hbox, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

How to change TreeItem's underline color in JavaFX?

I have a JavaFX TreeView which has TreeItems with two basic colors , red and white . When they are selected they both have -fx-underline:true but i have a problem with the line being Red or White i want the underline to be always white , is that possible with css in JavaFX ?
I have tried the below but it doesn't work .
.tree-cell-white:selected {
-fx-font-weight: bold ;
-fx-text-fill:white;
-fx-underline: true;
-fx-underline-fill: orange;
-fx-underline-color: blue;
}
.tree-cell-red:selected {
-fx-font-weight: bold ;
-fx-text-fill:firebrick;
-fx-underline: true;
}
Unfortunately I could not find how to do it by JavaFX CSS, but I found another solution. That is I created MyTrack class that derives from HBox and added an ImageView as an music/mp3 icon and Label with text.
Here is a little trick I underlined label by setting its bottom border color and set MyTrack as a graphic for TreeItem.
package underlined.treeview;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.stage.Stage;
import java.util.Arrays;
import java.util.List;
public class TreeViewApp extends Application {
public static void main(String[] args) {
launch(args);
}
private Parent createContent() {
TreeItem<String> treeRoot = new TreeItem<>("Root node");
List<TreeItem<String>> tracks = Arrays.asList(
new TreeItem<>(""),
new TreeItem<>(""),
new TreeItem<>(""));
for (TreeItem<String> track : tracks)
track.setGraphic(new MyTrack("Track"));
treeRoot.getChildren().addAll(tracks);
TreeView treeView = new TreeView();
treeView.setShowRoot(true);
treeView.setRoot(treeRoot);
treeRoot.setExpanded(true);
return treeView;
}
#Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(createContent());
primaryStage.setScene(scene);
primaryStage.show();
}
}
and MyTrack class:
package underlined.treeview;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
public class MyTrack extends HBox {
public MyTrack(String text) {
Label label = new Label(text);
label.setStyle("-fx-border-color: aqua;" +
"-fx-border-width: 0 0 1 0;");
Image image = new Image(getClass().getResource("/img/icon/mp3.png")
.toExternalForm());
ImageView imageView = new ImageView(image);
imageView.setFitWidth(16);
imageView.setFitHeight(16);
getChildren().addAll(imageView, label);
}
}
And Voila!!!
In my example bottom border color is aqua here. Note that TreeItem should be instantiated with empty String. It was just an example you need to optimize it yourself.

javafx fxml bring window to front form itself

I need to bring to front JavaFX FXML window from itself. Something like this:
procedure (boolean close)
{
if(close)
current_window.toFront();
}
How should I get this window(scene) ?
Try this
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) {
Text text = new Text("!");
text.setFont(new Font(40));
VBox box = new VBox();
box.getChildren().add(text);
final Scene scene = new Scene(box,300, 250);
scene.setFill(null);
stage.setScene(scene);
stage.show();
stage.toFront();
}
public static void main(String[] args) {
launch(args);
}
}
If you have access to any of the nodes, you can use the following
((Stage)node.getScene().getWindow()).toFront();

JavaFX PopOver From ControlFX

Can someone write a short JavaFX example of a Popover from ControlFX ? I haven't been able to get it to work. Any help is greatly appreciated!
This answer is a simple use of ControlsFX's PopOver.
When the mouse moves over the Label the PopOver appears. When the mouse exits the Label the PopOver disappears.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.controlsfx.control.PopOver;
/**
*
* #author Sedrick
*/
public class JavaFXApplication35 extends Application {
#Override
public void start(Stage primaryStage) {
//Build PopOver look and feel
Label lblName = new Label("John Doe");
Label lblStreet = new Label("123 Hello Street");
Label lblCityStateZip = new Label("MadeUpCity, XX 55555");
VBox vBox = new VBox(lblName, lblStreet, lblCityStateZip);
//Create PopOver and add look and feel
PopOver popOver = new PopOver(vBox);
Label label = new Label("Mouse mouse over me");
label.setOnMouseEntered(mouseEvent -> {
//Show PopOver when mouse enters label
popOver.show(label);
});
label.setOnMouseExited(mouseEvent -> {
//Hide PopOver when mouse exits label
popOver.hide();
});
StackPane root = new StackPane();
root.getChildren().add(label);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
you must google out for that
many sites are available nowadays
https://bitbucket.org/controlsfx/controlsfx/commits/dca9619e05de26d176aaafe785c3b94f022562ef
https://bitbucket.org/controlsfx/controlsfx/pull-request/158/initial-commit-of-popover-control/activity
and etc. just Google out.
Here we have program known as "HelloPopOver".
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.ListView;
import javafx.scene.control.Slider;
import javafx.scene.control.TitledPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import org.controlsfx.ControlsFXSample;
import org.controlsfx.control.popover.PopOver;
import org.controlsfx.control.popover.PopOverController;
import org.controlsfx.control.popover.PopOverHeader;
import org.controlsfx.control.popover.PopOverTitledPane;
import org.controlsfx.samples.Utils;
public class HelloPopOver extends ControlsFXSample {
private PopOverController<PopOver, Button> controller = new MyController();
#Override
public Node getPanel(Stage stage) {
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setAlignment(Pos.CENTER);
grid.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent evt) {
controller.hidePopOver();
}
});
for (int i = 0; i < 10; i++) {
final Button button = new Button("Button " + i);
grid.add(button, i % 2, i / 2);
button.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent evt) {
controller.hidePopOver();
if (evt.getClickCount() == 2) {
controller.setupPopOver(button);
controller.showPopOver(button, evt.getScreenX(),
evt.getScreenY());
}
}
});
}
return grid;
}
class MyController extends PopOverController<PopOver, Button> {
#Override
protected PopOver createPopOver(final Button button) {
PopOver editor = new PopOver();
PopOverHeader<?> header = (PopOverHeader<?>) editor.getHeader();
header.setTitle(button.getText() + " (edit me)");
header.setSubtitle("Just some random controls (edit me)");
editor.setDetachedTitle(button.getText());
editor.getPanes().add(createTitledPane("Start Time & Duration"));
editor.getPanes().add(createTitledPane("Dependencies"));
editor.getPanes().add(createTitledPane("Priority"));
editor.getPanes().add(createTitledPane("Assignments / Resources"));
editor.setExpandedPane(editor.getPanes().get(0));
editor.setFooter(new Footer());
ColorPicker picker = (ColorPicker) header.getExtra();
picker.valueProperty().addListener(new ChangeListener<Color>() {
#Override
public void changed(ObservableValue<? extends Color> value,
Color oldColor, Color newColor) {
button.setBackground(new Background(new BackgroundFill(
newColor, CornerRadii.EMPTY, Insets.EMPTY)));
}
});
return editor;
}
}
private TitledPane createTitledPane(String title) {
VBox box = new VBox(5);
box.getChildren().add(new Button("Test"));
box.getChildren().add(new Slider());
ListView<String> view = new ListView<>();
view.setPrefHeight(100);
box.getChildren().add(view);
final TitledPane pane = new PopOverTitledPane(title, box);
pane.setTextAlignment(TextAlignment.LEFT);
Pane connectivityArrow = (Pane) pane.lookup(".arrow");
if (connectivityArrow != null) {
connectivityArrow.translateXProperty().bind(
pane.widthProperty().subtract(
connectivityArrow.widthProperty().multiply(2)));
}
return pane;
}
class Footer extends FlowPane {
public Footer() {
super(Orientation.HORIZONTAL);
setAlignment(Pos.CENTER_RIGHT);
Button delete = new Button("Delete");
getChildren().add(delete);
delete.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent evt) {
}
});
}
}
public static void main(String[] args) {
Application.launch(args);
}
#Override
public String getSampleName() {
return "PopOver";
}
#Override
public String getJavaDocURL() {
return Utils.JAVADOC_BASE
+ "org/controlsfx/control/popover/PopOver.html";
}
#Override
public String getSampleDescription() {
return "An implementation of a pop over control as used by Apple for its iCal application. A pop over allows"
+ "the user to see and edit an objects properties. The pop over gets displayed in its own popup window and"
+ "can be torn off in order to create several instances of it.";
}
}

Resources