Complete fxml & Cotroller class

This commit is contained in:
2026-05-24 23:49:54 +03:30
parent c3205138cb
commit 2842cd414c
4 changed files with 119 additions and 10 deletions
+76 -7
View File
@@ -2,30 +2,99 @@ package sbu.javafx;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.ArrayList;
public class MusicController { public class MusicController {
// TODO: Define your UI components using the @FXML annotation. // TODO: Define your UI components using the @FXML annotation.
@FXML private ListView<String> lvSongs;
@FXML private Label lblStatus;
private ArrayList<String> playlist = new ArrayList<>();
private String[] songsArray = {"Arcade Duncan Laurence 3.07",
"How i love you Engelbert 4.20",
"Memmories Conan Gray 4.09",
"Night Changes One Direction 3.47",
"Careless Whisper George Michael 5.00"
};
@FXML public void initialize() { lvSongs.getItems().setAll(songsArray); }
//TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing. //TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing.
public String songName(int a)
{
String songName = "";
if (a == 0) songName = "Arcade";
else if (a == 1) songName = "How i love you";
else if (a == 2) songName = "Memmories";
else if (a == 3) songName = "Night Changes";
else songName = "Careless Whisper";
return songName;
}
@FXML @FXML
public void handlePlayAction() { public void handlePlayAction()
//Update labels, change button icons, etc. {
String selectedSong = lvSongs.getSelectionModel().getSelectedItem();
if (selectedSong == null)
{
lblStatus.setText("Please choose a song");
return;
}
lblStatus.setText(" is playing : " + songName(lvSongs.getSelectionModel().getSelectedIndex()));
} }
//TODO: Logic to add a specific song to the user's playlist. //TODO: Logic to add a specific song to the user's playlist.
public void addToPlaylist(String songName) { public void addToPlaylist()
//Add the song to a list or update a ListView. {
String selectedSong = lvSongs.getSelectionModel().getSelectedItem();
if (selectedSong == null)
{
lblStatus.setText("Please choose a song");
return;
}
if (!playlist.contains(selectedSong))
{
playlist.add(selectedSong);
lblStatus.setText(songName(lvSongs.getSelectionModel().getSelectedIndex()) + " added to playList");
}
else lblStatus.setText(songName(lvSongs.getSelectionModel().getSelectedIndex()) + " is already exists");
} }
// TODO:This method should open a new window or change the current scene to display the "Playlist" page. // TODO:This method should open a new window or change the current scene to display the "Playlist" page.
public void openPlaylistWindow() { public void openPlaylistWindow()
//Create a new stage and show the playlist UI. {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Playlist-view.fxml"));
Scene scene = null;
try {
scene = new Scene(loader.load());
} catch (IOException e) {
throw new RuntimeException(e);
}
PlayListController controller = loader.getController();
controller.setPlaylist(playlist);
Stage stage = new Stage();
stage.setTitle("Playlist");
stage.setScene(scene);
stage.show();
} }
} }
@@ -0,0 +1,14 @@
package sbu.javafx;
import java.util.ArrayList;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
public class PlayListController
{
@FXML private ListView<String> lvPlaylist;
public void setPlaylist(ArrayList<String> playlist)
{
lvPlaylist.getItems().addAll(playlist);
}
}
+19 -3
View File
@@ -1,8 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/fxml" alignment="CENTER" spacing="10"> <?import javafx.scene.layout.HBox?>
<!-- TODO: Add your code here! --> <VBox alignment="CENTER" spacing="15.0" xmlns:fx="http://javafx.com/fxml" fx:controller="sbu.javafx.MusicController">
<Label fx:id="lblStatus" text="Musiv Player" style="-fx-font-size: 18px; -fx-font-weight: bold;"/>
</VBox> <ListView fx:id="lvSongs" prefHeight="200.0" prefWidth="350.0"/>
<HBox spacing="10" alignment="CENTER">
<Button fx:id="btnPlayStop" text="PLay ▶" onAction="#handlePlayAction" prefWidth="300"/>
</HBox>
<VBox spacing="10"/>
<Button text="Add to playlist " onAction="#addToPlaylist" prefWidth="300"/>
<VBox spacing="10"/>
<Button fx:id="btnViewPlaylist" text="Show Playlist" onAction="#openPlaylistWindow" prefWidth="300"/>
</VBox>
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="TOP_CENTER" spacing="10.0" xmlns:fx="http://javafx.com/fxml" fx:controller="sbu.javafx.PlayListController">
<Label text="Your PlayList : " style="-fx-font-size: 20px; -fx-font-weight: bold;"/>
<ListView fx:id="lvPlaylist" prefHeight="300.0" prefWidth="300.0"/>
</VBox>