Common menu item for all scenes in Java FX - javafx-2

I want to develop a multiple scenes Java FX application. But I want to have the common menu across all scenes. I think using FXML I can create a menu in a scene. But Can I have the same menu across all the scenes even after I navigated to other screen?
If so how is it. Else let me know any alternative for it.

Yes this is possible. I'm using this mechanism in my own application.
What I do first is make an FXML with the menu bar and an AnchorPane who contains the content. This FXML is loaded when the application starts.
I use a Context class (based on the answer of Sergey in this question: Multiple FXML with Controllers, share object) which contains a method ShowContentPane(String url) method:
public void showContentPane(String sURL){
try {
getContentPane().getChildren().clear();
URL url = getClass().getResource(sURL);
//this method returns the AnchorPane pContent
AnchorPane n = (AnchorPane) FXMLLoader.load(url, ResourceBundle.getBundle("src.bundles.bundle", getLocale()));
AnchorPane.setTopAnchor(n, 0.0);
AnchorPane.setBottomAnchor(n, 0.0);
AnchorPane.setLeftAnchor(n, 0.0);
AnchorPane.setRightAnchor(n, 0.0);
getContentPane().getChildren().add(n);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
So what basically happens is:
When the program starts, set content pane in the Context:
#Override
public void initialize(URL url, ResourceBundle rb) {
Context.getInstance().setContentPane(pContent); //pContent is the name of the AnchorPane containing the content
...
}
When a button or menuitem is chosen, I load the FXML in the content Pane:
#FXML
private void handle_FarmerListButton(ActionEvent event) {
Context.getInstance().showContentPane("/GUI/user/ListUser.fxml");
}
Hope this helps :)

Related

Designer messes up location and size of a Custom UserControl child container that is design enabled

When I put this control on to a form, change it's size and location, save it and close the form. After opening it, location and size are not the same, but in ".Designer.cs" it's exactly how I set it.
I can't find a solution to this problem, not even someone mentioning it.
This is a simple example of a custom control I am using:
[Designer(typeof(myControlDesigner1))]
public partial class UserControl1 : UserControl
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[TypeConverter(typeof(Panel))]
[MergableProperty(false)]
public System.Windows.Forms.Panel Panel
{
get
{
return pnlWorkingArea;
}
set
{
pnlWorkingArea = value;
}
}
public UserControl1()
{
InitializeComponent();
}
}
public class myControlDesigner1 : ControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
UserControl1 bc = component as UserControl1;
EnableDesignMode(bc.Panel, "MyPanel");
}
}
Yes I can reproduce your issue now, that is because the panel is inside usercontrol, they are added as a whole to the Form, that means the location of the panel is relative to usercontrol, so if you set the location of the panel is (x, y), then when you reopen the form, the actual location of the panel will be (usercontrol.location.X+x, usercontrol.location.Y+y).
You can find there is not any problem if you set the location of the usercontrol in the form is (0, 0), please have a try.
If you do not want to set the location of the usercontrol is (0, 0), as an alternative solution, you can add the following code in Form_Load event, so the location will be where you set it when you run the form:
private void Form1_Load(object sender, EventArgs e)
{
this.userControl11.Panel.Location = new Point(userControl11.Panel.Location.X - userControl11.Location.X, userControl11.Panel.Location.Y - userControl11.Location.Y);
}

Popup window Label is not getting loaded

1.Trying to display exception message in popup window. Exception message is not appearing.
2.Eg: When i click button a popup window (Second fxml file) is to load with proper exception message in the label
3.Popup window is appearing, But the Label is not loading (Bold one --> ExceptionLabel.setText("Please enter Proper file path")) it says null pointer exception.
4.I am not sure what i missing. Same declared in FX:ID and also in second fxml file linked the main controller. Thanks in advance.
#FXML
public Label ExceptionLabel;
Stage PopupWindow = new Stage();
public void Buttonhandle(ActionEvent event) throws IOException {
try {
if(ESBOutboundFile!=null && OutputFile!=null){
String Output = SBlogpaser.Logpaser(ESBInboundFile,ESBOutboundFile,OutputFile);
System.out.println(Output);
}else{
Window(PopupWindow);
**ExceptionLabel.setText("Please enter Proper file path");**
}
} catch (Exception ex) {
System.out.println(ex);
}
}
public void Window(Stage Popup) throws Exception {
this.Popup=Popup;
final FXMLLoader fxmlLoader = new FXMLLoader();
Parent root= fxmlLoader.load(getClass().getResource("POPUPWindow.fxml"));
Scene scene1 = new Scene(root);
Popup.setScene(scene1);
Popup.show();
}
If i keep the label in "OK" handle button it is getting displayed.
From where are you expecting ExceptionLabel to be instantiated?
Assuming you are pointing the fx:controller attribute of the root of your POPUPWindow.fxml file to the current class, it will just create a new instance of that class, and inject values into that instance. The field ExceptionLabel in the current instance won't be initialized.
You could probably just about make this work by setting the controller of the FXMLLoader to the current object, something like this:
public void window(Stage popup) throws Exception {
this.popup=popup; // why?
final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("POPUPWindow.fxml"));
fxmlLoader.setController(this);
Parent root= fxmlLoader.load();
Scene scene1 = new Scene(root);
popup.setScene(scene1);
popup.show();
}
and then remove the fx:controller attribute from POPUPWindow.fxml.
This seems like a really bad idea, though, because now the current object is acting as a controller for two different FXML files. This will be confusing at best, and under fairly reasonable conditions would produce weird results. It would be much better to write a different controller class for the popup:
public class PopupController {
private final String message ;
#FXML
private Label exceptionLabel ;
public PopupController(String message) {
this.message = message ;
}
public void initialize() {
exceptionLabel.setText(message);
}
}
and then use the window(...) method above, but with
fxmlLoader.setController(new PopupController("Please enter Proper file path"));
Obviously, if you're reusing the window(..) method, you might want to pass the message in as a parameter to that method.

JavaFX "already set as root of another scene" exception when launching modal dialog a second time

I am building a simple JavaFX application that requires me to launch a modal window in front of my main application window. Using the code below, I am able to launch the modal window 1 time and close it. If I attempt to launch it again, I receive:
java.lang.IllegalArgumentException: BorderPane[id=root, styleClass=root]is already set as root of another scene
I am using the Spring Controller/FXML View dependency injection method described here:
http://www.zenjava.com/2012/02/20/porting-first-contact-to-spring/
I am able to programatically create a scene and hide/display a simple dialog without using FXML / Spring Controller injection. This works fine.
I am unable to explain the 'already set as root' exception, as I am creating a new Scene() each time the startButton is clicked. The 1st Scene should have been destroyed whenever the modal window was closed the 1st time.
Relevant files are below.
MainTabPanel.java - The main view of my application. This contains the 'startButton' that is clicked to launch the modal window.
The ActivePresentation Controller/View is injected as:
#Inject private ActivePresentation activePresentation;
Below is the initialize() method that attempts to launch the modal when startButton is clicked.
#FXML
public void initialize()
{
availableReceiversIdColumn.setCellValueFactory(new PropertyValueFactory("id"));
availableReceiversFirmwareVersionColumn.setCellValueFactory(new PropertyValueFactory("firmwareVersion"));
availableReceiversModelColumn.setCellValueFactory(new PropertyValueFactory("model"));
availableReceiversChannelColumn.setCellValueFactory(new PropertyValueFactory("channel"));
ObservableList<String> responseTypes = FXCollections.observableArrayList();
responseTypes.add("Single Response Alpha");
responseTypes.add("Single Response Numeric");
responseTypeChoiceBox.setItems(responseTypes);
startButton.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
presentationResponseService.startPresentation();
activePresentation.populateResponses(null);
activePresentation.populateResults(null);
Scene activePresentationScene = new Scene(activePresentation.getView());
activePresentationScene.getStylesheets().add("styles.css");
stage.setScene(activePresentationScene);
stage.setTitle("Active Presentation");
stage.showAndWait();
}
});
}
The closeButton is defined in the modal dialog as follows.
closeButton.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
presentationResponseService.closePresentation();
Stage stage = (Stage) root.getScene().getWindow();
stage.close();
}
});
The Java based Spring configuration for the ActivePresentation bean and FXML loader are as follows.
#Bean
public ActivePresentation activePresentation()
{
return loadPresenter("/fxml/ActivePresentation.fxml");
}
FXML Loader
private <T> T loadPresenter(String fxmlFile)
{
try
{
FXMLLoader loader = new FXMLLoader();
loader.load(getClass().getResourceAsStream(fxmlFile));
return (T) loader.getController();
} catch (IOException e)
{
throw new RuntimeException(String.format("Unable to load FXML file '%s'", fxmlFile), e);
}
}
I had the issue when I had a pop-up window autowired. it was working the first time but when I close the pop-up window the second time, I was getting this error.
I managed by checking if the pop-up window had already a scene, see the below code:
if (window.getScene() == null) {
Scene scene = new Scene(window);
stage.setScene(scene);
} else {
stage.setScene(window.getScene());
}
Seems, the stage is still under control of JFX (logic : you can try yo open it again).
And you create a stage each time while previous is still alive, so, could you try to add stage.setScene(null) near stage.close().
Or use the same scene or the same stage each time?
I don't see any bug in this part of JFX : node is the same, stages (and scenes in it) are different. So there are 2 ways : use only 1 stage+scene, or create different border pane instances each time.

Extend View and Custom Dialogs

I am working on a game where I am extending the view and performing operations in the class. I need to have a popup in the game which will have 3 buttons inside it. I have managed to have the popup show-up using custom dialogs but when I configure the onClick as follows:
private void popUp() {
Context mContext = getContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_fullimage_dialog);
dialog.setTitle("Cheese Market");
Button one = (Button)findViewById(R.id.firstpack);
one.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cheeseLeft = cheeseLeft + 10;
masterMoveLock = false;
return;
}
});
}
It force closes giving a nullpointerexeption even though it is defined in the custom_fullimage_dialog layout.
Can someone help me figure out how to have the button click detected in this scenario?
Thank you.
Try calling dialog.findViewById instead.
You're setting the contentView for the dialog, but by calling findViewById you're looking for it under your activity's content view.

How to use QLPreviewController in a non-modal way? Why does my code not work?

I have QLPreviewController up and running but I'm using PresentModalViewController() to show the QLPreviewController directly. For reasons beyond explanation, I would like to have my own UIViewController which will create its own view and within that view I would like to use the QLPreviewController. Should be easy I thought, but the code below just does nothing. The QLPreviewControllers ViewDidAppear never gets called. (In my example below, PreviewController inherits from QLPreviewController and encapsulates delegate, preview item and source).
Can somebody explain what is wrong with the code below (besides the fact that it is pointless :-))?
Oh, yeah: in my test scenario, I present the controller below modally. It shows up but witout the preview.
public class OuterPreviewController : UIViewController
{
public OuterPreviewController (QLPreviewControllerDataSource oDataSource) : base()
{
this.oDataSource = oDataSource;
}
private PreviewController oPreviewController;
private QLPreviewControllerDataSource oDataSource;
public override void LoadView ()
{
this.View = new UIView();
this.View.Frame = new RectangleF(0, 0, 500, 500);
this.View.BackgroundColor = UIColor.Red;
}
public override void ViewDidAppear (bool animated)
{
// Code execution comes her. No errors, no issues.
base.ViewDidAppear (animated);
this.oPreviewController = new PreviewController();
this.oPreviewController.DataSource = this.oDataSource;
// Preview controller's view is added but it never shows up.
this.View.AddSubview(this.oPreviewController.View);
this.oPreviewController.View.Frame = this.View.Frame;
this.oPreviewController.View.Center = this.View.Center;
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
return true;
}
}
Found a solution by coincidence today: all ReloadData() on the preview controller and magically it will show its contents.
This allows to add a QLPreviewController to an existing view as a subview and embed a preview. It also gets you rid of the toolbar which contains the open in menu.

Resources