Adding a custom Java Swing component in the GUI Designer

Answered

I've searched a lot, and apparently there are no tutorials for doing this in IntelliJ, but how do I create a custom component and use it in the GUI Designer form?

I want to create a custom JPanel with a gradient background and use it like I would use a regular JPanel, but I can't, I'm running on some problem

public class PanelMenu extends JPanel {
    
    public PanelMenu() {
        super();
        setOpaque(false);
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        GradientPaint gradientPaint = new GradientPaint(0, 0, Color.decode("1CB5E0"), 0, getHeight(), Color.decode("#000046"));
        g2.setPaint(gradientPaint);
        g2.fillRoundRect(0, 0, getWidth(), getHeight(), 15, 15);
        g2.fillRect(getWidth() - 20, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }

}

I create my custom class, and then add it to a custom group in the palette.

It does get added to the component tree, but it doesn't work like a JPanel and I can't do anything with it, and instead, it ends up breaking IntelliJ.

 

1
9 comments

I have the same problem

1

Make sure this class is built with the JDK version not higher than the JetBrains Runtime used to run IntelliJ IDEA (target version).

If your project targets JDK 18 or 19, IDE running on JDK 17 will not be able to load such class.

Change the project JDK target levels, see https://stackoverflow.com/a/12900859/104891 for the relevant screenshots.

0

I'm using JDK15, I even tried lowering the target levels for the project to JDK 11 and it didn't work.

0

Your class code throws exception which is causing the problem, check the logs: https://intellij-support.jetbrains.com/hc/en-us/articles/207241085.

Fix this issue and it should work fine.

2022-11-14 07:47:08,844 [ 804181] SEVERE - #c.i.i.p.PluginManager - For input string: "1CB5E0"
java.lang.NumberFormatException: For input string: "1CB5E0"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
    at java.base/java.lang.Integer.parseInt(Integer.java:668)
    at java.base/java.lang.Integer.valueOf(Integer.java:973)
    at java.base/java.lang.Integer.decode(Integer.java:1458)
    at java.desktop/java.awt.Color.decode(Color.java:731)
    at hello.PanelMenu.paintComponent(PanelMenu.java:17)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1122)
    at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:955)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1131)
    at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:955)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1131)
    at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:955)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1131)
    at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:955)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1131)
    at java.desktop/javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
    at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:955)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1131)
    at java.desktop/javax.swing.JViewport.paint(JViewport.java:736)
    at com.intellij.ui.components.JBViewport.paint(JBViewport.java:235)
    at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5319)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedFPScales(RepaintManager.java:1732)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1641)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1580)
    at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1347)
    at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5267)
    at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5077)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:882)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:865)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:865)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:838)
    at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:787)
    at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1909)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:779)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:730)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:724)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:749)
    at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:918)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:766)
    at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$6(IdeEventQueue.java:450)
    at com.intellij.openapi.progress.impl.CoreProgressManager.computePrioritized(CoreProgressManager.java:791)
    at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$7(IdeEventQueue.java:449)
    at com.intellij.openapi.application.TransactionGuardImpl.performActivity(TransactionGuardImpl.java:105)
    at com.intellij.ide.IdeEventQueue.performActivity(IdeEventQueue.java:624)
    at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$8(IdeEventQueue.java:447)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:488)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:207)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:92)
0

Thank you. I fixed the issue, and tried using the custom JPanel manually in a JFrame, and it works, but now when I drag and drop the custom JPanel in the GUI Builder, nothing happens.

0

Check logs for any potential exceptions.

0

Hmmm, all I'm getting is 

 

2022-11-14 14:56:32,725 [16449277]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@5981cdfa 
2022-11-14 14:56:32,738 [16449290]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@2d160a23 
2022-11-14 14:56:32,745 [16449297]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@307449fe 
2022-11-14 14:56:32,751 [16449303]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@2830f913 
2022-11-14 14:56:32,758 [16449310]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@94cd2a 
2022-11-14 14:56:32,764 [16449316]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@4f1d349e 
2022-11-14 14:56:32,771 [16449323]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@116a020b 
2022-11-14 14:56:32,778 [16449330]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@285b593 
2022-11-14 14:56:32,784 [16449336]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@7f912539 
2022-11-14 14:56:32,791 [16449343]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@221ddb00 
2022-11-14 14:56:32,799 [16449351]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@3a025649 
2022-11-14 14:56:32,806 [16449358]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@1cfd51bc 
2022-11-14 14:56:32,822 [16449374]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@40038585 
2022-11-14 14:56:32,831 [16449383]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@162022c9 
2022-11-14 14:56:32,836 [16449388]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@58ed7398 
2022-11-14 14:56:32,844 [16449396]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation com.intellij.uiDesigner.radComponents.RadXYLayoutManager$MyDropLocation@20d0ee39 
2022-11-14 14:56:32,849 [16449401]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridInsertLocation(ColumnAfter, row=0, col=0) 
2022-11-14 14:56:32,866 [16449418]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:32,884 [16449436]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:32,898 [16449450]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:32,906 [16449458]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:32,920 [16449472]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:32,933 [16449485]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:32,942 [16449494]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:32,954 [16449506]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:32,969 [16449521]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:32,982 [16449534]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:32,992 [16449544]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:33,002 [16449554]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:33,010 [16449562]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:33,154 [16449706]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:33,183 [16449735]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:33,192 [16449744]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 
2022-11-14 14:56:33,259 [16449811]   INFO - ignSurface.GridInsertProcessor - GridInsertProcessor.processDragEvent(): dropLocation GridDropLocation(row=0,col=0) 

0

Feel free to report a bug at https://youtrack.jetbrains.com/newIssue?project=IDEA and attach a sample project to reproduce this issue.

0

Please sign in to leave a comment.