0

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>
BabaNew
  • 884
  • 1
  • 13
  • 27
  • 1
    I have to frame challenge this. Why do you need Swing? Why do you need MigLayout? Why is [GridPane](https://openjfx.io/javadoc/18/javafx.graphics/javafx/scene/layout/GridPane.html) not sufficient? – VGR Jun 03 '22 at 15:55
  • 1
    Can you [edit] your question and post the entire stack trace? – Abra Jun 03 '22 at 16:12
  • 2
    Not sure if it makes a difference (probably not) but you **must not** create and show a `JFrame` from a thread other than the AWT Event Dispatch Thread. – James_D Jun 03 '22 at 16:23
  • 2
    I see `miglayout-javafx`, but not `miglayout-swing`. Amplifying on @James_D's aside, a program that ignores the [threading rule](https://stackoverflow.com/a/7158505/230513) "may _appear_ to work correctly, only to fail mysteriously in a different environment." – trashgod Jun 03 '22 at 16:47
  • @VGR, you really can't compare GridPane with MigLayout. It is much more than just a grid pane and, if you are used to it, much easier to use. – mipa Jun 03 '22 at 18:45

1 Answers1

3

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>
mipa
  • 10,369
  • 2
  • 16
  • 35
  • Wow, having https://mvnrepository.com/artifact/com.miglayout/miglayout as a reference i thought that 3.7.4 was the latest. I too wanted to have them at the same version but i thought that was not possible. Can you post the link of the repository where all swing miglayout versions are shown? Many thanks – BabaNew Jun 04 '22 at 09:46
  • It's just https://mvnrepository.com/artifact/com.miglayout/miglayout-swing and https://mvnrepository.com/artifact/com.miglayout/miglayout-javafx or do you mean something else? – mipa Jun 04 '22 at 11:23
  • Thanks i meant that. Why don't they delete this url mvnrepository.com/artifact/com.miglayout/miglayout it tricked me – BabaNew Jun 04 '22 at 11:34
  • 1
    @BabaNew the old public artifacts can never be deleted or old software that uses them would be unable to be built anymore. You just have to be careful that you get the right versions. For widely used things like [hibernate](https://mvnrepository.com/artifact/org.hibernate/hibernate) or Java EE or Apache commons sometimes the public indices like mvnrepository provide some warning about an obsolete package change, but sometimes they don’t. – jewelsea Jun 05 '22 at 05:14