JavaFX 2.0 Layout with MigPane

I have been starting to look at JavaFX 2.0 since I have been doing a lot of GUI work in swing.

The first challenge was layout since it is different from swing. JavaFX comes with a number of Panes (or panels) to layout your display. They are all pretty basic except for GridPane. GridPane reminds me of GridBagLayout, which is difficult to work with.

Fortunately there is a port of MiGLayout called MigPane. Below is a simple example of using MigPane to build a screen that will grow with a window.

You will need to download MigLayout and the MigPane wrapper.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package miglayoutexample;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import org.tbee.javafx.scene.layout.MigPane;
 
public class MigLayoutExample extends Application 
{
    public static void main(String[] args) {
        Application.launch(args);
    }
 
    private Button newBtn;
    private Button prevBtn;
    private Button nextBtn;
    private TextArea textArea;
 
    @Override
    public void start(Stage primaryStage) 
    {
        primaryStage.setTitle("MigPane example");
        newBtn = new Button("New");
        prevBtn = new Button("<<<<");
        nextBtn = new Button(">>>>");
 
        textArea = new TextArea();
 
        MigPane layout = new MigPane(
            "",                         // Layout Constraints
            "[grow]10[shrink 0]4[shrink 0]",  // Column constraints
            "[][200,grow]");            // Row constraint
 
        layout.add(newBtn);
        layout.add(prevBtn);
        layout.add(nextBtn, "wrap");
 
        layout.add(textArea, "grow,span");
 
        Scene scene = new Scene(layout);        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to JavaFX 2.0 Layout with MigPane

  1. Pingback: Java desktop links of the week, November 14 | Jonathan Giles

  2. hsan says:

    if i run i got this error
    package org.tbee.javafx.scene.layout does not exist

Leave a Reply

Your email address will not be published. Required fields are marked *