My media player is not working. But there is no error in execution.
I'm using an FXML file to generate the MediaView.
Media and MediaPlayer is generated via initialize.
Normally, the video would have to play directly, but it donĀ“t.
public class Controller {
#FXML
private MediaView mediaView;
private MediaPlayer mp;
private Media me;
#FXML
void play(ActionEvent event) {
}
#FXML
public void initialize() {
System.out.println("test");
String path = new File("src/video/video.mp4").getAbsolutePath();
me = new Media(new File(path).toURI().toString());
//me = new Media(getClass().getResource("../video/video.mp4").toExternalForm());
mp = new MediaPlayer(me);
mediaView.setMediaPlayer(mp);
mp.setAutoPlay(true);
}
}
Path is correct. Controller works
Related
I have a problem of full screen for the media View, I made my interface with scene builder and when I implement the method of fullScreen all the window becomes in full screen mode and not the mediaView only.
private MediaPlayer mediaPlayer;
#FXML
private MediaView mediaView;
#FXML
private Label label;
#FXML
private String filepath;
#FXML
private void handleButtonAction(ActionEvent event) {
}
#FXML
private void openFile(ActionEvent event){
FileChooser fileChooser=new FileChooser();
FileChooser.ExtensionFilter filter =new FileChooser.ExtensionFilter("select a file","*.mp4","*.mp3");
fileChooser.getExtensionFilters().add(filter);
File file=fileChooser.showOpenDialog(null);
filepath = file.toURI().toString();
if(filepath!=null){
Media media = new Media(filepath);
mediaPlayer = new MediaPlayer(media);
mediaView.setMediaPlayer(mediaPlayer);
mediaPlayer.play();
}
}
#FXML
private void fullScreen(ActionEvent event){
((Stage)mediaView.getScene().getWindow()).setFullScreen(true);
}
I have 1 "ViewElements"-Class, 1 Controller and 1 FXML-file.
The ViewElements-Class contains the elements of the FXML like Buttons and textfields.
The Controller-Class contains the Business logic.
I try to update the TextField "textfieldDateiAuswaehlen", I want to set the path of the File into the TextField but my method does not work.
ViewElements:
public class ViewElements {
#FXML private TextField textfieldDateiAuswaehlen;
#FXML private TextArea textareaXmlContent;
#FXML private Button buttonXmlBearbeiten;
#FXML private Button buttonXmlLaden;
#FXML private Button buttonXmlOeffnen;
public ViewElements() {
this.textfieldDateiAuswaehlen= new TextField();
this.textareaXmlContent = new TextArea();
this.buttonXmlBearbeiten = new Button();
this.buttonXmlLaden = new Button();
this.buttonXmlOeffnen = new Button();
}
public TextField getTextfieldDateiAuswaehlen() {
return textfieldDateiAuswaehlen;
}
public void setTextfieldDateiAuswaehlenText(String text) {
this.textfieldDateiAuswaehlen.setText(text);
}
public String getTextfieldDateiAuswaehlenContent() {
return this.textfieldDateiAuswaehlen.getText();
}
public TextArea getTextareaXmlContent() {
return textareaXmlContent;
}
public void setTextareaXmlText(String text) {
this.textareaXmlContent.setText(text);
}
public Button getButtonXmlBearbeiten() {
return buttonXmlBearbeiten;
}
public Button getButtonXmlLaden() {
return buttonXmlLaden;
}
public Button getButtonXmlOeffnen() {
return buttonXmlOeffnen;
}}
Controller:
public class SampleController implements Initializable{
ViewElements viewElems= new ViewElements();
#FXML
private void handleButtonLaden(ActionEvent event){
System.out.println("Klicked");
}
#FXML
private void handleButtonXmlOeffnen(ActionEvent event){
FileChooser filechooser = new FileChooser();
File file = filechooser.showOpenDialog(null);
//Falls eine Datei ausgewaehlt ist
if(file != null){
//Falls TextField leer ist
if(viewElems.getTextfieldDateiAuswaehlenContent().isEmpty()) {
System.out.println(file.getAbsolutePath().toString());
viewElems.getTextfieldDateiAuswaehlen().clear();
String verzeichnis = file.getAbsolutePath().toString();
viewElems.setTextfieldDateiAuswaehlenText(verzeichnis);
Service<Void> service = new Service<Void>() {
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
protected Void call() throws Exception {
Platform.runLater(() -> viewElems.setTextfieldDateiAuswaehlenText(verzeichnis));
return null;
}
};
}
};
service.start();
System.out.println("PRINT: " + viewElems.getTextfieldDateiAuswaehlenContent());
}
}
}
#Override
public void initialize(URL location, ResourceBundle resources) {
}}
In the screenshot you see that the path is passed to TextField but the TextField in the UI does not update.
Where is my mistake?
When you load the FXML file the FXMLLoader creates the UI nodes corresponding to the elements in the FXML.
If you declare a controller, give the elements fx:id attributes, and declare #FXML-annotated fields in the controller, the FXMLLoader will set those fields in the controller to the UI nodes created from the FXML.
In your code, your controller contains no #FXML-annotated fields. You create an instance of your ViewElements class, which creates some new instances of TextField and Button:
public ViewElements() {
this.textfieldDateiAuswaehlen= new TextField();
this.textareaXmlContent = new TextArea();
this.buttonXmlBearbeiten = new Button();
this.buttonXmlLaden = new Button();
this.buttonXmlOeffnen = new Button();
}
Obviously these are not the same text fields and buttons created by the FXMLLoader.
Presumably, somewhere, you load the FXML and display the UI created by the FXMLLoader; but you don't display the UI nodes created in your ViewElements instance. So when you modify the nodes in your ViewElements instance, you are not modifying the UI you have displayed, and consequently you don't see anything.
You need to place the UI elements directly in the controller (which is perhaps better thought of as a presenter). The only way the FXMLLoader can assign the objects it creates to fields is if those fields are in the controller, because that is the only other object the controller "knows about".
If you want to separate the logic into a different class from the class that contains the UI elements, then make the "controller" the class that has the UI elements, and create a different class containing the implementation of the logic. Then in the "controller" class, just delegate the user event handling to your new class.
I.e. change the fx:controller attribute to point to ViewElements, and refactor as
public class ViewElements {
#FXML private TextField textfieldDateiAuswaehlen;
#FXML private TextArea textareaXmlContent;
#FXML private Button buttonXmlBearbeiten;
#FXML private Button buttonXmlLaden;
#FXML private Button buttonXmlOeffnen;
private SampleController controller ;
public void initialize() {
controller = new SampleController(this);
}
#FXML
private void handleButtonXmlOeffnen(ActionEvent event){
controller.handleButtonXmlOeffnen();
}
public TextField getTextfieldDateiAuswaehlen() {
return textfieldDateiAuswaehlen;
}
public void setTextfieldDateiAuswaehlenText(String text) {
this.textfieldDateiAuswaehlen.setText(text);
}
public String getTextfieldDateiAuswaehlenContent() {
return this.textfieldDateiAuswaehlen.getText();
}
public TextArea getTextareaXmlContent() {
return textareaXmlContent;
}
public void setTextareaXmlText(String text) {
this.textareaXmlContent.setText(text);
}
public Button getButtonXmlBearbeiten() {
return buttonXmlBearbeiten;
}
public Button getButtonXmlLaden() {
return buttonXmlLaden;
}
public Button getButtonXmlOeffnen() {
return buttonXmlOeffnen;
}
}
public class SampleController {
private final ViewElements viewElems ;
public SampleController(ViewElements viewElems) {
this.viewElems = viewElems ;
}
public void handleButtonXmlOeffnen() {
FileChooser filechooser = new FileChooser();
File file = filechooser.showOpenDialog(null);
//Falls eine Datei ausgewaehlt ist
if(file != null){
//Falls TextField leer ist
if(viewElems.getTextfieldDateiAuswaehlenContent().isEmpty()) {
System.out.println(file.getAbsolutePath().toString());
viewElems.getTextfieldDateiAuswaehlen().clear();
String verzeichnis = file.getAbsolutePath().toString();
viewElems.setTextfieldDateiAuswaehlenText(verzeichnis);
Service<Void> service = new Service<Void>() {
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
protected Void call() throws Exception {
Platform.runLater(() -> viewElems.setTextfieldDateiAuswaehlenText(verzeichnis));
return null;
}
};
}
};
service.start();
System.out.println("PRINT: " + viewElems.getTextfieldDateiAuswaehlenContent());
}
}
}
}
here is my problem :
I have 3 FXML files with each controller assigned.
ExplorerPage.FXML : layout used to show a gridpane with undefined object number inside.
ExplorerNoteController.FXML : the object (StackPane) inserted in the gridpane.
NoteHoverMenuController.FXML : When the user moves the mouse on the previous object it shows options to update or delete the object.
To resume :
ExplorerPage.FXML -> ExplorerNoteController.FXML -> NoteHoverMenuController.FXML
Gridpane -> Stackpane -> Borderpane(2)
What I want to do is when I click on the "delete" button in the NoteHoverMenuController.FXML, this remove the current object from the gridpane (located in ExplorerPage.FXML). But I have no clue to do this. I tried binding with properties, static methods, .. but no satisfaying results.
This is my code :
ExplorerPage controller
public class ExplorerPage implements Initializable {
private Connection conn;
// LOAD ALL NOTES
private List<Note> gridNotes = new LinkedList<Note>();
JFXRadioButton alertButton = new JFXRadioButton(); // TEMP
StackPane spaneExpNote; // StackPane used to switch front/back pane (overlay)
private byte j = 0;
private byte k = 0;
#FXML
private GridPane FXMLNotesPane = new GridPane();
#Override
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
try {
// LOAD DATAS FROM DB W/ PARAM FOLDER
conn = DBInitialize.getInstance();
DAO<Note> findAllNotes = new NoteDAO(conn);
gridNotes = findAllNotes.findAll("Orange");
// STACKPANES CONSTRUCTION
for(int i = 0; i < gridNotes.size(); i++)
{
if (j>2) {
j=0;
k++;
}
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fr/cryption/view/ExplorerNote.fxml"));
spaneExpNote = (StackPane) loader.load();
ExplorerNoteController controller = loader.getController();
controller.setData(gridNotes.get(i).getTitleNote(), gridNotes.get(i).getContentNote(), gridNotes.get(i).getDateNote());
FXMLNotesPane.add(spaneExpNote,j,k);
j++;
}
ExplorerNoteController
public class ExplorerNoteController implements Initializable {
#FXML
StackPane rootNotePane;
#FXML
private Label labPaneTitle1;
#FXML
private Label labPaneDate1;
#FXML
private Label labPaneContent1;
BorderPane pane = null;
String ok = "ok";
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
}
public void setData(String title, String content, String date) {
labPaneTitle1.setText(title);
labPaneContent1.setText(content);
labPaneDate1.setText(date);
}
public void showHoverMenu() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fr/cryption/view/NoteHoverMenu.fxml"));
pane = (BorderPane) loader.load();
NoteHoverMenuController controller = loader.getController();
rootNotePane.getChildren().add(pane);
}
public void hideHoverMenu() throws IOException {
rootNotePane.getChildren().remove(pane);
}
....
NoteHoverMenuController
public class NoteHoverMenuController implements Initializable {
String idNote = "";
#FXML
BorderPane hovernode;
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
public void setID(String id) {
idNote = id;
}
public void deleteNoteExplorer(ActionEvent event) {
System.out.println(hovernode.getParent().lookup("stckpane")); // DELETE THE NOTE
}
}
I will update this post if you want FXML(s) as well.
Thanks.
SCREENS :
Screen Gridpane
Screen Hover StackPane
I have two fxml window login and main window.Their respective controllers are given below:
public class MainwindowController extends Stage implements Initializable {
#FXML private Button Send;
#FXML private TextField txtBcast;
#FXML private ListView listviewUsers;
#FXML Label lblDisplayName;
/**
* Initializes the controller class.
* #param url
* #param rb
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<String> chat =FXCollections.observableArrayList ("default");
listviewUsers.setItems(chat);
}
public void setLblName(String msg){
lblDisplayName.setText(msg);
}
#FXML public void ActionSend(ActionEvent e){
send();
txtBcast.setText("");
}
private void send() {
if (txtBcast.getText().isEmpty())
return;
// chatManager.sendPublicMsg(format,txtBcast.getText());
}
/**
*
* #param e
* #throws Exception
*/
#FXML public void ActionUserSelected( MouseEvent e) throws Exception{
// String lineRest = e.getActionCommand();
if(e.getClickCount()==2)
{
if(!listviewUsers.getSelectionModel().isEmpty())
{
String str=(String)listviewUsers.getSelectionModel().getSelectedItem();
Parent main= FXMLLoader.load(getClass().getResource("/letschat/fxwindows/Usertab.fxml"));
Scene scene = new Scene(main);
Stage stage = new Stage();
stage.setTitle(str);
stage.setScene(scene);
stage.show();
}
else { JOptionPane.showMessageDialog(null, "Oops! it seems you are trying to click the list view"); }
}
//Stage pstage = (Stage)listUsers.getScene().getWindow();
//pstage.close();
}
}
And
public class LoginwindowController extends Stage implements Initializable {
#FXML private LoginwindowController loginwindowController;
#FXML private MainwindowController mainwindowController;
#FXML private Button btnSignIn;
#FXML private TextField txtDisplayName;
#FXML private ToggleGroup Gender;
#FXML private ComboBox comboStatus;
/**
* Initializes the controller class.
* #param url
* #param rb
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<String> items =FXCollections.observableArrayList ("Online","Offline");
comboStatus.setItems(items);
writeToTextField();
}
public void writeToTextField() {
String username = System.getProperty("user.name");
txtDisplayName.setText(""+ username);
}
#FXML protected void ActionSignIn(ActionEvent event) throws Exception {
mainwindowController.setLblName(txtDisplayName.getText());
InetAddress addr = InetAddress.getLocalHost();
if(addr.isLoopbackAddress())
{
Dialogs.create().message("Oops! It seems you are not connected to any network..\n :(").showError();
}
else{
start(txtDisplayName.getText());// start chat manager
Parent root= FXMLLoader.load(getClass().getResource("/letschat/fxwindows/Mainwindow.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setTitle("LetsChat-Welcome "+ txtDisplayName.getText());
// Context.getInstance().setDisplayName(txtDisplayName.getText());
stage.setScene(scene);
stage.getIcons().add(new Image("/letschat/images/logo.png"));
Stage pstage = (Stage)btnSignIn.getScene().getWindow();
stage.show();
pstage.close();
}
}
private void start(String name) {
try {
ChatManager ic = new ChatManager(name);
ic.start();
} catch (Exception ex) {
Dialogs.create().message( "Could not start the chat session\nCheck that there no other instances running :(").showError();
}
}
}
I want the label lblDisplayName in main window updated with text from txtDisplay Name in login window when user clicks signin button.can someone help how to do so..soon plz
There are various ways to do this, in your case Login will create the other stage so an easy way is to create a new FXML loader (variable name: myLoader) and, if you want to pass the username of the user as constructor argument you can use myLoader.setControllerFactory and as return:
return clazz == MyController.class ? new MyController(userName) : null;
MyController is the name of the Controller where you want to read the username
If you want to use set methods, with getController you get the controller instance and call the set method (e.g, myController.setUsername());
To create a custom FXML
FXMLLoader myLoader = new FXMLLoader(<if you use relative paths, here you should pass the position);
remember to call the load() because the URI overload is static. (i.e, use getResourceAsStream).
If your application is large and complex, you could use EventBus (which I prefer everywhere..)
I'm not sure I completely understand the relationship between the two controllers, and which are the FXML files that correspond to the controllers, but it looks like the LoginWindowController loads MainWindow.fxml, and I'm guessing that MainWindowController is the controller for MainWindow.fxml.
In that case, you can just do
#FXML protected void ActionSignIn(ActionEvent event) throws Exception {
InetAddress addr = InetAddress.getLocalHost();
if(addr.isLoopbackAddress())
{
Dialogs.create().message("Oops! It seems you are not connected to any network..\n :(").showError();
}
else{
start(txtDisplayName.getText());// start chat manager
FXMLLoader loader = new FXMLLoader(getClass().getResource("/letschat/fxwindows/Mainwindow.fxml"));
Parent root= loader.load();
MainWindowController mainWindowController = loader.getController();
mainWindowController.setLblName(txtDisplayName.getText());
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setTitle("LetsChat-Welcome "+ txtDisplayName.getText());
// Context.getInstance().setDisplayName(txtDisplayName.getText());
stage.setScene(scene);
stage.getIcons().add(new Image("/letschat/images/logo.png"));
Stage pstage = (Stage)btnSignIn.getScene().getWindow();
stage.show();
pstage.close();
}
}
i've tried to make a browser with JavaFX i want to load a web page which is in FXMLFile1 contains WebView and in the FXMLfile2 there's a button that button loads a web page in WebView which is in FXMLFile1 i write this code but unfortunately didn't work :
#FXML
public void tabfirst (ActionEvent ee) throws IOException {
try {
FXMLLoader vve = new FXMLLoader(getClass().getResource("Choose.fxml"));
Button b1 = tab1b = vve.getController();
FXMLLoader vvve = new FXMLLoader(getClass().getResource("WorkSpace.fxml"));
WebView wv = web1 = vvve.getController();
WebEngine myWebEngine = wv.getEngine();
myWebEngine.load("https://www.google.com");
}
catch (IOException e){
}
}
note this class tabfirst is in the Button in the FXMLFile2 that open the webpage in the WebView and the two FXMLfiles are using the same controller. Please answer me and thanks!
Your Choose controller could have something like this:
public class Choose
{
#FXML private TextField addressField;
/** for Button in FXML onAction="#useAddress" */
#FXML private void useAddress()
{
addressProp.set( addressField.getText() );
}
private final StringProperty addressProp = new SimpleStringProperty();
public StringProperty addressProperty()
{
return addressProp;
}
}
And your WorkSpace controller could have:
public class WorkSpace
{
#FXML private WebView web;
public void setAddress( String address )
{
web.getEngine().load( address );
}
}
Then in the controller that loads the two FXML files you'll have something like:
FXMLLoader workFxml = new FXMLLoader( getClass().getResource("WorkSpace.fxml") );
Node workView = workFxml.load(); // You must call load BEFORE getController !
WorkSpace workCtrl = workFxml.getController();
FXMLLoader chooseFxml = new FXMLLoader( getClass().getResource("Choose.fxml") );
Node chooseView = chooseFxml.load();
Choose chooseCtrl = chooseFxml.getController();
chooseCtrl.addressProperty().addListener
(
(ov, old, newAddress) -> workCtrl.setAddress( newAddress )
);
Alternatively if you are loading the two FXML files from inside a parent FXML with for eg: <fx:include fx:id="work" source="WorkSpace.fxml"/> you'll use:
#FXML private WorkSpace workController; // Must end with Controller !
#FXML private Choose chooseController;
#FXML private void initialize()
{
chooseController.addressProperty().addListener
(
(ov, old, newAddress) -> workController.setAddress( newAddress )
);
}