Reputation: 43
I'm using JavaFX and SceneBuilder to create the front-end for an educational text processing application. For this part of the application, The user enters two words and then the edit distance between them is calculated. A List of strings which is the word path is returned. The specifics aren't important at this point, but I am working on how to display the results. I'd like them to be centered no matter the length. I'll also have to deal with cases where the width of the string becomes too large for the dialog window, so input on that would be great too. Right now I am just using a single AnchorPane and Labels which I'm pretty sure is too simplistic, and I'd like some input on a better way to do this.
EDIT: Also there is an issue where when the dialog firsts pops up the labels aren't present then once clicked they come up.
public void setResult(List<String> result) {
numStepsLabel.setText(Integer.toString(result.size()-2));
//TODO -- work on layout
String str = buildResultString(result);
pathLabel.setText(str);
}
private String buildResultString(List<String> result) {
StringBuilder sb = new StringBuilder();
for(String str : result) {
sb.append(str);
sb.append(" -> ");
}
// delete last ->
sb.delete(sb.length() - 4, sb.length() -1);
return sb.toString();
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="200.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.EDResultController">
<!-- TODO Add Nodes -->
<children>
<Label layoutX="120.0" layoutY="45.0" text="Word Path:" />
<Label fx:id="pathLabel" layoutX="136.0" layoutY="74.0" text="Label" textAlignment="CENTER" />
<Label layoutX="59.0" layoutY="123.0" text="Number of steps:" />
<Label fx:id="numStepsLabel" layoutX="180.0" layoutY="123.0" text="Label" />
<Button layoutX="132.0" layoutY="159.0" mnemonicParsing="false" text="OK" />
</children>
</AnchorPane>
Upvotes: 0
Views: 484
Reputation: 49195
You set the layout x and y properties of the nodes in AnchorPane
. It results to give fixed positions to them and they cannot be centered. Alternative approach is to use combination of VBox
and HBox
es. And not to set x-y layout properties, since VBox/HBox ignore them and do the layout automatically. For info about different layouts read Working With Layouts in JavaFX.
<VBox prefHeight="200.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2"
fx:controller="application.EDResultController" alignment="CENTER" fillWidth="true" spacing="10" >
<!-- TODO Add Nodes -->
<children>
<Label text="Word Path:" />
<Label fx:id="pathLabel" text="Label" textAlignment="CENTER" />
<HBox alignment="CENTER" spacing="10" >
<children>
<Label text="Number of steps:" />
<Label fx:id="numStepsLabel" text="Label" />
</children>
</HBox>
<Button mnemonicParsing="false" text="OK" />
</children>
</VBox>
Also the buildResultString() can be simplified as
private String buildResultString( List<String> result )
{
return String.join( " -> ", result);
}
Upvotes: 1
Reputation: 1135
Not the solution, but you might want to check for an empty list in buildResultString.
// delete last ->
if (sb.length() > 0)
sb.delete(sb.length() - 4, sb.length() - 1);
Upvotes: 0