I am writing a javafx program, for moving text. The program display the background (some red points) and text at the beginning.
Now, I would like to add a menu to select the content of text to display. In addition, I want the scene to display nothing at the beginning, then after I select content of text, the program starts to display everything...
Is there any special command to implement this?
PS: I add the contents (text and shapes) -> "Group" -> "root".
I think what you want is something along this:
public class PersonOverviewController implements Initializable{
#FXML
BorderPane paneWithControls;
#FXML
MenuItem menuItem;
#FXML
Label text;
#FXML
private void initialize() {
paneWithControls.setVisible(false); //hide content
menuItem.setOnAction(new EventHandler<ActionEvent>() { //implementing action listener
#Override
public void handle(ActionEvent event) {
text.setText("Some text you want to display"); //set value for controls
paneWithControls.setVisible(true); //display content
}
});
}
}
EDIT:
If you really want to "hide" the scene you can do something like this:
primaryStage.setScene(null);
and later on:
primaryStage.setScene(sceneObject1);
However this is not good way to achieve result you are looking for. Once you have the scene, just change the root of it, or set properties of it's controls. I have mistaken this also when I was learning JavaFX.
Related
I am working with JavaFX, and I have a Scene object I created in SceneBuilder with a bunch of Button objects. I want the text of each of these Button objects to be underlined when the mouse cursor enters their area in the window, and to remove the underlining when the cursor leaves.
I know I can just type:
public class Controller {
#FXML
private Button exitButton;
public void exitButtonMouseEntered() {
this.exitButton.setUnderline(true);
}
public void exitButtonMouseLeft() {
this.exitButton.setUnderline(false);
}
}
However, doing this for each Button object is time-consuming, and it seems like there must be an easier way to force all the Button objects to have the same behavior given the same conditions.
How can I make a controller method that will affect all the Buttons in the Scene?
Apply the following css:
Button:hover{
-fx-underline: true;
}
Background Information: I am currently working in a Dialog class I have extended for my game. Inside of this dialog's content table I have both an Image and a Table (lets call it ioTable). Inside of ioTable I have a combination of both Labels and TextFields. The idea is that the dialog becomes a sort of form for the use to fill out.
Next, inside of the Dialog's button table, I want to include a "Clear" TextButton (clearButton). The idea that clearButton will clear any values written to the TextFields of ioTable.
My Question: Is is possible to add a listener to each of the TextFields of ioTable that will trigger when clearButton is pressed. As always, any other creative solution is more than welcome.
You could just give the EventListener a reference to the table you want to clear:
// Assuming getSkin() and ioTable are defined elsewhere and ioTable is final
TextButton clearButton = new TextButton("Clear", getSkin());
clearButton.addListener(new EventListener() {
#Override
public boolean handle(Event event) {
for(Actor potentialField : table.getChildren()) {
if(potentialField instanceof TextField) {
((TextField)potentialField).setText("");
}
}
return true;
}
});
// Add clearButton to your dialog
If you see yourself creating multiple clearButtons, you could easily wrap this in a helper method or extend TextButton.
I would like to hide or deactivate a TextField and its Label in my JavaFX application.
This is what I tried
myTextField.setVisible(false);
, but it's not working
I use Eclipse V4.5.0 on windows 7 with jfx8 V2.0.0
There is difference between hiding and deactivating a TextField in JavaFX.
To Hide :-
You need to set the visible property to false.
The possible reason's why its not working in your case is that if you have skipped mentioning the fx:id for your TextField or Label.
To do so just go through the fxml if using and set the fx:id="myTextField" , and then the same code that you have write will start to work.
The same is used to hide any Label.
To Deactivate :- There is a field named as disable just set that disable property to true to disable or deactivate any field.
I understand that you want to hide/show a text field (javaFX), i usually use the same method you mentioned
assume that the text field variable name is field then:
to hide it use
field.setVisible(false);
to show it use
field.setVisible(true);
and it works always for me.
I am not sure that I understand correctly your question, but I will try to answer to what I understand.
if you only want to deactivate TextField you can use:
myTextField.setEditable(false);
This will not "hide" the TextField it will be only not editable.
Based on your provided code, the problem might be in static created (in the FXML) TextField. What I suggest is to try and create the Pane and the TextField dynamically in the runtime.
Here is simple example how to create and use JavaFX components in the runtime :
public class ButtonInPane extends Application{
// Override the start method in the Application class
Button btn=new Button("OK");
HBox cont;
TextField myTextField;
public void start(Stage stage1){
Label myLable=new Label("Some Lable");
myTextField=new TextField("Some text");
cont=new HBox();
cont.getChildren().addAll(myLable,myTextField);
Stage stage = new Stage(); //this instead of JFrame
FlowPane pane2 = new FlowPane(); //this instead of JPanel
pane2.getChildren().addAll(btn,cont);
Scene scene2 = new Scene(pane2, 250, 50);
stage.setTitle("Button in a FlowPane"); // Set the stage title
stage.setScene(scene2); // Place the scene in the stage
stage.show(); // Display the stage
stage.setAlwaysOnTop(true);
//set event
setEventActions();
}
private void handlePlayAction() {
cont.setVisible(false);
//OR myTextField.setVisible(false);
}
private void setEventActions() {
this.btn.setOnAction(event -> this.handlePlayAction());
}
public static void main(String[] args)
{ launch(args);
}
}
You can use:
myTextField.setDisable(true);
It will disable field for a particular Action.
You can also do this for a Label in your .fxml file like this:
<Label layoutX="349.0" layoutY="85.0"
text="Label" visible="false" fx:id="actionSuccessLabel"/>
and then display it later in your Controller class like this:
actionSuccessLabel.setVisible(true);
Don't forget to do the setVisible or the visible property binding after the new function.
TextField textField = new TextField(field.getValue());
textField.visibleProperty().bind(field.getVisibleProperty());
There is plenty of online resource on how to add a graphic to a JavaFX button.
However I would like to know if there is a way to add a second (or any number, but more than 2 images probably woudn't make much sense in most situations) image.
My use case : i have a button with a square icon on the left, followed by a text label. The image is a representation of some real-life concept that button is linked with (could e.g. a car or a person). I would like to add a small icon to the right of some buttons, a "right chevron" to indicate the nature of the interaction.
I was thinking maybe to use a HBox with the full width as the graphic node of the button, and add the 2 images to it, but I don't think it is possible to put the text on top of the graphic node.
Any idea ?
Create your own custom node with icons and text and set it as graphic. Of course you don't show the button text because it's already in your custom graphic node.
Here's a simple example. You need to provide the files icon1.png and icon2.png.
public class ButtonWithMultipleIcons extends Application {
#Override
public void start(Stage primaryStage) {
try {
Group group = new Group();
Button button = new Button();
button.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
HBox hBox = new HBox();
ImageView icon1 = new ImageView( getClass().getResource( "icon1.png").toExternalForm());
ImageView icon2 = new ImageView( getClass().getResource( "icon2.png").toExternalForm());
Label label = new Label("Text");
//make the button grow if you want the right icon to always be on the right of the button :
label.setMaxWidth(Long.MAX_VALUE);
HBox.setHgrow(label, Priority.ALWAYS);
hBox.getChildren().addAll( icon1, label, icon2);
button.setGraphic(hBox);
group.getChildren().add( button);
Scene scene = new Scene(group,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
You can try using html formatting in your button and adding an image with the tag before and after your text.
You can find more information about it in here:http://docs.oracle.com/javase/tutorial/uiswing/components/html.html
I had the same thing to get along with. You describing the solution with using a HBox. I think this would also be the best way.
Do the following:
Create a new CustomButton with your own specific layout maybe using a HBox and extending Button class from JavaFX. Style it your way you need it and you can add as many images you want to have in your custom button. Maybe it can look like this:
public class CustomButton extends Button
{
private HBox hbox;
private ImageView image1;
private ImageView image2;
public CustomButton()
{
super();
// Here add your specific images to global or local variables and then add all the children (images) to your HBox layout and this one to the button.
}
}
The problem ist, that JavaFX provides normal buttons and not such deeply customized components. If you want to style it, you can use CSS anyway.
The following code is supposed to select a word in a JavaFX TextField:
public class NewFXMain extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
final TextInputControl textField = new TextField("Hello World, World!");
Button button = new Button("select");
button.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
textField.positionCaret(0);
textField.selectNextWord();
System.out.println(textField.getSelectedText());
}
});
VBox root = new VBox();
root.getChildren().add(textField);
root.getChildren().add(button);
primaryStage.setScene(new Scene(root, 300, 100));
primaryStage.show();
}
}
It prints Hello in the console, however in the interface nothing is selected (highlighted).
If one does the same with a TextArea, the text is correctly selected.
The (wrong) result with a TextField:
And the (correct) result with a TextArea:
What's going on?!?
TextField doesn't show selection unless it has focus (although I'm not sure is it a bug or a feature). You can see selection by using next code:
button.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
textField.requestFocus(); // get focus first
textField.positionCaret(0);
textField.selectNextWord();
System.out.println(textField.getSelectedText());
}
});
The answer to the question is that our understanding of native controls in this regard is that they both clear selection when the text field loses focus and that they (typically) select all text when gaining focus. Our intent with UI controls in JavaFX was to have a native feel but a customized look. Of course, we can have our mind changed on how this works, but it has to be balanced with the goal of maintaining a native feel (which includes select-all on focus gained and not showing selection on focus lost, even if selection exists. But since there is little point to there being any selection on focus lost if it is just going to select-all on focus gained and since weird edge cases will still exist as a result, we have to wonder whether it is worth changing this).