Jfxtras8 align controls in windows

277 views
Skip to first unread message

Dadi Boonz

unread,
Jan 26, 2014, 1:05:54 AM1/26/14
to jfxtra...@googlegroups.com

I have recently shifted from c# to try Javafx2. I am also new to this forum. I have been stuck trying to implement internal frames in Javafx. I stumbled upon this link: Internal Frames in JavaFX I have managed to add jfxtras 8 jar file to my project as well as in scene builder 2. However, am stuck in aligning the controls on the window.

This is the code for the fxml file:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import jfxtras.labs.scene.control.window.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="trials.MamaCont">
<children><Window fx:id="wini" layoutX="122.0" layoutY="105.0" prefHeight="190.0" prefWidth="313.0" title="Window" />
</children></AnchorPane>


This is the code for the controller class:


package trials;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import jfxtras.labs.scene.control.window.CloseIcon;
import jfxtras.labs.scene.control.window.MinimizeIcon;
import jfxtras.labs.scene.control.window.Window;

/**
 * FXML Controller class
 *
 * @author smoothie
 */
public class MamaCont implements Initializable {

    /**
     * Initializes the controller class.
     */


    /*@FXML
    private Button pb;

    @FXML
    private Label lb;*/

    @FXML
    private Window wini;

    /*@FXML
    void pressed(ActionEvent event) {
         lb.setText("Gotcha!!!....");
    }*/

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        wini.getLeftIcons().add(new CloseIcon(wini));
        wini.getRightIcons().add(new MinimizeIcon(wini));
        //wini.setVisible(false);

        Button butt = new Button("Enter");
        /*butt.setLayoutX(100);
        butt.setLayoutY(100);*/

        Label lab = new Label();
        /*lab.setLayoutX(261);
        lab.setLayoutY(192);*/



        butt.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                lab.setText("I've been pressed!!!");
            }
        });

        wini.getContentPane().getChildren().add(butt);
        wini.getContentPane().getChildren().add(lab);
    }    

}


This is the screen when it is loaded:


This is the screen when the button has been pressed:



As you can see, the message from the label appears over the button.

However, this is not I wanted to achieve.

Now, does anyone know how I can be able to align the label so that its text appears below or above the button? 

Has anyone been able to arrange javafx controls in the window? 

After I added the jfxtras8 jar file to scene builder 2, I was able to access the controls of jfxtras in scene builder. However, when I tried to add controls (eg button) to the window it aligned itself as a child of the anchor pane rather than the child of the window.

Is it possible to add controls to the window from the scenebuilder and have the window as their container?

Thank you... 




tbeernot

unread,
Jan 26, 2014, 3:40:31 AM1/26/14
to jfxtra...@googlegroups.com
In Java UI's the positioning of elements is normally handled by a layout manager, in JavaFX these are classes extending from Pane. There are several options to choose from, there are the build in ones (http://docs.oracle.com/javafx/2/layout/builtin_layouts.htm) or 3rd party (http://www.miglayout.com/).

In your case the wini.getContentPane() returns a Pane, and the behavior it seems to be a StackPane (http://docs.oracle.com/javafx/2/api/javafx/scene/layout/StackPane.html) which neatly stacks all UI elements on top of each other. What you want is VBox which will layout all UI elements vertically.

Try adding this line in the initialize
    wini.setContentPane(new VBox());
before the
    wini.getContentPane().getChildren().add(...);
--
You received this message because you are subscribed to the Google Groups "JFXtras Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jfxtras-user...@googlegroups.com.
To post to this group, send email to jfxtra...@googlegroups.com.
Visit this group at http://groups.google.com/group/jfxtras-users.
For more options, visit https://groups.google.com/groups/opt_out.

Dadi Boonz

unread,
Jan 28, 2014, 4:02:15 PM1/28/14
to jfxtra...@googlegroups.com
Hey,
Thanks for your reply.
It was helpful...
I found a work around the issue.
What I did was to build controls in a fxml file.
Here is the code:


<?xml version="1.0" encoding="UTF-8"?>

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

<AnchorPane id="AnchorPane" prefHeight="100.0" prefWidth="250.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="trials.MamaCont">
<children><Button fx:id="pb" layoutX="84.0" layoutY="60.0" mnemonicParsing="false" onAction="#clicked" text="Click me!!!" /><Label fx:id="lb" layoutX="89.0" layoutY="26.0" prefHeight="16.0" prefWidth="71.0" />
</children></AnchorPane>

The controller class for the window.

package trials;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

/**
 * FXML Controller class
 *
 * @author vijiguys
 */
public class MamaCont implements Initializable{

    /**
     * Initializes the controller class.
     */
        
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }
        
    @FXML
    private Button pb;

    @FXML
    private Label lb;

    @FXML
    void clicked(ActionEvent event) {
        lb.setText("Pewa!!!");
    }    
    
}

Then I injected the fxml file into a window with the code below:


package trials;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javax.swing.JOptionPane;
import jfxtras.labs.scene.control.window.*;

/**
 *
 * @author vijiguys
 */
public class TrialsCont implements Initializable {
    
    @FXML
    private Label label;
    
    @FXML
    private void handleButtonAction(ActionEvent event) throws IOException {
        
        label.setText("You clicked me...");
        
        try{
                        
            Stage weyda = new Stage();

            Parent balou = FXMLLoader.load(getClass().getResource("mamaGUI.fxml"));
            //FXMLLoader balou= new FXMLLoader(getClass().getResource("mamaGUI.fxml"));
     
            Group kitu = new Group();
            //stage.setResizable(false);


            weyda.setScene(new Scene(kitu, 500, 400));

            Window w = new Window("My Window");

            // set the window position to 10,10 (coordinates inside canvas)
            w.setLayoutX(10);
            w.setLayoutY(10);

            // define the initial window size
            w.setPrefSize(300, 150);
            w.setResizableWindow(false);

             // either to the left
            w.getRightIcons().add(new CloseIcon(w));

            // add some content
            w.getContentPane().getChildren().add(balou);

            // add the window to the canvas
            kitu.getChildren().add(w);

            weyda.setTitle("Mama Screen");
            weyda.show();
            
            //Hides the current screen
            //((Node)(event.getSource())).getScene().getWindow().hide(); 
        }
        catch(Exception x){
            
           JOptionPane.showMessageDialog(null, x.toString(), "Exception",JOptionPane.ERROR_MESSAGE);
        }
        
        /*Parent root = FXMLLoader.load(getClass().getResource("mamaGUI.fxml"));
        
        Stage stage = new Stage();
        stage.setTitle("Mama Window");
        Scene scene = new Scene(root);
        
        stage.setScene(scene);
        stage.show();*/
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
    
}


This is how it runs:

This is the first screen....

This is the second screen the button is pressed. The second scene is opened in a new stage with the fxml loaded in the window.


These are the two screens side by side.



Finally, this is what happens when one presses the button on the window screen....


My original question has been fully answered.

If you can permit me to deviate slightly, does anyone know how I can open a new fxml scene on the same stage as the previous fxml scene? I would like to open the jfxtra window on the same stage as the first screen using the first screen's controller but am stuck...

Thanks...





Reply all
Reply to author
Forward
0 new messages