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.
Related
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);
very new to programming. I am learning java and javaFX.
I have tried to make a login scene and a home scene and currently have a "login" button that invokes a method in the controller -loginButton()- that switches between the two scenes. The part I am unsure on is how to set up this method to retrieve the strings from the username and password text/password fields.
I am not interested just yet in setting this up with a database. what I want to go for is something like this (rubbish login details for the purpose of example, I would never actually use login info this bad):
public void loginButton(javafx.event.ActionEvent actionEvent) throws IOException {
if(textFromLoginField=="admin"){
if(textFromPasswordField=="Password123"){
//run code to change scene
}
}
}
I know how to switch scene but I dont know how to get the login info (shown as textFromLoginField and textFromPasswordField in the example above.
How I am switching scenes
Node node=(Node) actionEvent.getSource();
Stage stage=(Stage) node.getScene().getWindow();
stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("homeScene.fxml")),750,500));
stage.show();
My FXML text:
VBox maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="159.0" prefWidth="157.0" spacing="10.0" style="-fx-background-color: Black;" HBox.hgrow="NEVER">
<HBox.margin>
<Insets bottom="60.0" top="60.0" />
</HBox.margin>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<children>
<Label text="LOGIN:" textFill="WHITE" />
<TextField fx:id="loginTextField" promptText="Username" />
<PasswordField fx:id="passwordTextField" promptText="Password" />
<HBox alignment="TOP_RIGHT" prefHeight="21.0" prefWidth="137.0" spacing="10.0">
<children>
<Button mnemonicParsing="false" fx:id="loginbutton" onAction="#loginButton" style="-fx-background-color: Blue;" text="Login" textAlignment="CENTER" textFill="WHITE">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
<Button mnemonicParsing="false" fx:id="helpbutton" onAction="#helpButton" style="-fx-background-color: Orange;" text="Help" textAlignment="CENTER" textFill="WHITE">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
</children>
</HBox>
</children>
</VBox>
Any help greatly appreciated :)
-Also, if I have used any technical vocab incorrectly please let me know. I am new to this. thanks
"==" doesn't work with Strings. Use textFromLoginField.equals();. Check out the string functions.
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.
I have a problem when my FXML are initialized and theirs values are listen by listeners.
So my main window is a BorderPane
TOP = menu.fxml
CENTER = center1.fxml
BOTTOM = footer.fxml
When I click on one of buttons on center1Controller the CENTER fxml should changed to center2.fxml.
In center2.fxml I have a Pane that should be Anchor.anchorLeft=60 and anchorTop = anchorBottom
I was created an listener :
public void addTopOffsetListener() {
this.logonWindowPane.heightProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
double y = (newValue.doubleValue() - logonForm.getHeight()) / 2;
logonForm.setLayoutY(y);
}
});
}
But one my screen is reload the logonForm is in bad place. When I debug moment the initialization getHeight() returns 0.
It is placed in correct place after resizing window.
I was trying fixed size of this element on FXML and on CSS.
Actually I was find an workaround. When I initialize a screen2Controller a I have added this line
public void initialize(URL url, ResourceBundle rb) {
this.logonForm.resize(300, 400);
}
There is my Screen2 fxml :
<AnchorPane id="logonWindowPane" fx:id="logonWindowPane" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="genealogytree.application.fxmlcontrollers.LogonWindowController">
<stylesheets>
<URL value="#../styles/application_logon.css" />
</stylesheets>
<children>
<Pane id="logonForm" fx:id="logonForm" prefHeight="400" prefWidth="300" minWidth="300" minHeight="400" styleClass="projectPane" AnchorPane.rightAnchor="60.0" />
</children>
</AnchorPane>
And the FXML of element there I put in logonForm :
<Pane styleClass="logonFXForm" prefHeight="400" prefWidth="300" minWidth="300" minHeight="400" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="genealogytree.application.fxmlcontrollers.LogonFormController">
<stylesheets>
<URL value="#../styles/application_logon.css" />
</stylesheets>
<children>
<JFXPasswordField focusColor="#1f7f6a" layoutX="38.0" layoutY="133.0" maxWidth="224.0" minWidth="224.0" prefHeight="48.0" prefWidth="224.0" promptText="Password" unFocusColor="#253930">
<font>
<Font size="14.0" />
</font>
</JFXPasswordField>
<JFXTextField focusColor="#1f7f6a" layoutX="38.0" layoutY="54.0" maxWidth="224.0" minWidth="224.0" prefHeight="48.0" prefWidth="224.0" promptText="Login" unFocusColor="#253930">
<font>
<Font size="14.0" />
</font>
</JFXTextField>
<JFXButton buttonType="RAISED" graphicTextGap="8.0" layoutX="61.0" layoutY="224.0" prefHeight="48.0" prefWidth="178.0" ripplerFill="#32ae8f" styleClass="jfxButton" text="CONNECT" textAlignment="CENTER">
<font>
<Font name="System Bold" size="18.0" />
</font>
</JFXButton>
<Hyperlink layoutX="38.0" layoutY="305.0" prefHeight="25.0" prefWidth="250.0" text="Do not have an Account ? " />
<Hyperlink layoutX="38.0" layoutY="340.0" prefHeight="25.0" prefWidth="250.0" text="Forgotten Password ? " />
</children>
</Pane>
But there is another method to resolve my problem in FXML or CSS only ?
I'm using Accelerator in my application, and when I hit the F1 key it should open a dialog just once, but when I close the dialog it appears again. The runnable is been called twice.
Does anyone know what it could be? Is it a bug? I'm using a Macbook to develop.
This is my controller:
public class Screen1Controller {
#FXML
private TextField txtCode;
#FXML
private TableView tblProducts;
#FXML
private Button buttonAskUserDecision;
#FXML
public void initialize () {
Platform.runLater(new Runnable() {
#Override
public void run() {
initializeKeyCombinations();
}
});
}
#FXML
private void executePrint (ActionEvent event) {
System.out.println("ExecutePrint");
}
public void initializeKeyCombinations() {
buttonAskUserDecision.getScene().getAccelerators().clear();
buttonAskUserDecision.getScene().getAccelerators().put(
new KeyCodeCombination(KeyCode.F1),
new Runnable() {
#Override
public void run() {
System.out.println("Executed F1");
}
}
);
}
}
This is my FXML:
<StackPane prefHeight="600.0" prefWidth="800.0" stylesheets="#../../../css/main.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Screen1Controller">
<children>
<BorderPane prefHeight="200.0" prefWidth="200.0">
<center>
<ScrollPane fitToHeight="true" fitToWidth="true" BorderPane.alignment="CENTER">
<content>
<VBox alignment="TOP_CENTER">
<children>
<GridPane hgap="10.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="20.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="20.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="30.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="30.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints fillHeight="false" minHeight="30.0" vgrow="NEVER" />
</rowConstraints>
<children>
<TextField fx:id="txtCode" GridPane.columnSpan="2" />
</children>
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</VBox.margin>
</GridPane>
<TableView fx:id="tblProducts" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn prefWidth="75.0" text="C1" />
<TableColumn prefWidth="75.0" text="C2" />
</columns>
</TableView>
</children>
</VBox>
</content>
</ScrollPane>
</center>
<bottom>
<ToolBar maxHeight="80.0" minHeight="80.0" prefHeight="80.0" styleClass="default-skin-pdv-toolbar" stylesheets="#../../../css/main.css">
<items>
<Button fx:id="buttonAskUserDecision" layoutX="10.0" layoutY="20.0" mnemonicParsing="false" onAction="#executePrint" prefHeight="60.0" prefWidth="69.0" styleClass="default-skin-pdv-button">
</Button>
</items>
<padding>
<Insets left="10.0" />
</padding>
</ToolBar>
</bottom>
</BorderPane>
</children>
Inside this screen I have a textfield, a tableview and a toolbar with a button.
I did some new tests. Every time I give focus on my tableview and hit the F1 button, the execution is incremented by 1. For instance, if I click on the tableview and hit F1, it prints "Executed F1" just once on the console and the focus go back to the textfield, if i give focus to the tableview again and hit F1, It prints two times the text on the console and give focus back to the textfield, and if I give focus to the tableview for the third time and press the F1 again it prints three more lines on the console, and so on.
It only happens when I use the F1 button.. When I click the button with the mouse it prints only once.
I removed the TableView from the screen and it stopped duplicating the executions.
The problem is: I need this tableView in my screen. What could be happening here?
That's my Java version details:
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
The problem was in a component (Dialog) from ControlsFX library.
I created a new Project and included a textfield, viewTable and a button. The duplication didn't happened.
When I included the ControlsFX Dialog, the started to happen. When I click the button, with the mouse, the dialog opens just once. But when I use the accelerator to fire the button, the dialog opens twice.
Edited:
I created my own dialog, but it happened again. Instead of using the stage.showAndWait() I tried the stage.show() and it stopped to open twice.
I searched the stackoverflow and found this answer https://stackoverflow.com/a/22463857/916804 ... When I tried to wrap the call to the Dialog with the Platform.runLater the execution happened only once.