In this lab, we are going to explore some components in JavaFX, we will first talk about media elements, and how we could play a video, or music in JavaFX, and then we will talk about Table Views, which will enable us to show tabular data.
The media elements in JavaFX are pretty straightforward, you will add the element, then you will set the path for the element to play and that's it!
Media View Example
The media class will define the media source for the elements, so it will contain the path of the media.
Notice that you could get the file URI from a resource, or use the File
class.
The media element doesn't play anything on its own, it will just define the resource and a helper for reading the file.
The media player class will allow us to play and control a media file, notice that this will control it but never view it, so it is the player, not the viewer.
Try to create a media player to play something, you will notice that the sound will stop after 5 seconds or so, this is due to the garbage collector deleting our media player, so be sure to define it somewhere safe!
The media view element is responsible for showing the media (e.g video), this is a regular component you can add anywhere you want and it will show the video for you!
You could now create your control elements to control the media player!
The table view is a very common and useful component, Table view will allow you to view data as a table, sort data, edit data, and more.
Table View Example
The table view is a generic class, which takes the type of each row in it, which makes it easier for the developer to work with.
Now, let create a colume for the data we want to view.
Notice that the column will not show the data, we should define for the column how to get the value of Name
from Student
class.
We could do that by extending CellDataFeatures
, or we could easily use PropertyValueFactory
, which will try to get an observable property or use the get
method to get the data.
You could change how the cell gets viewed, for example, you could display a checkbox instead of the string true
, false
, or you could create a combo box.
You could use TextFieldTableCell
, which will allow you to show a text field to edit string data or use ComboBoxTableCell
to create a combo box.
You could make the table view editable using the method setEditable
, which will allow the user to edit the values.
If you used a Property
, the table view will know how to handle edits, but if used regular classes, you will have to handle it yourself.
Notice that because we're using generics, we get the row as the type of the data Student
.
So use StringProperty
to make your life easier!
The table selection model is responsbable for the selection of rows, you can get the selected row like this:
You could enable multipale selection like this:
Create a video player with controls, the video player should have the following:
Task 1 Expected Output
Note: Don't panic when you implement seek bar XD.
Programming
Java
IUG