Request focus to shape is getting to the wrong control - javafx

I am trying to request focus to a rectangle shape in my scene. When I do that using the mouse the focus property listener hasn't fired even the shape is traversable to focus. I tried to add an EventListener to the shape to call the requestFocus() method on it but after the rectangle gain the focus another button that doesn't have any listeners or additional code stole the focus immediately. After I removed that button the same thing is done by another control (all the controls that stole the focus are before the rectangle in the order of addition to the root node). I tried to traverse the focusing using the Tab button and it works. So, how can I request the focusing to the rectangle using the mouse?
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="232.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ToolBar prefHeight="40.0" prefWidth="200.0">
<items>
<Button mnemonicParsing="false" text="Button" />
</items>
</ToolBar>
<HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<children>
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<ListView id="slotsList" prefHeight="200.0" prefWidth="200.0" />
</children>
</VBox>
<FilmTimeLine HBox.hgrow="ALWAYS" fx:id="timeLine"/>
</children>
</HBox>
</children>
</VBox>
Rectangle class that I am adding it to the scene inside the FilmTimeLine (the FilmTimeLine is a ScrollPane that has a Pane child. The rectangle is inside that Pane):
public class EventSlot extends Rectangle {
public EventSlot() {
setFocusTraversable(true);
setWidth(100);
setHeight(25);
setFill(Color.web("#9DD3DF"));
focusedProperty().addListener((observable, oldValue, newValue) -> {
if (isFocused()) {
setEffect(FOCUSED_EFFECT);
} else {
setEffect(INNER_SHADOW);
}
});
addEventHandler(MouseEvent.MOUSE_PRESSED, (mouseEvent) -> {
requestFocus();
});
}
}
gif of what is happening

When an action occurs, the system constructs event route.
Looks like your mouse event is handled by Rectangle (I assume it is inherited from Node), Pane and FilmTimeLine.
So we need to construct event route to ignore/filter all nodes except of selected rectangle.
Options to modify the route:
Consuming of an Event. Invoke mouseEvent.consume() to stop event propagation.
addEventHandler(MouseEvent.MOUSE_CLECKED, (mouseEvent) -> {
requestFocus();
mouseEvent.consume();
});
Making parent node mouse transparent Node.setMouseTransparent(true). If true, this node (together with all its children) is completely transparent so make sure that all rectagles mouseTransparent property is false.
pane.setMouseTransparent(true);//the FilmTimeLine is a ScrollPane that has a Pane child
Adding event filter Node.addEventFilter
pane.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) { event.consume(); };
});

Related

Making child collapse with parent

i will try to expose my problem as good as i can.
This is the XML
<VBox fx:id="main" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="500.0" prefWidth="800.0" style="-fx-background-color: #DEE2E6;" xmlns="http://javafx.com/javafx/17"
xmlns:fx="http://javafx.com/fxml/1">
<BorderPane prefHeight="408.0" prefWidth="600.0" VBox.vgrow="ALWAYS">
<left>
<AnchorPane fx:id="section_to_hide" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: red;" BorderPane.alignment="CENTER">
<Label fx:id="hide_btn1" layoutX="14.0" layoutY="54.0" onMouseClicked="#hide" text="Hide me">
<font>
<Font name="Prompt-Black" size="24.0"/>
</font>
</Label>
</AnchorPane>
</left>
<center>
<BorderPane fx:id="main_content" prefHeight="442.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<left>
<VBox fx:id="side_section_mails" prefHeight="500.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<AnchorPane prefHeight="59.0" prefWidth="200.0">
<Label fx:id="hide_btn" layoutX="14.0" layoutY="19.0" onMouseClicked="#hide" text="< Hide it">
<font>
<Font name="Prompt-Black" size="24.0"/>
</font>
</Label>
</AnchorPane>
</VBox>
</left>
<center>
<AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #CED4DA;" BorderPane.alignment="CENTER"/>
</center>
</BorderPane>
</center>
</BorderPane>
</VBox>
Controller:
public class Controller {
#FXML
private Label hide_btn;
#FXML
private AnchorPane section_to_hide;
#FXML
private void hide() {
Animation animation = new Timeline(
new KeyFrame(Duration.millis(300),
new KeyValue(section_to_hide.prefWidthProperty(), 0),
new KeyValue(section_to_hide.minWidthProperty(), 0)
)
);
animation.play();
}
#FXML
public void initialize() { }
}
The problem is: i want the content of the AnchorPane (section_to_hide) to collapse with its parent. Because this is what happens:
https://gyazo.com/41dfc5340a8e4495938cbc43d801fea2
It seems that the label inside the AnchorPane (section_to_hide) does not collapse with the parent.
Is there a way to make it work?
+1 for #Oboe answer. I would like add a bit more to his answer.
If you set managed/visible to false after the animation is finished, you may not see a smooth transition of the label. Means the complete Label is still visible while the pane closes (as in below gif) and gets invisible only after the animation is finished.
On the other hand, if you bind the clip with the bounds of the section that is hiding, you will get an effect of hiding something with a piece of cover (as in below gif) but not like hiding by sliding the section.
If the above behaviour is Ok for you, then no issues :). But if your are looking for a peferct sliding animation (as in below gif), you need to add another change of translating the clip position also.
But for this, you will need to wrap the hiding pane with an extra container.
The end implementation will be :
Outer container width is reduced to 0
Hiding container is translated in negative direction
And the clip is translated in positive direction.
Wrap section_to_hide with StackPane in fxml:
<StackPane fx:id="section_to_hide_container" prefHeight="200.0" prefWidth="200.0" alignment="CENTER_LEFT">
<AnchorPane fx:id="section_to_hide" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: red;" >
<children>
<Label fx:id="hide_btn1" layoutX="14.0" layoutY="54.0" onMouseClicked="#hide"
text="Hide me">
<font>
<Font name="Prompt-Black" size="24.0"/>
</font>
</Label>
</children>
</AnchorPane>
</StackPane>
And you controller code will change as below:
#FXML
private StackPane section_to_hide_container;
#FXML
private void hide() {
double width = section_to_hide_container.getWidth();
// Create a clip and apply on the pane to hide
Rectangle clip = new Rectangle();
clip.setWidth(width);
clip.heightProperty().bind(section_to_hide.heightProperty());
section_to_hide.setClip(clip);
Animation animation = new Timeline(
new KeyFrame(Duration.millis(300),
new KeyValue(section_to_hide_container.prefWidthProperty(), 0),
new KeyValue(section_to_hide.translateXProperty(), -width),
new KeyValue(clip.translateXProperty(), width)
)
);
animation.setOnFinished(e -> {
// Removing the clips
clip.heightProperty().unbind();
section_to_hide.setClip(null);
//Hiding the panes at the end
section_to_hide_container.setVisible(false);
section_to_hide.setVisible(false);
});
animation.play();
}
#FXML
public void initialize() {
// Binding min/max to pref, to not allow the panes width change.
section_to_hide_container.minWidthProperty().bind(section_to_hide_container.prefWidthProperty());
section_to_hide_container.maxWidthProperty().bind(section_to_hide_container.prefWidthProperty());
section_to_hide.minWidthProperty().bind(section_to_hide.prefWidthProperty());
section_to_hide.maxWidthProperty().bind(section_to_hide.prefWidthProperty());
}
I wrote a detailed blog about this particular feature. Please do check to get more inputs on this. Also please note that this blog was written almost 10 years back with my knowledge at that time. So some code and wordings may not be relevant now :)
As #James_D noticed in the comments, you should check if an AchorPane is the right container for your needs. For the specific example, a StackPane may be better suited.
If you need to use an AchorPane, one way to hide the pane completely is by applying .setManaged(false) to the pane at the end of the animation:
animation.setOnFinished(e -> section_to_hide.setManaged(false));
As #Slaw noticed in the comments, you can also use a clip on the AnchorPane:
Rectangle clip = new Rectangle();
clip.widthProperty().bind(section_to_hide.widthProperty());
clip.heightProperty().bind(section_to_hide.heightProperty());
section_to_hide.setClip(clip);

I have chosen a Pane, but I would like to set the alignment, is it possible?

I'm doing a project with javafx. As cointainer I choose the Pane, maybe the worst decision! Is it possible to center the text, using the lenght of the sentence which will be loaded according to the size of the panel?
I'm actually showing the code that i wrote. As you can see, the GeneralQuestion lenght will be various, the question has not a min or max character. How can i center it according to the size of the panel?
This is how does it look like:
This is the output, as you can see it isn't centered. I know there's the VBox or other type of panel but i've already chosen this one and before changing everything i would like to know if there's any way to make it look better!
This is the controller:
public void nextQuest(ActionEvent e) throws IOException
{
secondaryImg.setVisible(false);
nextQuestionButton.setVisible(false);
getQuestion(cont);
cont++;
}
public void getQuestion(int cont) throws FileNotFoundException,
IOException
{
FileManager f = new FileManager();
numQuest();
QuestionManager m = new QuestionManager(f.loadQuestion());
GeneralQuestion.setText(m.getAllQuestion().get(cont).getBody());
answ=m.getAllQuestion().get(cont).getAnswer();
if(m.getAllQuestion().get(cont).getType().equals("normale")||m.getAllQuestion().get(cont).getType().equals("perditutto"))
answS.setVisible(true);
else if(m.getAllQuestion().get(cont).getType().equals("verofalso")){
RbVero.setVisible(true);
RbFalso.setVisible(true);
}
}
This is (a part of) the FXML:
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #b8ffe0; -fx-border-width: 8px; -fx-border-color: #0e3d73;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="guiz.controller.GameScreenController">
<children>
<Label id="numquest" fx:id="numquest" layoutX="286.0" layoutY="35.0" text="Label">
<font>
<Font size="15.0" />
</font></Label>
<Label fx:id="GeneralQuestion" layoutX="225.0" layoutY="123.0" style="-fx-font-weight: 800;" text="Quest">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</Pane>
In JavaFX you achieve a desired layout by nesting various layout panes inside each other. Pane is indeed a bad descision because a Pane just does not do any layout at all. In your case a VBox would indeed be a better alternative which you might put into a BorderPane.

JavaFX TextArea does not append text properly

I'm trying to build a simple calculator using TDD, so I have a TextArea that is right aligned to display the results and 20 buttons. Everytime one of the digit buttons are pressed it just appends the digit to the TextArea, the same thing was supposed to happen when I press the dot button. But instead it places the dot as the first character in the TextArea, only when I press a new digit the dot goes to its proper place.
So for example, if I press 9 and then 8 the TextArea shows "98", now if the dot is pressed the result will be ".98", finally if 7 is pressed the result is "98.7".
The expected result when I pressed the dot button should have been "98.".
I created a Minimal, Complete, and Verifiable example bellow
FXMLDocument.fxml:
<AnchorPane id="AnchorPane" prefHeight="175.0" prefWidth="256.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="mcv.FXMLDocumentController">
<children>
<Button fx:id="buttonNine" layoutX="14.0" layoutY="100.0" onAction="#handleButtonNine" prefHeight="60.0" prefWidth="69.0" text="9">
<font>
<Font size="24.0" />
</font></Button>
<TextArea fx:id="textArea" editable="false" focusTraversable="false" layoutX="15.0" layoutY="14.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" nodeOrientation="RIGHT_TO_LEFT" prefHeight="66.0" prefWidth="221.0" wrapText="true">
<font>
<Font size="35.0" />
</font>
</TextArea>
<Button fx:id="buttonDot" layoutX="169.0" layoutY="100.0" mnemonicParsing="false" onAction="#handleButtonDot" prefHeight="60.0" prefWidth="69.0" text=".">
<font>
<Font size="24.0" />
</font>
</Button>
</children>
</AnchorPane>
FXMLDocumentController.java:
#FXML
private TextArea textArea;
#Override
public void initialize(URL location, ResourceBundle resources) {
}
private void appenToResult(String ch){
textArea.appendText(ch);
}
#FXML
void handleButtonDot(ActionEvent event) {
appenToResult(".");
}
#FXML
void handleButtonNine(ActionEvent event) {
appenToResult("9");
}
As you can see the code is very simple. It should be working, my only clue is that this is a bug with the TextArea when the Node Orientation is set to RIGHT_TO_LEFT. If node orientation is set to INHERIT or LEFT_TO_RIGHT it works as expected. Thank you.

javaFX :: popup window launches, but controller doesn't...?

I am writing an app where I need the same custom popup window to appear when different buttons are clicked. Right now the popup is just a simple "Are You Sure? OK/Cancel" window, but later it will expand to include more customized features... so I can't use the quickie Dialog built-ins.
Here's the weird thing. When Button X is pushed, the popUp (defined in FXML) launches just fine, but my controller class doesn't seem to be running. I didn't think that you could do that. What I can't figure out is why the controller isn't running. I would have thought the app would crash if the controller wasn't working.
Here' the code a button will call to launch the popup:
private void popUpLaunch(Button caller){
Stage popUpStage = new Stage();
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("popUp1.fxml"));
popUpStage.setScene(new Scene(root));
popUpStage.initModality(Modality.APPLICATION_MODAL); // popup
popUpStage.initOwner(caller.getScene().getWindow());
popUpStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
All of that works great. Here's the complete FXML, /src/sl/view/popUp1.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="130.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sl.view.PopUp1Controller">
<children>
<Text fx:id="popUpMessageText" layoutX="14.0" layoutY="14.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Message Goes Here" textAlignment="CENTER" wrappingWidth="577.6708984375" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<font>
<Font size="38.0" />
</font>
</Text>
<Button fx:id="btnPopUpOK" layoutX="126.0" layoutY="68.0" mnemonicParsing="false" prefHeight="31.0" prefWidth="157.0" text="OK" />
<Button fx:id="btnPopUpCancel" layoutX="286.0" layoutY="68.0" mnemonicParsing="false" prefHeight="31.0" prefWidth="169.0" text="Cancel" />
</children>
</AnchorPane>
The window loads just fine. And finally, here's the complete controller, /src/sl/view/PopUp1Controller.java:
package sl.view;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class PopUp1Controller {
#FXML Text popUpMessageText;
#FXML Button btnPopUpOK;
#FXML Button btnPopUpCancel;
Stage stage;
public void start(Stage stage) throws Exception{
System.out.println("Popup controller launched!"); // never reach this... so the controller is not launching???
this.stage = stage;
popUpMessageText.setText("Interesting message here!");
btnPopUpOK.setOnAction(event -> {
System.out.println("You cliced OK...");
});
btnPopUpCancel.setOnAction(event -> {
System.out.println("You cliced Cancel");
stage.close();
});
}
}
Some thoughts...
I used SceneBuilder to generate the FXML. When I assigned the Controller Class for that AnchorPane, I picked "sl.view.PopUp1Controller" from the drop-down menu. So I'm pretty sure that's right.
Also: I've looked through the other "JavaFX Popup" posts, but I don't see one that specifically addresses my issue. A lot of post are like the below post which are basically, "Why not use these other popup options rather than re-invent the wheel?" e.g.:
JavaFX 2 custom popup pane
In my case, I do want to reinvent the wheel, because I need my popups to carry more-then-usual functionality, they will not be simple dialog boxes.
If you want code in a controller to execute when the controller is initialized, put it in the initialize() method (see the documentation):
public class PopUp1Controller {
#FXML Text popUpMessageText;
#FXML Button btnPopUpOK;
#FXML Button btnPopUpCancel;
public void initialize() {
System.out.println("Popup controller launched!"); // never reach this... so the controller is not launching???
popUpMessageText.setText("Interesting message here!");
btnPopUpOK.setOnAction(event -> {
System.out.println("You cliced OK...");
});
btnPopUpCancel.setOnAction(event -> {
System.out.println("You cliced Cancel");
btnPopupCancel.getScene().getWindow().hide();
});
}
}

JavaFX controller injection does not work

I have two fxml files. I connect them with an include statement:
The "main" fxmlfile looks like that:
<?import javafx.geometry.*?>
// ...
<BorderPane prefHeight="962" prefWidth="1280" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MyMainController">
<center>
<SplitPane dividerPositions="0.63" BorderPane.alignment="CENTER">
<items>
<fx:include source="AnotherFile.fxml" />
// ...
</items>
</SplitPane>
</center>
<top>
// ...
</top>
</BorderPane>
And the second one (= "AnotherFile.fxml") like that:
<?import java.lang.*?>
// ...
<SplitPane dividerPositions="0.15" orientation="VERTICAL" prefHeight="400.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<items>
// ...
<Label fx:id="oneOfMyLabels" text="myText" GridPane.columnIndex="2" GridPane.rowIndex="1" />
</items>
</SplitPane>
Now, I am using injections in the "main"-controller application.MyMainController:
#FXML
private Label oneOfMyLabels;
If I run the controller I get a java.lang.NullPointerException exception, respectively a java.lang.reflect.InvocationTargetException one. In debugging mode I found out, that the injected Label is null!
Now, my question:
Can't reach the MyMainController from the "main fxml file" the components of the included fxml file?? Do I have to use an own controller on each fxml file, if it is included or not?!
Thanks for your help!!
You need to have a different controller for each FXML file, and the fx:id-annotated elements of each file will be injected into the corresponding controller instance.
When you have included FXML files, you can inject the controller for the included file into the controller for the including file, by setting an fx:id attribute on the fx:include element:
"main" fxml file:
<?import javafx.geometry.*?>
// ...
<BorderPane prefHeight="962" prefWidth="1280" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MyMainController">
<center>
<SplitPane dividerPositions="0.63" BorderPane.alignment="CENTER">
<items>
<fx:include fx:id="another" source="AnotherFile.fxml" />
// ...
</items>
</SplitPane>
</center>
<top>
// ...
</top>
</BorderPane>
and in the "main controller":
public class MyMainController {
#FXML
private AnotherController anotherController ;
// ...
}
(the rule being that the field name is the value of the fx:id attribute with "Controller" appended). Here AnotherController is the controller class for AnotherFile.fxml.
Now you can, for example, expose the data you need to access in the "included controller":
public class AnotherController {
#FXML
private Label oneOfMyLabels ;
public StringProperty textProperty() {
return oneOfMyLabels.textProperty();
}
public final String getText() {
return textProperty().get();
}
public final setText(String text) {
textProperty().set(text);
}
// ...
}
and then your main controller can do things like
anotherController.setText(...);
which will of course update the label. This preserves encapsulation, so that if you choose to use another control instead of a label, those changes do not have to propagate outside of the immediate controller.

Resources