Issues Adding Custom Swing Classes to IntelliJ GUI Designer

  Kiến thức lập trình

I have a project in IntelliJ IDEA where I’ve created a custom Swing component. I’d like to use this custom component in the GUI Designer, but I’m encountering issues when trying to drag and drop the components into the designer.

It would be much appreciated if you could provide an example of how to add such a custom Swing component, say MyButton extending Java Swing’s JButton and use it in the GUI Designer without errors.

Further specific background, just in case it is important:

  • the gui is supposed to be part of a maven project
  • I am using JDK 19.0.2

Here are the steps I’ve taken so far:

  1. I’ve created a custom Swing class named MyButton that inherits from JButton.
    Code (trying to create a minimal reproducable example):
package view.my_swing_elements;

import javax.swing.*;

public class MyButton extends JButton {
}

Folder structure:

Folder structure

  1. I’ve added MyButton to the palette in the GUI Designer by creating a group named “MySwing” and adding MyButton to this group.

    Settings of MyButton added to MySwing

(I’ve also tried ticking the bottom three boxes with no success)

  1. However, I’m still receiving an error message when dragging MyButton into the GUI Designer. The error message states: Class "view.my_swing_elements.MyButton" not found, even though the class definitely exists and has been compiled.

  2. Weirdly, if I rebuild the whole maven project with mvn clean install, I can’t even drag the MyButton from the palette to the component tree / preview window.

LEAVE A COMMENT