Managing a ComboBox items in JavaFX - java

I'm populating a ComboBox using Text objects. Using Text objects rather than Strings allows me to add an id value that I can use in my program and later exploit when I decide to internationalize the UI. Anyway, here is what I'm doing:
Main class:
public class MainApp extends Application {
private Stage primaryStage;
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
try {
AnchorPane paneMain = (AnchorPane) FXMLLoader.load(getClass().getResource("Test.fxml"));
Scene scene = new Scene(paneMain);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Controller:
public class Test implements Initializable{
#FXML
private AnchorPane anchorPane;
#FXML
private ComboBox<Text> comboTime;
private Text days;
private Text hours;
private Text minutes;
private int timeMultiplier;
public Test(){
days = new Text("Days");
days.setId("86400000");
hours = new Text("Hours");
hours.setId("3600000");
minutes = new Text("Minutes");
minutes.setId("3600000");
timeMultiplier = 0;
}
#Override
public void initialize(URL location, ResourceBundle resources) {
comboTime.getItems().removeAll(comboTime.getItems());
comboTime.getItems().addAll(days, hours, minutes);
comboTime.getSelectionModel().select(hours);
}
#FXML
private void setTimeMultiplier(){
Text text = comboTime.getSelectionModel().getSelectedItem();
timeMultiplier = Integer.valueOf(text.getId());
}
}
Test.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" fx:id="anchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.99990000000253" prefWidth="94.99990000000253" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.spacitron.backupp.ui.controllers.Test">
<children>
<HBox id="HBox" alignment="CENTER" layoutX="41.0" layoutY="224.0" prefWidth="216.0" spacing="5.0" />
<ComboBox id="comboInterval" fx:id="comboTime" editable="false" layoutX="14.0" layoutY="22.0" onAction="#setTimeMultiplier">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Item 1" />
<String fx:value="Item 2" />
<String fx:value="Item 3" />
</FXCollections>
</items>
</ComboBox>
</children>
</AnchorPane>
Now, this works just fine. The problem however is that when I select an item the text on that item goes blank like so:
And if I select another one, that disappears too:
I can still select the items, but they're just not there to be seen. Is this a bug and if so is there a way around it?
EDITED to provide MCVE

You shouldn't be putting the Text nodes into the ComboBox ... please see this question that will help you: setButtonCell for ComboBox

Related

Image FXML is not opening in JavaFX

While I was trying to load two different scenes, it doesn't open the image FXML, basically I want the image FXML to be opened first followed by welcome screen. Some how Image scene is not showing.
Main.java
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception {
System.out.println("im in main");
Parent root = FXMLLoader.load(getClass().getResource("ImageScreen.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("test");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
ImageScreen.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>
<StackPane fx:id="imagepane" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.ImageScreenController">
<children>
<ImageView fitHeight="438.0" fitWidth="603.0" pickOnBounds="true"
preserveRatio="true">
<image>
<Image url="#bamboo-fountain-and-zen-stone.jpg" />
</image>
</ImageView>
<Pane prefHeight="200.0" prefWidth="200.0" />
</children>
ImageScreenController
public class ImageScreenController implements Initializable {
#FXML
private StackPane imagepane;
public static AnchorPane welcomepane;
#Override
public void initialize(URL url, ResourceBundle rb) {
AnchorPane pane;
try {
System.out.println("im in controller");
pane = FXMLLoader.load(getClass().getResource("WelcomeFXMLDoc.fxml"));
imagepane.getChildren().setAll(pane);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
WelcomeFXMLDoc.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane fx:id="welcomepane" maxHeight="-Infinity"
maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.102"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.WelcomeFXMLController">
<children>
<Text layoutX="212.0" layoutY="100.0" strokeType="OUTSIDE"
strokeWidth="0.0" text="Welcome" wrappingWidth="176.13671875">
<font>
<Font size="35.0" />
</font>
</Text>
</children>
WelcomeFXMLController.java
public class WelcomeFXMLController implements Initializable {
/**
* Initializes the controller class.
*
*/
#FXML
private AnchorPane welcomepane;
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
In ImageScreenController you use imagepane.getChildren().setAll(pane). setAll() clear collection and then add new elements. In this case you remove ImageView from scene graph and show only welcomepane.
Use imagepane.getChildren().add(pane); instead and then remove welcomepane when you don't need is anymore.
EDIT
Based on your comment.
Use PauseTransition to wait for WelcomeFXMLController:
#Override
public void initialize(URL url, ResourceBundle rb) {
PauseTransition pt = new PauseTransition(Duration.seconds(10));
pt.setOnFinished(e -> {
AnchorPane pane;
try {
System.out.println("im in controller");
pane = FXMLLoader.load(getClass().getResource("WelcomeFXMLDoc.fxml"));
imagepane.getChildren().setAll(pane);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
pt.play();
}

JavaFX / FXML UI does not look like it should

I have a UI I have made using Netbeans and Scene builder using Java FX / FXML. I have never had a problem with it before but for this project for some reason in Scene building my UI would look one way, even the windows preview would look identical to it, but when running it in netbeans it would look messed up. In netbeans, all I am doing is loading and calling it. I have no idea on what the issue can be and nothing is helping on Google.
working screen shot on scene builder and editing and netbeans
http://imgur.com/laZYjck
compile and run
http://imgur.com/NOMVBZe
main.java
public class main
{
public static void main(String[] args)
{
View view = new View();
view.launch();
}
}
view.java
public class View extends Application
{
#FXML
private Button login;
#Override
public void start(Stage primaryStage)
{
Parent login_page = null;
try {
login_page = FXMLLoader.load(getClass().getResource("FXMLViewLogin.fxml"));
}
catch (IOException ex)
{
}
primaryStage.setTitle("Welcome!");
primaryStage.setScene(new Scene(login_page));
primaryStage.show();
}
#FXML
private void login(ActionEvent event)
{}
}
FXMLViewLogin.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="520.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.ViewControllerLogin">
<children>
<Button fx:id="button1" defaultButton="true" layoutX="74.0" layoutY="296.0" text="Sign Up" />
<Label layoutX="198.0" layoutY="153.0" text="Password" />
<Label layoutX="187.0" layoutY="35.0" text="User Name" />
<TextField fx:id="password" layoutX="74.0" layoutY="204.0" />
<TextField fx:id="username" layoutX="74.0" layoutY="80.0" />
<Button fx:id="button" layoutX="323.0" layoutY="296.0" onAction="#login" text="Log In" />
</children>
</Pane>

How do I show each character in TextArea when it is typed in TextField?

I am trying to show every character instantly into TextArea as and when it is typed into the TextField.
I've written some code as follows but it didn't work:
Application_Controler.java
public class Application_Controler {
#FXML
private TextField txt;
#FXML
private TextArea showTxt;
#FXML
void keyTyped(ActionEvent event) {
String text = txt.getText();
String oldText = showTxt.getText();
String nextText = oldText+text;
showTxt.setText(nextText);
}
}
Here is the .fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="imran.jfx.application.Application_Controler">
<children>
<AnchorPane layoutX="-17.0" layoutY="-14.0" prefHeight="461.0" prefWidth="454.0">
<children>
<TextField fx:id="txt" layoutX="122.0" layoutY="87.0" onKeyTyped="#keyTyped" prefHeight="55.0" prefWidth="229.0" />
<TextArea fx:id="showTxt" layoutX="56.0" layoutY="215.0" prefHeight="210.0" prefWidth="362.0" />
</children>
</AnchorPane>
</children>
</AnchorPane>
How should I do it?
You can bind the textProperty() of TextField to the textProperty() of TextArea.
textArea.textProperty().bind(textField.textProperty());
Try adding the following code inside the initialize() of your controller :
public class Application_Controler implements Initializable {
#FXML
private TextField txt;
#FXML
private TextArea showTxt;
#Override
public void initialize(URL location, ResourceBundle resources) {
showTxt.textProperty().bind(txt.textProperty());
}
}

How do i restart an application in JavaFx, built with FXML?

Say I have a window, with a button. And everytime that button is pressed I want the current window to be disposed and a new one to be shown.
In Swing, that was easy, but I can't find the syntax for it in JavaFx?
So in this case, with my example code, how do I do this?
public class Main extends Application {
Parent root;
Scene scene;
#Override
public void start(Stage primaryStage) throws Exception {
root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.setResizable(false);
primaryStage.show();
root.setOnMouseClicked((MouseEvent mouseEvent) -> {
if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
if (mouseEvent.getClickCount() == 2) {
Stage stage = new Stage();
stage.setScene(primaryStage.getScene());
stage.show();
primaryStage.close();
}
}
});
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Trying to create a new window: http://sv.tinypic.com/r/1jkq4w/8
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<VBox prefHeight="430.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="blackjack.FXMLDocumentController">
<children>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<Label fx:id="labelPlayerTotal" layoutX="510.0" layoutY="335.0" prefHeight="15.0" prefWidth="121.0" text="Playertotal:" />
<Label fx:id="labelDealerTotal" layoutX="510.0" layoutY="359.0" prefHeight="15.0" prefWidth="112.0" text="Dealertotal:" />
<TextArea fx:id="dealerArea" layoutY="29.0" prefHeight="159.0" prefWidth="503.0" />
<TextArea fx:id="playerArea" layoutY="244.0" prefHeight="159.0" prefWidth="503.0" />
<Button fx:id="stayButton" layoutX="512.0" layoutY="173.0" mnemonicParsing="false" onAction="#drawCardForDealer" prefHeight="25.0" prefWidth="72.0" text="STAY" />
<Button fx:id="hitButton" layoutX="512.0" layoutY="130.0" mnemonicParsing="false" onAction="#drawCardForPlayer" prefHeight="25.0" prefWidth="72.0" text="HIT" />
<Label fx:id="labelWinner" layoutX="104.0" layoutY="208.0" prefHeight="15.0" prefWidth="296.0" />
<MenuBar fx:id="helpBar">
<menus>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" onAction="#aboutApplication" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</AnchorPane>
</children>
</VBox>
You can just hide (i.e. dispose) the window by calling hide() or close() on it.
And you can just show a new window with exactly the same code you used before:
primaryStage.close();
Stage stage = new Stage();
root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
scene = new Scene(root);
stage.setScene(scene);
stage.sizeToScene();
stage.setResizable(false);
stage.show();
But this seems like way too much for what you want to achieve. Why not just replace the root of the existing scene, and use the existing stage?
try {
scene.setRoot(FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")));
} catch (Exception exc) {
exc.printStackTrace();
throw new RuntimeException(exc);
}
Note though that (either way you do this) you have now replaced the root of the scene; the new root does not have the same mouse handler associated with it. If you use the second method, you can just put the handler on the scene (which doesn't change) instead. Or, perhaps better, is that you can define the listener in the FXML and controller:
<VBox prefHeight="430.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="blackjack.FXMLDocumentController"
fx:id="root"
onMouseClicked="reload">
<!-- ... -->
</VBox>
And the controller:
package blackjack ;
public class FXMLDocumentController {
// ...
#FXML
private VBox root ;
// ...
#FXML
private void reload() throws Exception {
// Really, it would be way better here to reset the whole UI to its initial
// state, instead of reloading it from scratch. This should work as a
// quick hack though.
Scene scene = root.getScene();
scene.setRoot(FXMLLoader.load(Main.class.getResource("FXMLDocument.fmxl")));
}
// ...
}

JavaFX 2.0 FXML Child Windows

After much searching I found this question How to create a javafx 2.0 application MDI. What I really wanted to know is if I can create a pop-up window or child window to the main window using JavaFX components and Scene Builder to create the new window.
I ended up with this for a modal pop-up window:
In the Main class I wanted to save the primary stage to a field I can access from my primary controller class. So, I added a static variable Stage to it and this in the Main.Start() method:
primaryController.primaryStage = primaryStage;
This the method that a button in the primaryController uses:
public void OnBtnShowChild(ActionEvent event) {
MessageBoxController msgBox = new MessageBoxController();
try {
msgBox.showMessageBox(primaryStage);
} catch (Exception e) {
e.printStackTrace();
}
}
This is the MessageBoxController class that I created with help from Scene Builder. It has the basic layout of a standard pop-up box that can be used to display an Icon (ImageView), TextBox (for your message text), and two buttons (for YES/NO functionality). I am not sure yet how to have it communicate the results of what button was pressed back to the primaryController.
public class MessageBoxController implements Initializable {
#FXML
// fx:id="btnNo"
private Button btnNo; // Value injected by FXMLLoader
#FXML
// fx:id="btnYes"
private Button btnYes; // Value injected by FXMLLoader
#FXML
// fx:id="imgMessage"
private ImageView imgMessage; // Value injected by FXMLLoader
#FXML
// fx:id="txtMessage"
private TextField txtMessage; // Value injected by FXMLLoader
private Stage myParent;
private Stage messageBoxStage;
public void showMessageBox(Stage parentStage) {
this.myParent = parentStage;
try {
messageBoxStage = new Stage();
AnchorPane page = (AnchorPane) FXMLLoader.load(MessageBoxController.class.getResource("/MessageBox/MessageBoxFXML.fxml"));
Scene scene = new Scene(page);
messageBoxStage.setScene(scene);
messageBoxStage.setTitle("Message Box");
messageBoxStage.initOwner(this.myParent);
messageBoxStage.initModality(Modality.WINDOW_MODAL);
messageBoxStage.show();
} catch (Exception ex) {
System.out.println("Exception foundeth in showMessageBox");
ex.printStackTrace();
}
}
#Override
public void initialize(URL fxmlFileLocation, ResourceBundle arg1) {
txtMessage.setText("Howdy");
}
public void OnBtnYes(ActionEvent event) {
}
public void OnBtnNo(ActionEvent event) {
}
}
And finally, this is the FXML file I created in Scene Builder:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane2" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="172.0" prefWidth="524.0" xmlns:fx="http://javafx.com/fxml" fx:controller="MessageBox.MessageBoxController">
<children>
<VBox prefHeight="172.0" prefWidth="524.0" styleClass="vboxes" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<HBox alignment="CENTER" prefHeight="109.99990000000253" prefWidth="516.0" spacing="30.0">
<children>
<ImageView fx:id="imgMessage" fitHeight="110.0" fitWidth="146.66666666666666" pickOnBounds="true" preserveRatio="true" styleClass="null" />
<TextField fx:id="txtMessage" editable="false" prefHeight="47.0" prefWidth="325.0" />
</children>
<stylesheets>
<URL value="#MyCSS.css" />
</stylesheets>
</HBox>
<HBox alignment="CENTER" prefHeight="58.0" prefWidth="516.0" spacing="30.0">
<children>
<Button fx:id="btnYes" mnemonicParsing="false" onAction="#OnBtnYes" text="Button" />
<Button fx:id="btnNo" mnemonicParsing="false" onAction="#OnBtnNo" text="Button" />
</children>
</HBox>
</children>
<stylesheets>
<URL value="#MyCSS.css" />
</stylesheets>
</VBox>
</children>
<stylesheets>
<URL value="#MyCSS.css" />
</stylesheets>
</AnchorPane>
With this I can create a modal pop-up window, and I also want to create other child windows for displaying data in other ways using different controls. And, most importantly, I can use Scene Builder to create the layout.
What do you think? Is this a good way to do this until they add real support in Java 8 and JavaFX 8?
did you try wit the Group class? you can add diferent elements with fxml and controllers.
Group root= new Group();
AnchorPane frame=FXMLLoader.load(getClass().getResource("frame.fxml"));
AnchorPane content= FXMLLoader.load(getClass().getResource("principal.fxml"));
root.getChildren().add(window);
root.getChildren().add(frame);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

Categories