The JButton
extends AbstractButton
which has these default variables.
// Display properties
private boolean paintBorder = true;
private boolean contentAreaFilled = true;
I'd like said JButton
created via the palette to have those as false
.
I tried creating a new class and copied the code from AbstractButton
(which would've been extended by a new type of JButton
) and only changed those values, but some of the methods used inside are private and can not be accessed from a different package.
Even if I could access them from a different package, I'd need to also create a new custom Swing component - is there no simple way to do this?
Why am I trying to do this? I'll be creating a lot of buttons where those values will be false and I'd like to avoid having to set them via their respective methods for each one, if possible.
Edit 1:
IconButton.java - As suggested by Abra
import javax.swing.*;
public class IconButton extends JButton {
public IconButton() {
setContentAreaFilled(false);
setBorderPainted(false);
}
}
Adding the button programmatically via the code below, it works, but trying to add it via the Palette by drag-and-drop into the JPanel
(Card Layout
), doesn't work. Nothing happens, no error or anything.
MyForm.java
import components.IconButton;
import components.CustomIkon;
import com.formdev.flatlaf.FlatDarkLaf;
import org.kordamp.ikonli.materialdesign2.MaterialDesignA;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MyForm extends JFrame {
private JPanel mainPanel;
private JButton normalButton;
private JPanel testPanel;
public MyForm(String title) {
super(title);
CustomIkon icon = CustomIkon.of(MaterialDesignA.ACCOUNT);
normalButton.setIcon(icon);
normalButton.setBorderPainted(false);
normalButton.setContentAreaFilled(false);
IconButton iconButton = new IconButton();
iconButton.setIcon(icon);
testPanel.add(iconButton);
this.setContentPane(mainPanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack();
}
public static void main(String[] args) {
FlatDarkLaf.setup();
JFrame frame = new MyForm("Hello World");
frame.setVisible(true);
}
}
I found a similar problem, but the answer there does not help. The JDK used by the project and the bytecode version are the same (in this case, version 16) in IntelliJ.
I'm investigating the log as suggested in the answer below the accepted one.
Edit 2:
I found this error in the log after the application was ran:
2021-06-27 18:39:12,264 [2711861] ERROR - llij.ide.plugins.PluginManager - IconButton has been compiled by a more recent version of the Java Runtime (class file version 60.0), this version of the Java Runtime only recognizes class file versions up to 55.0
2021-06-27 18:39:12,267 [2711864] ERROR - llij.ide.plugins.PluginManager - IntelliJ IDEA 2021.1.2 Build #IC-211.7442.40
2021-06-27 18:39:12,268 [2711865] ERROR - llij.ide.plugins.PluginManager - JDK: 11.0.11; VM: Dynamic Code Evolution 64-Bit Server VM; Vendor: JetBrains s.r.o.
From what I understand, that means I'm compiling the application with a newer version than I am running it. Though, I don't see what that has to do with not being able to add the button from the palette to the form.
I'll keep investigating.
Edit 3:
Project Structure
Project
a) Project SDK: openjdk-16
b) Project language level: SDK default (16 - Records, patterns, local enums and interfaces)
Modules
a) Sources -> Language level: 16 - Records, patterns, local enums and interfaces
b) Dependencies -> Module SDK: Project SDK openjdk-16
SDKs -> openjdk-16
Settings
Java Compiler:
a) Use compiler: Javac
b) Project bytecode version: Same as language level
c) Per-module bytecode version -> Target bytecode version: 16
Java Version:
[username]:~/.jdks/openjdk-16.0.1/bin$ ./java -version
openjdk version "16.0.1" 2021-04-20
OpenJDK Runtime Environment (build 16.0.1+9-24)
OpenJDK 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)
Edit 4:
As suggested in some of the answers here, I went and checked the JDK that Maven uses (File -> Settings -> Build, Execution, Deployment -> Build Tools -> Maven -> Importing -> JDK for importer) and it was JRE version 11. I changed it to use the Project SDK
.
After that, I invalidated the caches (File -> Invalidate Caches), deleted the target
folder and ran my application again.
The error in the log mentioned in Edit 2 has disappeared.
Unfortunately, I still can't add the bloody IconButton
from the palette to the JPanel
.
Edit 5:
It seems the error mentioned in Edit 2 keeps appearing every time I try to drag-and-drop the IconButton
from the palette to the JPanel
, but not when I compile my application.
Removing and creating the component again doesn't seem to help.