javaswingjavafxopenjfx

Cannot use miglayout with swing and javafx at the same time


My application needs to start as a javafx application and create some swing jframe istances. I Would like to use MigLayout both for java swing container and javafx panes. The app works fine as long as i use miglayout only in javafx. As i use it for swing too, i get this error:

java.lang.AbstractMethodError: Receiver class net.miginfocom.swing.SwingComponentWrapper does not define or inherit an implementation of the resolved method abstract getContentBias()I of interface net.miginfocom.layout.ComponentWrapper.

And this is my code:

package com.myproj.mavenproject13;

import java.awt.Dimension;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javax.swing.JFrame;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
import org.tbee.javafx.scene.layout.MigPane;


public class HelloWorld extends Application {
    private MigLayout migLayout = new MigLayout("debug, fillx", "", "");
    
    @Override
    public void start(Stage primaryStage) {
        MigPane migPane = new MigPane("debug, fillx", "", "");
        migPane.add(getButton1(), "grow, push, wrap");
        migPane.add(getButton2(), "grow, push");
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(new Scene(migPane, 300, 250));
        primaryStage.show();
    }
    
    private Button getButton1(){
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new javafx.event.EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                System.out.println("asd");
            }
        });
        return btn;
    }
    
    private Button getButton2(){
        Button btn = new Button();
        btn.setText("Create frame");
        btn.setOnAction(new javafx.event.EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                JFrame f = new JFrame();
                f.setLayout(migLayout);
                JTextField jtf = new JTextField("asd");
                f.add(jtf, "growx, pushx");
                f.setTitle("my frame");
                f.setPreferredSize(new Dimension(400, 400));
                f.setVisible(true);
            }
        });
        return btn;
    }
}

The main class is:

package com.proj.mavenproject13;

import javafx.application.Application;

public class Main {

    public static void main(String[] args) {
        Application.launch(HelloWorld.class, args);
    }

}

And this is my project's pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.proj</groupId>
    <artifactId>mavenproject13</artifactId>
    <version>1.0.0-RELEASE</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.miglayout</groupId>
            <artifactId>miglayout-javafx</artifactId>
            <version>11.0</version>
        </dependency>
        <dependency>
            <groupId>com.miglayout</groupId>
            <artifactId>miglayout</artifactId>
            <version>3.7.4</version>
        </dependency>
    </dependencies>
</project>

Solution

  • I guess you have to use at least the same version for Swing and JavaFX otherwise conflicts are to be expected because both implementations share some code. You should also use the latest versions so I think this should be ok.

    <dependency>
        <groupId>com.miglayout</groupId>
        <artifactId>miglayout-javafx</artifactId>
        <version>11.0</version>
    </dependency>
    <dependency>
        <groupId>com.miglayout</groupId>
        <artifactId>miglayout-swing</artifactId>
        <version>11.0</version>
    </dependency>