Tuesday, November 15, 2016

Create Beautiful Dialog with JFoenix in JavaFX

JFoenix is designed to get beautiful material design in JavaFX application. Download the JFoenix from here. One thing here to remember that JFoenix Dialog is designed to be displayed over a StackPane. That means you must have a StackPane as a Parent node.

Create a class for your Dialog.

public class OK_Message extends JFXDialog {
    
    private StackPane Container;
    private JFXDialogLayout Content;
    String DialogText;
    String Headline;
    
    JFXButton  btn;
    
    public OK_Message(
            StackPane Container,
            String Headline, 
            String DialogText
    ){
        this.Container = Container;
        this.DialogText = DialogText;
        this.Headline = Headline;
    } 
 
Now Create your dialog content.  First of all create headline of your dialog. 
 
 private Label HeadLine(){
        Label l = new Label(Headline);
        l.setFont(new Font(18));
        return l;
    }
 
Create Main text or the message.
 
 private GridPane Body(){
        Label l = new Label(DialogText);
        l.setFont(new Font(14));
        
        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(8);
        GridPane.setConstraints(l, 0, 0, 1, 1, 
HPos.LEFT, VPos.CENTER, Priority.ALWAYS, 
Priority.ALWAYS, new Insets(0, 0, 0, 0));
        grid.getChildren().addAll(l);
        return grid;
    } 
 
And create a button and add action, so that when user clicks on the button, your dialog will go away.. 
 
 private JFXButton getButton(){
    btn = new JFXButton("OK");
    btn.setOnAction((ActionEvent event) -> {
        CloseDialog();
    });
    btn.setStyle("-fx-background-color:#006666");
    return btn;
    }
 
 private void CloseDialog(){
    close();
    }
 
 
Finally create the dialog layout.
private JFXDialogLayout getDialogContent(){
        Content = new JFXDialogLayout();
        Content.setHeading(HeadLine());
        Content.setBody(Body())
        Content.setActions(getButton());
        return Content;
    } 
Now your dialog is ready. Create a public method to use the dialog whenever you need.
 
public void ShowDialog(){
        setDialogContainer(Container);
        setContent(getDialogContent());
        setTransitionType(JFXDialog.DialogTransition.RIGHT);
        show();      
    } 


Now use this dialog whenever you need to show a message.

Dialog.OK_Message  message = new Dialog.OK_Message((StackPane)app_setup.getParent(),
                "Message", 
                "Changes will be affected after restart.");
    message.ShowDialog();

Tuesday, May 6, 2014

This is not an expert's blog. Humbly saying, I am a learner of Java. I'll just share some experience with java. May be, someone can find a way.