I'm trying to display playing cards in a FlowPane. I have a main layout and a nested layout. For some reason when I debug IntelliJ reports that all fields, on both controllers, annotated with #FXML are null.
Here's a shortened version of what I've got thus far. Full Code on GitHub:
MainWindow.fxml
<BorderPane fx:controller="controller.MainWindowController">
<center>
<fx:include fx:id="tableScene" source="TableScene.fxml"/>
</center>
</BorderPane>
MainWindowController.java
public class MainWindowController implements Initializable {
#FXML
MenuBar menuBar;
#FXML
Menu fileMenu;
[...] more fields
#Override
public void initialize(URL location, ResourceBundle resources) {
// nothing here in my code
}
}
TableScene.fxml
<AnchorPane fx:controller="controller.TableSceneController">
<children>
<FlowPane fx:id="dealerHandFlowPane"></FlowPane>
<FlowPane fx:id="playerHandFlowPane"></FlowPane>
</children>
</AnchorPane>
TableSceneController
public class TableSceneController implements Initializable {
#FXML
private FlowPane dealerHandFlowPane;
#FXML
private FlowPane playerHandFlowPane;
public void displayInitialHand(Player player) {
var cards = new ArrayList<>(player.getHand().getCards());
for (BlackjackCard card : cards) {
if(player.getName().equals("Dealer")) {
dealerHandFlowPane.getChildren().add(new ImageView(getCardFace(card)));
} else {
playerHandFlowPane.getChildren().add(new ImageView(getCardFace(card)));
}
}
}
public void displayHand(Player player) {
var cards = new ArrayList<>(player.getHand().getCards());
}
public Image getCardFace(BlackjackCard card) {
return new Image("/images/cards/" + card.getRank().getLetter()
+ card.getSuit().getLetter() + ".png");
}
public Image getCardBack() {
String color[] = {"blue","red"};
String design = "123";
return new Image("/images/backs/" + color[0] + design.charAt(2));
}
#Override
public void initialize(URL location, ResourceBundle resources) {
// nothing here in my code either
}
}
BlackjackMain
public class BlackjackMain extends Application {
private final String MAIN_WINDOW_PATH = "/fxml/MainWindow.fxml";
private final String ICON_PATH = "/images/blackjack_icon.png";
private final String MAIN_STYLE_PATH = "/css/MainWindow.css";
private final String TABLE_STYLE_PATH = "/css/TableScene.css";
private final Image MAIN_ICON = new Image(getClass().getResourceAsStream(ICON_PATH));
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Blackjack");
// close the app gracefully when the 'X' is clicked
primaryStage.setOnCloseRequest(e -> Platform.exit());
primaryStage.centerOnScreen();
primaryStage.setResizable(false);
initializeMainWindow(primaryStage);
primaryStage.getIcons().add(MAIN_ICON);
primaryStage.show();
primaryStage.toFront();
initializeGame();
}
public void initializeMainWindow(Stage primaryStage) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(MAIN_WINDOW_PATH));
try {
Parent mainWindow = loader.load();
Scene scene = new Scene(mainWindow,600,600);
scene.getStylesheets().add(TABLE_STYLE_PATH);
primaryStage.setScene(scene);
} catch (IOException e) {
System.err.println("There was a problem loading /fxml/MainWindow.fxml");
e.printStackTrace();
}
}
public void initializeGame() {
var tableSceneController = new TableSceneController();
var mainWindowController = new MainWindowController();
Dealer dealer = new Dealer();
List<Player> allPlayers = new ArrayList<>();
var playerName = tableSceneController.getPlayerName();
allPlayers.add(new BlackjackPlayer(playerName));
BlackjackGame game = new BlackjackGame(dealer, allPlayers,
mainWindowController, tableSceneController);
game.playGame();
}
}
BlackjackGame.java
public class BlackjackGame implements BlackjackGameRules {
private List<Player> playerList;
private Deck deck;
private Shoe shoe;
private final TableSceneController tableSceneController;
private final MainWindowController mainWindowController;
public BlackjackGame(Dealer dealer, List<Player> players,
final MainWindowController mainWindowController,
final TableSceneController tableSceneController) {
Objects.requireNonNull(dealer,
"You must provide a dealer to begin the game.");
Objects.requireNonNull(players,
"You must provide a list of players to begin the game.");
playerList = new ArrayList<>();
this.tableSceneController = tableSceneController;
this.mainWindowController = mainWindowController;
// add dealer first for easier future access
playerList.add(dealer);
playerList.addAll(players);
deck = new Deck(BlackjackGameRules.NUMBER_OF_DECKS);
// place the shuffled deck in the shoe
shoe = new Shoe(deck.getDeck());
}
public void dealInitialCards() {
for (Player player : playerList) {
player.getHand().addCard(shoe.dealCard());
player.getHand().addCard(shoe.dealCard());
}
}
public boolean hasValidNumberOfPlayers() {
// this number includes the dealer
var numPlayers = playerList.size();
return numPlayers >= BlackjackGameRules.MIN_PLAYERS &&
numPlayers <= BlackjackGameRules.MAX_PLAYERS;
}
public List<Player> getPlayers() {
return new ArrayList<>(playerList);
}
public Shoe getShoe() {
return shoe;
}
public void playGame() {
dealInitialCards();
for(Player player: playerList) {
tableSceneController.displayInitialHand(player);
}
}
}
I get a NullPointerException on displayIntitialHand in TableSceneController. Here's the brief stacktrace:
Caused by: java.lang.NullPointerException
at blackjack.controller.TableSceneController.displayInitialHand(TableSceneController.java:35)
at blackjack.model.BlackjackGame.playGame(BlackjackGame.java:139)
at blackjack.controller.BlackjackMain.initializeGame(BlackjackMain.java:70)
at blackjack.controller.BlackjackMain.start(BlackjackMain.java:44)
For the life of me I cannot figure this one out. Where have I gone wrong? I have double checked that I've set the names of the controllers in the fx:controller attribues in the *.fxml files. I have also double checked that I have the fx:id attributes correct in the components and that they also match the #FXML annotations in the controller correctly.
My understanding of the process of JavaFX is:
that load() is supposed to load the *.fxml file
instantiate the controller (specified by the fx:controller attribute in the .fxml file)
Calls the no-arg constructor on the controller
Sets the #FXML values (by injection)
Registers any event handlers
Calls initialize on each controller
Is the problem with my nested fxml files? If this was the case I would think that the #FXML fields in MainWindowController.java would not also be null. I'm s truggling to figure this out. I could use another set of eyes and someone smarter than myself.
Thanks in advance.
Took a while to figure out, but when you create the controllers for your scenes, within initializeGame() you do:
var tableSceneController = new TableSceneController();
var mainWindowController = new MainWindowController();
What this means is you are creating a new instance of the controller, not the instance that is created when you load your FXML files within initializeMainWindow.
To remedy this, I'd suggest creating a class variable to hold each of your controllers, and then assign them when you load the FXML files.
So, in BlackJackMain.java, declare class variables
private TableSceneController tableSceneController;
private MainWindowController mainWindowController;
then when you load them, I can see you load the main window in initializeMainWindow, so add
mainWindowController = loader.getController();
to the try block, just after the loader.load line.
This resolves your null pointers for this scene, but I cannot figure out where or if you load the table scene FXML, and thus you don't have an instance of the controller to pass into your method. If you do load the file, apply the same logic to it to get an instance of that controller too.
Related
So I have an integer number called "size" saved to a controller class called SettingsStageController.java and I want that variable to be accessed through my other controller class file called GameStageController.java but I can't seem to find out how.
SettingsStageController.java
/* has the int size variable stored in this file */
int size = 5;
public void startGame(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("gameStage.fxml"));
root = loader.load();
stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); // ti ston poutso
scene = new Scene(root);
stage.setTitle("DnB: " + Integer.toString(size) + "x" + Integer.toString(size) + " Game");
stage.setScene(scene);
stage.show();
GameStageController gameStageController = loader.getController();
gameStageController.showPane();
}
GameStageController.java
public class GameStageController implements Initializable {
#FXML
Text testText;
#FXML
AnchorPane twoXtwoPane;
#FXML
AnchorPane threeXthreePane;
#FXML
AnchorPane fourXfourPane;
#FXML
AnchorPane fiveXfivePane;
public void showPane() {
switch (/* I WANT TO PUT THE "SIZE" NUMBER HERE" */) {
case 2:
twoXtwoPane.setDisable(false);
twoXtwoPane.setVisible(true);
break;
case 3:
threeXthreePane.setDisable(false);
threeXthreePane.setVisible(true);
break;
case 4:
fourXfourPane.setDisable(false);
fourXfourPane.setVisible(true);
break;
case 5:
fiveXfivePane.setDisable(false);
fiveXfivePane.setVisible(true);
break;
default:
twoXtwoPane.setDisable(false);
twoXtwoPane.setVisible(true);
break;
}
}
}
If a method needs data to perform its functionality, then that data should be a parameter to the method. You should do:
public class GameStageController implements Initializable {
// ...
public void showPane(int size) {
switch (size) {
// ...
}
}
}
and then of course
private int size = 5;
public void startGame(ActionEvent event) throws IOException {
// ...
GameStageController gameStageController = loader.getController();
gameStageController.showPane(size);
}
If your GameStageController instance needs the size variable later on, you can create an instance variable in that class, and set it in the showPane method to the value passed as the parameter.
You just need to make it static so you can access it using the class name directly and if your controllers are in different packages you need to add public because by default the visibility is package.
So you have to declare size like this :
public static int size = 5;
To access it you do :
SettingsStageController.size
To give some background: I now am able to load files onto my mp3 program and play them but all the values in my tableview are null?
My song class
package application;
//imports here
public class Song {
private String title;
private String artist;
private String album;
private SimpleStringProperty pTitle;
private SimpleStringProperty pArtist;
private SimpleStringProperty pAlbum;
private Media music;
private MediaPlayer mp;
private Image coverArt;
public Song(File file) {
music = new Media(file.toURI().toString());
music.getMetadata().addListener((Change<? extends String, ? extends Object> c) -> {
if (c.wasAdded()) {
if ("artist".equals(c.getKey())) {
System.out.println(c.getKey()+":"+c.getValueAdded());
this.pArtist = new SimpleStringProperty(c.getValueAdded().toString());
//pArtist.set(c.getValueAdded().toString());
artist = c.getValueAdded().toString();
} else if ("title".equals(c.getKey())) {
title = c.getValueAdded().toString();
System.out.println(c.getKey()+":"+c.getValueAdded());
} else if ("album".equals(c.getKey())) {
album = c.getValueAdded().toString();
System.out.println(c.getKey()+":"+c.getValueAdded());
} else if ("image".equals(c.getKey())) {
coverArt = (Image) c.getValueAdded();
}
}
});
mp = new MediaPlayer(music);
System.out.println(pArtist);
System.out.println(artist);
//artist = (String) mp.getMedia().getMetadata().get("artist");
//title = (String) music.getMetadata().get("title");
//album = (String) music.getMetadata().get("album");
//artist = "test";
//album = "test";
//title = "test";
}
public void play() {
mp.play();
}
public void pause() {
mp.pause();
}
public void stop() {
mp.stop();
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
public String getArtist(){
return artist;
}
public void setArtist(String artist){
this.artist = artist;
}
public String getAlbum(){
return album;
}
public void setAlbum(String album){
this.album = album;
}
public Image getCover(){
return coverArt;
}
public MediaPlayer getMP(){
return mp;
}
}
Weirdly enough at first I thought it was because my String variables were not setting correctly and were set to null since it shows as null in the console when I put these print lines to test it when the Song object is being constructed. Here is a sample of the console when I test this.
null
null
artist:Foo Fighters
album:Saint Cecilia EP
title:Saint Cecilia
Here is my controller class
public class SceneController implements Initializable{
#FXML
private Button stopBtn;
#FXML
private Slider volume;
#FXML
private Button loadBtn;
#FXML
private Button playBtn;
#FXML
private TableView<Song> table;
#FXML
private Label label;
#FXML
private ProgressBar proBar;
private TableColumn songCol,artistCol,albumCol;
ObservableList<Song> songList = FXCollections.observableArrayList();
List<File> list;
FileChooser fileChooser = new FileChooser();
Desktop desktop;
Song mySong;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
TableColumn songCol = new TableColumn("Song");
TableColumn artistCol = new TableColumn("Artist");
TableColumn albumCol = new TableColumn("Album");
songCol.setCellValueFactory(
new PropertyValueFactory<Song,String>("title"));
//songCol.setCellFactory(new Callback);
artistCol.setCellValueFactory(
new PropertyValueFactory<Song,String>("artist"));
albumCol.setCellValueFactory(
new PropertyValueFactory<Song,String>("album"));
volume.setMin(0);
volume.setMax(100);
volume.setValue(100);
volume.valueProperty().addListener(new InvalidationListener() {
#Override
public void invalidated(Observable observable) {
mySong.getMP().setVolume(volume.getValue()/100.0);
}
});
}
// Event Listener on Button[#loadBtn].onAction
#FXML
public void loadFile(ActionEvent event) {
Node source = (Node) event.getSource();
Window theStage = source.getScene().getWindow();
//set fileChooser filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("MP3 files", "*.mp3");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.setTitle("Select MP3 files");
//File file = fileChooser.showOpenDialog(theStage);
//mySong = new Song(file);
list = fileChooser.showOpenMultipleDialog(theStage);
if(list!=null){
for(File x: list) {
mySong = new Song(x);
System.out.println(mySong.getTitle());
songList.add(mySong);
}
}
table.setItems(songList);
}
#FXML
public void playSong(ActionEvent event) {
mySong.play();
}
#FXML
public void stopSong(ActionEvent event) {
//mySong.pause();
System.out.println("song title: "+mySong.getArtist()+mySong.getTitle());
ImageView img = new ImageView(mySong.getCover());
//img.fitWidthProperty().bind(label.widthProperty());
//img.fitHeightProperty().bind(label.heightProperty());
img.setFitHeight(120);
img.setFitWidth(200);
label.setGraphic(img);
//label.setGraphic(new ImageView(mySong.getCover()));
}
But I made another test print line for my "Stop" button in the controller class and after everything is loaded and I press it, it prints out the artist and title fine. I have saw this other thread and checked my getter methods and they seem to be correct? I am really lost on this and if anyone could provide some insight and a solution as to whether it is because my variables are null or my PropertyValueFactory is not done correctly
Also I notice that the nulls come first even though should they not be the last thing printed since when I create a new song object in my controller class the first print lines that run are in the if statements?
There are several things wrong with the way you have your current code, that are evident from the limited example you posted in the question:
Your Song class does not properly follow the JavaFX properties pattern. In particular, you store each "property" twice, once in a "traditional" JavaBean-style field, for example private String title, and once in a JavaFX property: private StringProperty pTitle;. Each property should be stored once. If you want the table to be aware when the value changes, you should use JavaFX properties, and have the "standard" getXXX() and setXXX() retrieve and set the underlying values stored in those properties.
The listener you attach to the media's metadata is called asynchronously at some indeterminate point in the future. When you add the song to the table's list, the cell value factories attached to the columns will, at some point, be executed, and retrieve the assigned property from the Song instance. With the code the way you currently have it, those property instances are only actually created once the listener on the metadata is invoked. So it is possible (perhaps likely) that the cell value factory will inspect the Song instance for its property before the JavaFX property is instantiated, making it impossible for the table to properly observe the property and respond to changes in it. You should instantiate the JavaFX properties when the Song instance is created, and set their value in the listener on the metadata.
At no point do you add the columns you create in the controller to the table. If you are creating them in the FXML file (which you didn't post in the question), you should inject those columns into the controller and initialize those columns with the cell value factories. (Since the screenshot shows there are columns in the table, I am going to assume they are defined in the FXML file, and have appropriate fx:ids.)
So your Song class should look something like this:
public class Song {
private final StringProperty title = new SimpleStringProperty();
private final StringProperty artist = new SimpleStringProperty();
private final StringProperty album = new SimpleStringProperty();
private Media music;
private MediaPlayer mp;
private Image coverArt;
public Song(File file) {
music = new Media(file.toURI().toString());
music.getMetadata().addListener((Change<? extends String, ? extends Object> c) -> {
if (c.wasAdded()) {
if ("artist".equals(c.getKey())) {
setArtist(c.getValueAdded().toString());
} else if ("title".equals(c.getKey())) {
setTitle(c.getValueAdded().toString());
} else if ("album".equals(c.getKey())) {
setAlbum(c.getValueAdded().toString());
} else if ("image".equals(c.getKey())) {
// maybe this needs to be a JavaFX property too: it is not clear from your question:
coverArt = (Image) c.getValueAdded();
}
}
});
mp = new MediaPlayer(music);
}
public void play() {
mp.play();
}
public void pause() {
mp.pause();
}
public void stop() {
mp.stop();
}
public StringProperty titleProperty() {
return title ;
}
public final String getTitle(){
return titleProperty().get();
}
public final void setTitle(String title){
titleProperty().set(title);
}
public StringProperty artistProperty() {
return artist ;
}
public final String getArtist(){
return artistProperty().get();
}
public final void setArtist(String artist){
artistProperty.set(artist);
}
public StringProperty albumProperty() {
return album ;
}
public final String getAlbum(){
return albumProperty().get();
}
public final void setAlbum(String album){
albumProperty().set(album);
}
public Image getCover(){
return coverArt;
}
public MediaPlayer getMP(){
return mp;
}
}
For your controller, I am going to assume your FXML file has defined table columns with fx:ids of "songCol", "artistCol", and "albumCol", respectively. You need to inject these into the controller as you do with the other columns. I also strongly recommend not using the PropertyValueFactory class, which uses reflection and lacks much in the way of compile-time checking, and implementing the callback yourself. Using lambda expressions makes this pretty easy.
So your controller should look like:
public class SceneController implements Initializable{
// non-table code omitted...
#FXML
private TableView<Song> table;
#FXML
private Label label;
#FXML
private ProgressBar proBar;
#FXML
private TableColumn<Song, String> songCol ;
#FXML
private TableColumn<Song, String> artistCol ;
#FXML
private TableColumn<Song, String> albumCol;
ObservableList<Song> songList = FXCollections.observableArrayList();
List<File> list;
FileChooser fileChooser = new FileChooser();
Desktop desktop;
Song mySong;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
songCol.setCellValueFactory(cellData -> cellData.getValue().titleProperty());
artistCol.setCellValueFactory(cellData -> cellData.getValue().artistProperty());
albumCol.setCellValueFactory(cellData -> cellData.getValue().albumProperty());
// ...
}
// other non-table code omitted...
}
You didn't post an minimal, complete, verifiable example, so there may well be other errors in your code which prevent the table from displaying correctly. This should get you started, however.
Normally the TableColumns would be defined in FXML and injected via #FXML.
See the Oracle TableView FXML example.
If you don't want to do it that way, you need to do:
table.getColumns().add(songCol);
And similarly for your other columns.
Also, as HypnicJerk pointed out in comments you also need to follow appropriate naming conventions when using the PropertyValueFactory.
songCol.setCellValueFactory(
new PropertyValueFactory<Song,String>("title")
);
For more details see:
Javafx tableview not showing data in all columns
Im fairly new to both stackoverflow and javafx so please be nice.
Description of what Im doing:
I am making a simple quiz game. First window is just a like a welcome/start screen, when that button is clicked we get to the second screen where all the category buttons are, when one of them is clicked it will randomly pick a question of that kind of category user has selected and the third and last window will appear with the questions category as a label, question as textfield and answear as textfield.
Problem:
Whenever a category is clicked I need that current controller to set the next controllers textfields and label. I havent come to far with this. I just get a nullpointerexception when im calling the setQuestion method in the second controller, FXMLCategoriesDocumentController, when I tried to debug it it just says that the instantiated "questControll" is null all the time, and the "questControll.question/category/answear" is referenced from a null object
Code:
second controller
public class FXMLCategoriesDocumentController implements Initializable {
/**
* Initializes the controller class.
*/
private FXMLQuestionDocumentController questControll;
private Question quest;
#FXML
private void geografButtonAction(ActionEvent event) {
try {
FXMLLoader fxmlQuestLoader = new FXMLLoader(getClass().getResource("FXMLQuestionDocument.fxml"));
this.questControll = fxmlQuestLoader.<FXMLQuestionDocumentController>getController();
quest = new Question("Geografi", "Vad heter Sveriges huvudstad?", "Stockholm");
questControll.setQuestion(quest.getCategory(), quest.getQuestion(), quest.getAnswear());
Parent root1 = (Parent) fxmlQuestLoader.load();
root1.setId("pane");
Stage app_stage = (Stage)((Node) event.getSource()).getScene().getWindow();
Scene root1_scene = new Scene(root1);
root1_scene.getStylesheets().addAll(this.getClass().getResource("style.css").toExternalForm());
app_stage.hide();
app_stage.setScene(root1_scene);
app_stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
third controller
public class FXMLQuestionDocumentController implements Initializable {
private FXMLCategoriesDocumentController catControll;
private Question quest;
#FXML
public Label category = new Label();
#FXML
public TextField question = new TextField();
#FXML
public TextField answear = new TextField();
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
}
public void setQuestion(String cat, String quest, String ans){
if(category.getText() == null || question.getText() == null || answear.getText() == null){
System.out.println("everything is null");
}else{
category.setText(cat);
question.setText(quest);
answear.setText(ans);
}
}
Question class
public class Question {
private String category;
private String question;
private String answear;
public Question(String cat, String quest, String ans){
this.category = cat;
this.question = quest;
this.answear = ans;
}
public void setCategory(String cat){
this.category = cat;
}
public void setQuestion(String quest){
this.question = quest;
}
public void setAnswear(String ans){
this.answear = ans;
}
public String getCategory(){
return category;
}
public String getQuestion(){
return question;
}
public String getAnswear(){
return answear;
}
}
FXML second controller(category)
category xml
FXML third controller(question)
question xml
You can specify the the class of controller in each XML-File by using the fx:controller-Tag (here) in the highest element of your (f)xml-Tree.
You can load it then by using this:
YourCustomController controller;
//some code...
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLQuestionDocument.fxml"));
controller = loader.<YourCustomController>getController();
//assume that we create a question here
Question q = new Question(...);
controller.setQuestion(q);
}
//further code...
Edit after comments:
As fabian said, attributes annotated with #FXML are created before the initialize()-method if the fx:id-tag is set for an element in your FXML-file. The fx:id-tag must be the same as the attribute in your controller.
In you fxml-file (for example a Label):
<Label fx:id="question" ...>
....
</Label>
In your Controller-class:
public class YourCustomController implements Initializable {
#FXML Label question;
//...
public void initialize() {
//...
}
public void setQuestion(Question q) {
question.setText(q.getQuestion();
}
}
For further information, see the link in jewelseas comment.
I tried this tomorrow and I hope I transferred it here the right way.
Edit from July, 13th
In your FXMLQuestionDocumentController, you don't need to initiliaze your controls. See:
public class FXMLQuestionDocumentController implements Initializable {
private FXMLCategoriesDocumentController catControll;
private Question quest;
#FXML
public Label category;
#FXML
public TextField question;
#FXML
public TextField answear;
}
Also, initialize your controller after you initialized your Pane.
`#FXML
private void geografButtonAction(ActionEvent event) {
try {
FXMLLoader fxmlQuestLoader = new FXMLLoader(getClass().getResource("FXMLQuestionDocument.fxml"));
quest = new Question("Geografi", "Vad heter Sveriges huvudstad?", "Stockholm");
Parent root1 = (Parent) fxmlQuestLoader.load();
root1.setId("pane");
this.questControll = fxmlQuestLoader.FXMLQuestionDocumentController>getController();
questControll.setQuestion(quest.getCategory(), quest.getQuestion(), quest.getAnswear());
Stage app_stage = (Stage)((Node) event.getSource()).getScene().getWindow();
Scene root1_scene = new Scene(root1);
root1_scene.getStylesheets().addAll(this.getClass().getResource("style.css").toExternalForm());
app_stage.hide();
app_stage.setScene(root1_scene);
app_stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}`
I'm working on a MVC project and have the following issue:
In a view called CentralLayout is the following code:
EventView e = new EventView();
gridSchedule.add(e.createView(5), 1, 1, 1, 5);
where createView method is defined in the following interface:
public interface Creatable {
public Node createView();
public Node createView(int eventDuration);
}
EventView is implemented as:
public class EventView extends BaseViewController implements Initializable, Creatable {
private int PANE_HEIGHT = 10;
final int PANE_WIDTH = 99;
#FXML
private Label lblObjectOne;
#FXML
private Label lblObjectTwo;
#FXML
private Pane eventPane;
#Override
public Node createView() {
return null;
}
#Override
public Node createView(int eventDuration) {
PANE_HEIGHT = eventDuration * 20;
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("eventView.fxml"));
Parent root = null;
try {
root = fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
return root;
}
#Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println(PANE_HEIGHT);
eventPane.setMaxHeight(PANE_HEIGHT);
eventPane.setMaxWidth(PANE_WIDTH);
lblObjectOne.setText("test");
lblObjectTwo.setText("test");
}
}
NOTE: I've created eventPane in SceneBuilder and its properties are defined in eventView.fxml file.
The problem is that the final, drawn, height of eventPane is equals to 10, and not 100. In console value 10 is printed as a PANE_HEIGHT value.
Can anyone tell me, why PANE_HEIGHT haven't changed when initialize is called, when I first called the the createView method on object e and passed the value that multiplied PANE_HEIGHT variable? Thanks.
You create a new controller when you use the 'FXMLLoader'. To use the one already created you have to use the setController method of the FXMLLoader before loading the fxml file to use a controller instance you created yourself:
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("eventView.fxml"));
Parent root = null;
fxmlLoader.setController(this);
try {
root = fxmlLoader.load();
You are probably not re-using the same EventView instance (e) when you call initialize(). Only the EventView instance you used to call createView() will have the expected value for PANE_HEIGHT.
I'm trying to fill a listview with the artist and title of songs using the open() method.
To achieve this I created the artist and title ArrayLists and merged them using the create() method.
The problem is, when I try to run create() inside open() nothing happens. However, if I assign the create() method to a different button and click it after using the filechooser everything works fine.
So, I would like to know if it is possible to run the create() method after the open() method using only one button via fxml or regular java code.
public class PLController implements Initializable {
#Override
public void initialize(URL location, ResourceBundle resources) {
list.setItems(visibleList);
}
List<File> filelist = new ArrayList<File>();
ArrayList<String> title = new ArrayList<String>();
ArrayList<String> artist = new ArrayList<String>();
ObservableList<String> visibleList = FXCollections.observableArrayList();
#FXML
ListView<String> list;
#FXML
Button impButton;
public void create(){
for(int i = 0; i < title.size(); i++){
visibleList.add(artist.get(i) +" - " +title.get(i));
Collections.sort(visibleList);
}
}
public void handleMetadata(String key, Object value){
if (key.equals("title")){
title.add(value.toString());
}
if (key.equals("artist")){
artist.add(value.toString());
}
}
public void open(){
FileChooser chooser = new FileChooser();
filelist = chooser.showOpenMultipleDialog(impButton.getScene().getWindow());
for(File f:filelist){
try {
Media media = new Media(f.toURI().toURL().toString());
media.getMetadata().addListener(new MapChangeListener<String, Object>(){
#Override
public void onChanged(Change<? extends String, ? extends Object> change) {
if(change.wasAdded()) {
handleMetadata(change.getKey(), change.getValueAdded());
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
}
}create(); //Nothing happens
}
As others have pointed out, the Media object does not have its metadata initialized immediately. (It needs to read data from the URL and populate those metadata as it receives them.) That is why the metadata are exposed as an ObservableMap. When you reach the end of your open() method, it is highly unlikely that the metadata will have been initialized, so your create() method will not see any data at that point.
What you need to do is observe the map, and update the ListView once both the artist and title are available. The best way to do this, in my opinion, is to encapsulate the information you want into a separate class:
public class Video {
private final Media media ;
private final ReadOnlyStringWrapper artist = new ReadOnlyStringWrapper("Unknown");
private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper("Title");
public Video(File file) {
try {
this.media = new Media(file.toURI().toURL().toExternalForm());
artist.bind(Bindings.createStringBinding(() -> {
Object a = media.getMetadata().get("artist");
return a == null ? "Unknown" : a.toString();
}, media.getMetadata()));
title.bind(Bindings.createStringBinding(() -> {
Object t = media.getMetadata().get("title");
return t == null ? "Unknown" : t.toString();
}, media.getMetadata()));
}
catch (Exception e) {
throw new RuntimeException("Could not create Video for "+file, e);
}
}
public ReadOnlyStringProperty titleProperty() {
return title.getReadOnlyProperty();
}
public ReadOnlyStringProperty artistProperty() {
return artist.getReadOnlyProperty();
}
public final String getTitle() {
return title.get();
}
public final String getArtist() {
return artist.get();
}
public final Media getMedia() {
return media ;
}
}
Now you can create a ListView<Video> to display the videos. Use a cell factory to display the artist and the title in the format you want. You can make sure that the observable list fires updates when either the artist or title properties change, and you can keep it sorted via a SortedList.
#FXML
private ListView<Video> list ;
private ObservableList<Video> visibleList ;
public void initialize() {
visibleList = FXCollections.observableArrayList(
// make list fire updates when artist or title change:
v -> new Observable[] {v.artistProperty(), v.titleProperty()});
list.setItems(new SortedList<>(list, Comparator.comparing(this::formatVideo)));
list.setCellFactory(lv -> new ListCell<Video>() {
#Override
public void updateItem(Video item, boolean empty) {
super.updateItem(item, empty) ;
setText(formatVideo(item));
}
});
}
#FXML
private void open() {
FileChooser chooser = new FileChooser();
List<File> fileList = chooser.showOpenMultipleDialog(impButton.getScene().getWindow());
if (fileList != null) {
fileList.stream()
.map(Video::new)
.forEach(visibleList::add);
}
}
private String formatVideo(Video v) {
if (v == null) return "" ;
return String.format("%s - %s", v.getArtist(), v.getTitle());
}
Simply creating a Media object and assigning a listener to it won't fire the code in the listener. So the title list in your code remains empty. The create() method is called, but since you are iterating over an empty list, nothing actually happens.
Use a debugger or add some logging information in such cases.
Also, you should sort the list after the for loop, not every time you add an item.