repost: JavaFX GUI Full Course ☕【𝙁𝙧𝙚𝙚】 - YouTube

# Enviroment set up in VSCode

  1. Install Extension Pack for Java

  2. Ctrl + Shift + P input Java: Cretate Java Project and then select the project type No build tools.

  3. Download JavaFX SDK jar and add to Referenced Libraries.

  4. Run debug and config launch.json.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    {
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
    {
    "type": "java",
    "name": "Main",
    "request": "launch",
    "mainClass": "com.jbn.mp3player.Main",
    "vmArgs": "--module-path D:/study/java/javafx/javafx-sdk-17.0.8/lib --add-modules javafx.controls,javafx.fxml --add-modules javafx.media,javafx.web"
    }
    ]
    }

# Define Layout and Control in FXML use Scene Builder

# Define Logic in Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.jbn.mp3player;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.Slider;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;

public class Controller implements Initializable {

@FXML
private Label songLabel;

@FXML
private ProgressBar sonProgressBar;

@FXML
private Button playButton;
@FXML
private Button pauseButton;
@FXML
private Button resetButton;
@FXML
private Button preButton;
@FXML
private Button nextButton;

@FXML
private ComboBox<String> speedBox;

@FXML
private Slider volumeSlider;

private File directory;
private File[] files;

private ArrayList<File> songs;

private int songNumber;

private int[] speeds = { 50, 75, 100, 125, 150, 175, 200 };

private Timer timer;

private TimerTask timerTask;

private boolean running;

private Media media;
private MediaPlayer mediaPlayer;

@Override
public void initialize(URL location, ResourceBundle resources) {
songs = new ArrayList<>();

directory = new File("D:/study/java/javafx/hellofx/src/com/jbn/mp3player/music");
files = directory.listFiles();
if (files != null) {
for (File file : files) {
songs.add(file);
}
}

changeSong();

for (int i = 0; i < speeds.length; i++) {
speedBox.getItems().add(Integer.toString(speeds[i]) + "%");
}

speedBox.setOnAction(this::changeSpeed);

volumeSlider.valueProperty().addListener(new ChangeListener<Number>() {

@Override
public void changed(ObservableValue<? extends Number> arg0, Number arg1, Number arg2) {
mediaPlayer.setVolume(volumeSlider.getValue() * 0.01);
}

});

sonProgressBar.setStyle("-fx-accent: #0F0");
}

private void changeSong() {
media = new Media(songs.get(songNumber).toURI().toString());
mediaPlayer = new MediaPlayer(media);
songLabel.setText(songs.get(songNumber).getName());
}

public void playMedia() {
beginTimer();
changeSpeed(null);
mediaPlayer.play();
}

public void pauseMedia() {
cancelTimer();
mediaPlayer.pause();
}

public void resetMedia() {
sonProgressBar.setProgress(0.0);
mediaPlayer.seek(Duration.seconds(0.0));
}

public void preMedia() {
if (songNumber > 0) {
songNumber--;
} else {
songNumber = songs.size() - 1;
}

stopMedia();

if (running) {
cancelTimer();
}

changeSong();
playMedia();
}

public void nextMedia() {
if (songNumber < songs.size() - 1) {
songNumber++;
} else {
songNumber = 0;
}
stopMedia();

if (running) {
cancelTimer();
}

changeSong();
playMedia();
}

private void stopMedia() {
mediaPlayer.stop();
}

public void changeSpeed(ActionEvent event) {
if (speedBox.getValue() == null) {
mediaPlayer.setRate(1);
} else {
String value = speedBox.getValue().substring(0, speedBox.getValue().length() - 1);
mediaPlayer.setRate(Integer.parseInt(value) * 0.01);
}
}

private void beginTimer() {
timer = new Timer();

timerTask = new TimerTask() {

@Override
public void run() {
running = true;
double current = mediaPlayer.getCurrentTime().toSeconds();
double end = mediaPlayer.getTotalDuration().toSeconds();
sonProgressBar.setProgress(current / end);
if (current / end == 1) {
cancelTimer();
}
}

};

timer.schedule(timerTask, 1000, 1000);
}

private void cancelTimer() {
running = false;
timer.cancel();
}
}

# Load FXML and run program in Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.jbn.mp3player;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Scene.fxml"));
stage.setScene(new Scene(root));
stage.show();
}

public static void main(String[] args) {
launch(args);
}

}

# Run program

In VSCode, click Debug and select defined Main in launch.json then execute.

Edited on