This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

How to read an fxml file inside a UIInternalFrame?

General questions regarding the development with JavaFX UI for JVx.

How to read an fxml file inside a UIInternalFrame?

Postby wellspinto » Fri Oct 06, 2017 5:20 pm

Hi!

I am a beginner in both Java (FX) and SIB libraries.
I created an MDI application and inside it has a UIInternalFrame.
I would like to know if it has a way to read a screen created by InseneBuilder (FXML) within this object?

Tks,

My Code:

package entrada;

import javafx.fxml.FXMLLoader;
import javafx.scene.layout.AnchorPane;

import javax.rad.genui.UIColor;
import javax.rad.genui.UIImage;
import javax.rad.genui.UIResource;
import javax.rad.genui.component.UIIcon;
import javax.rad.genui.container.UIDesktopPanel;
import javax.rad.genui.container.UIFrame;
import javax.rad.genui.container.UIInternalFrame;
import javax.rad.genui.container.UIPanel;
import javax.rad.genui.layout.UIBorderLayout;
import javax.rad.genui.layout.UIFormLayout;
import javax.rad.genui.menu.UICheckBoxMenuItem;
import javax.rad.genui.menu.UIMenu;
import javax.rad.genui.menu.UIMenuBar;
import javax.rad.genui.menu.UIMenuItem;
import javax.rad.ui.IAlignmentConstants;
import javax.rad.ui.IContainer;
import javax.rad.ui.layout.IFormLayout;

public class LoginFrame extends UIFrame {
public LoginFrame() {
super();

// The layout for the header panel.
UIFormLayout headerPanelLayout = new UIFormLayout();

// The panel used for the top bar.
UIPanel headerPanel = new UIPanel();
//headerPanel.setBackgroundImage(UIImage.getImage("/entrada/images/20101020_Sheep_shepherd_at_Vistonida_lake_Glikoneri_Rhodope_Prefecture_Thrace_Greece.jpg"));
headerPanel.setLayout(headerPanelLayout);
headerPanel.setBackground(UIColor.white);
headerPanel.add(new UIIcon(new UIImage("/entrada/images/jvx.png")), headerPanelLayout.getConstraints(0, 0));

addBorder(headerPanel, IAlignmentConstants.ALIGN_STRETCH, IAlignmentConstants.ALIGN_BOTTOM);

// Setting up the Frame.
setLayout(new UIBorderLayout());
setPreferredSize(1024, 768);
setTitle("jAdministrador");
// Icone do Programa
//setIconImage(UIImage.getImage("/entrada/images/20101020_Sheep_shepherd_at_Vistonida_lake_Glikoneri_Rhodope_Prefecture_Thrace_Greece.jpg"));

add(headerPanel, UIBorderLayout.NORTH);

// Menu do Sistema
//UIMenuBar menuBar = jAdm_Menu();
//setMenuBar(menuBar);

// Login do Sistema
UIPanel content = Login();
add(content); //,UIBorderLayout.CENTER);

}

private UIPanel Login() {
AnchorPane root = null;
try {
root = FXMLLoader.load(UIResource.class.getResource("/entrada/Login2.fxml"));
} catch (Exception e) {e.printStackTrace();}

UIDesktopPanel desktopPanel = new UIDesktopPanel();

UIInternalFrame internalFrame = new UIInternalFrame(desktopPanel);
internalFrame.setLayout(new UIBorderLayout());
internalFrame.setPreferredSize(400, 300);
internalFrame.setModal(true);
internalFrame.setResizable(false);
internalFrame.add(root);

internalFrame.pack();
internalFrame.setVisible(true);

UIPanel content = new UIPanel();
content.setLayout(new UIBorderLayout());
content.add(desktopPanel, UIBorderLayout.CENTER);

// Ajustar Tamanho do Desktop
//desktopPanel.setPreferredSize(content.getSize().getWidth(),content.getSize().getHeight());

// BackGround do DeskTop
content.setBackgroundImage(UIImage.getImage("/entrada/images/20101020_Sheep_shepherd_at_Vistonida_lake_Glikoneri_Rhodope_Prefecture_Thrace_Greece.jpg"));

return content;
}

private static UIMenuBar jAdm_Menu() {
UIMenu namesMenu = new UIMenu("Names");
namesMenu.add(new UIMenuItem("Rick"));
namesMenu.add(new UIMenuItem("John"));
namesMenu.add(new UIMenuItem("Gonzo"));
namesMenu.addSeparator();
namesMenu.add(new UIMenuItem("Martin"));
namesMenu.add(new UIMenuItem("Mike"));

UIMenu alabamaMenu = new UIMenu("Alabama");
alabamaMenu.add(new UIMenuItem("Birmingham"));
alabamaMenu.add(new UIMenuItem("Oxford"));
alabamaMenu.add(new UIMenuItem("Aliceville"));

UIMenu kansasMenu = new UIMenu("Kansas");
kansasMenu.add(new UIMenuItem("Hutchinson"));
kansasMenu.add(new UIMenuItem("Wichita"));
kansasMenu.add(new UIMenuItem("Topeka"));
kansasMenu.add(new UIMenuItem("Hays"));

UIMenu checkBoxMenu = new UIMenu("CheckBox");
checkBoxMenu.add(new UICheckBoxMenuItem("Check it!"));
checkBoxMenu.add(new UICheckBoxMenuItem("With image!"));
//((UICheckBoxMenuItem)checkBoxMenu.getComponent(1)).setPressedImage(getImage("emblem-favorite.png"));

UIMenu southDakotaMenu = new UIMenu("South Dakota (empty)");

UIMenu placesMenu = new UIMenu("Places");
placesMenu.add(alabamaMenu);
placesMenu.add(kansasMenu);
placesMenu.add(southDakotaMenu);

UIMenuBar menuBar = new UIMenuBar();
menuBar.add(namesMenu);
menuBar.add(placesMenu);
menuBar.add(checkBoxMenu);

return menuBar;
}

private static void addBorder(IContainer pContainer, int pHorizontalAlignment, int pVerticalAlignment) {
UIFormLayout layout = (UIFormLayout)pContainer.getLayout();
IFormLayout.IConstraints constraints = layout.getConstraints(layout.getTopAnchor(), layout.getLeftAnchor(), layout.getBottomAnchor(), layout.getRightAnchor());

UIIcon border = new UIIcon(UIImage.getImage("/images/border-pixel.png"));
border.setHorizontalAlignment(pHorizontalAlignment);
border.setVerticalAlignment(pVerticalAlignment);

pContainer.add(border, constraints);
}

}
wellspinto
 
Posts: 10
Joined: Fri Oct 06, 2017 4:59 pm

Re: How to read an fxml file inside a UIInternalFrame?

Postby Support@SIB » Fri Oct 06, 2017 5:47 pm

JVx' UI is an UI abstraction but it doesn't hide the underlying technology. You could add the root node as new UICustomContainer(root).

An UICustomContainer or an UIComponent wraps the given technology dependent resource. An example with Swing is here
User avatar
Support@SIB
 
Posts: 221
Joined: Mon Sep 28, 2009 1:56 pm

Re: How to read an fxml file inside a UIInternalFrame?

Postby Support@SIB » Fri Oct 06, 2017 5:50 pm

It's always possible to directly access the technology dependent resource, e.g. frame.getResource() and cast it to e.g. FXInternalWindow.

But be careful, if you use the underlying technology directly, it's not easy to use another UI technology without problems!
User avatar
Support@SIB
 
Posts: 221
Joined: Mon Sep 28, 2009 1:56 pm

Re: How to read an fxml file inside a UIInternalFrame?

Postby rzenz » Mon Oct 09, 2017 10:05 am

Just to be clear, what you're looking for is the UICustomComponent, which allows to wrap native components into the UI layer. See the blog post about creating custom components, to be exact the section "Wrapping custom components with UICustomComponent" will interest you.

So what you actually want to do is this:

Code: Select all
    AnchorPane root = FXMLLoader.load(resource);
    UICustomComponent wrappedRoot = new UICustomComponent(root);
    interalFrame.add(wrappedRoot, UIBorderLayout.CENTER);


Also if you are a beginner with JVx, I'd suggest to read through the whole JVx Reference series on the blog, or at least the post about how the UI layer works and also have a look at the JVx FirstApp.
User avatar
rzenz
 
Posts: 28
Joined: Mon Dec 12, 2016 1:40 pm
Location: Vienna, Austria

Re: How to read an fxml file inside a UIInternalFrame?

Postby wellspinto » Tue Oct 17, 2017 5:46 pm

Hi!
In my code, how do I in the fxml controller on the close button close the UIInternalFrame?
In Login2.fxml have:
UserName, Password, button Enter and Button Close.

AnchorPane root = null;
try {
root = FXMLLoader.load(getClass().getResource("/entrada/Login2.fxml"));
} catch (Exception e) {e.printStackTrace();}
UICustomComponent wrappedRoot = new UICustomComponent(root);

UIDesktopPanel desktopPanel = new UIDesktopPanel();

UIInternalFrame internalFrame = new UIInternalFrame(desktopPanel);
internalFrame.setLayout(new UIBorderLayout());
internalFrame.setPreferredSize(565, 289);
internalFrame.setModal(true);
internalFrame.setResizable(false);
internalFrame.add(wrappedRoot, UIBorderLayout.CENTER);

internalFrame.pack();
internalFrame.setVisible(true);

UIPanel content = new UIPanel();
UIBorderLayout uiBorderLayout = new UIBorderLayout();
content.setLayout(uiBorderLayout);
content.add(desktopPanel, UIBorderLayout.CENTER);

// Ajustar Tamanho do Desktop
//desktopPanel.setPreferredSize(content.getSize().getWidth(),content.getSize().getHeight());

// BackGround do DeskTop
UIImage uiImage = new UIImage("/Figuras/Imagens/fundo.jpg");
content.setBackgroundImage(uiImage);
wellspinto
 
Posts: 10
Joined: Fri Oct 06, 2017 4:59 pm

Re: How to read an fxml file inside a UIInternalFrame?

Postby rzenz » Wed Oct 18, 2017 11:00 am

How to close the (internal) frame is easy enough, simply set its visibility to false, or call dispose.

How to get it do that from inside a JavaFX component on the other hand, is a little bit more involved. Either you create a controller class which handles the button event, or you wire the event up manually.

You could set a controller, for example by using the current class which you are in. Assume the following FXML file:

Code: Select all
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.web.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <center>
        <Button id="button" mnemonicParsing="false" text="Button" onAction="#handleButtonAction"/>
    </center>
</BorderPane>


And this code in the JVx project:

Code: Select all
public class FileLoadedFrame extends UIFrame
{
    public FileLoadedFrame()
    {
        super();
       
        setLayout(new UIBorderLayout());
        add(loadContent(), UIBorderLayout.CENTER);
    }
   
    @FXML
    protected void handleButtonAction(ActionEvent pActionEvent)
    {
        System.out.println("Button pressed.");
    }
   
    protected IComponent loadContent()
    {
        FXMLLoader loader = new FXMLLoader(FileLoadedFrame.class.getResource("test.fxml"));
        // Set this instance as controller before loading the actual document
        // so that the specified method can be executed on button press.
        loader.setController(this);
       
        try
        {
            loader.load();
        }
        catch (Exception e)
        {
            // TODO Handle the exception.
            e.printStackTrace();
           
            return null;
        }
       
        // We need to assign it here once to make sure that it has the correct
        // type. UICustomComponent does have two constructors, one with
        // IComponent and Object, to avoid undefined behavior we have to be
        // explicit about the type of the parameter. That is needed because
        // getRoot() returns the type that is expected, which would be undefined
        // behavior if we hand it directly to the constructor.
        BorderPane root = loader.getRoot();
       
        return new UICustomComponent(root);
    }
   
}


By setting the class as controller, the button is able to call the handleButtonAction method.

The second way is to manually wire the event against the button, if we assume a nearly identical FXML file from above, just without the onAction property:

Code: Select all
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.web.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <center>
        <Button id="button" mnemonicParsing="false" text="Button"/>
    </center>
</BorderPane>


We can lookup the button and manually set its action event:

Code: Select all
BorderPane root = FXMLLoader.load(this.getClass().getResource("test.fxml")
Button button = ((Button)root.lookup("#button"));
button.setOnAction(this::handleButtonAction);


Which can become quite tedious when having a lot of events, though.
User avatar
rzenz
 
Posts: 28
Joined: Mon Dec 12, 2016 1:40 pm
Location: Vienna, Austria


Return to Development