Help with creating GUI for plugin
Answered
I am trying to create a dialog/settings window for my plugin. To test it out, i tried copying the code at https://plugins.jetbrains.com/docs/intellij/dialog-wrapper.html#example
So i have a class SampleDialogWrapper.java with the following code:
import com.intellij.openapi.ui.DialogWrapper;
import javax.annotation.Nullable;
import javax.swing.*;
import java.awt.*;
public class SampleDialogWrapper extends DialogWrapper {
public SampleDialogWrapper() {
super(true); // use current window as parent
setTitle("Test DialogWrapper");
init();
}
@Nullable
@Override
protected JComponent createCenterPanel() {
JPanel dialogPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("testing");
label.setPreferredSize(new Dimension(100, 100));
dialogPanel.add(label, BorderLayout.CENTER);
return dialogPanel;
}
}
And i have a test program to use it:
import <my package path>.SampleDialogWrapper;
public class maintest {
public static void main(String[] args) {
SampleDialogWrapper d = new SampleDialogWrapper();
d.showAndGet();
}
}
The IDE shows no issues in the editor itself, but when i try to run this main method, i get the following 2 errors:
Execution failed for task ':maintest.main()'.
Caused by: java.lang.ClassNotFoundException: com.intellij.openapi.ui.DialogWrapper
I'm assuming i need to somehow add this as a dependency, but i'm not sure how to.
On a side note, is there any way to make use of these custom swing components (as described here) in the visual designer? or does it have to be done fully through code and just running it to see what it looks like? Thanks
Please sign in to leave a comment.
Hi,
These components are not intended to be used outside the IntelliJ Platform context. If you need to run it from a main method for some reason, you need to add missing classes to the classpath.
You can use what is in GUI Designer's palette.
I suggest building UI and checking it in actual IDE.
By that, do you mean i should only try to open a gui from the AnAction method i use in my plugin?
I just tried to use a main method because this is the first time im working with a UI so it seemed easier to play around with than having to reload the entire plugin all the time which takes ages.
I originally started building the gui using the new -> Swing UI designer -> Gui form option, but thought it only provided default swing elements. Are the elements in the palette the same ones intellij uses for its UI? or do i need to somehow import a custom set?
Also, how does that SampleDialogWrapper above work? it seems to be programmatically placing the elements of the UI. Is there any way to see that visually in the IDE without having to run the plugin to see it?
Yes, not every element is visible in the GUI Designer preview.
No, in the default project setup, the palette will be limited to plain Swing components. You can customize it, though.
If you build a component programmatically, you can view it in the running IDE. You can optimize your workflow by enabling auto-reload.
Thanks for you help. With the auto reload, i disabled buildSearchableOptions, but when i run ./gradlew buildPlugin, it fails with error:
Please search the forum for this error:
https://intellij-support.jetbrains.com/hc/en-us/search?utf8=%E2%9C%93&topic=200366979&query=The+requested+operation+cannot+be+performed+on+a+file+with+a+user-mapped+section+open
There can be many reasons, from Windows antivirus protection to using outdated Gradle and Gradle IntelliJ Plugin.
By the way, If you develop UI forms, consider using Kotlin UI DSL instead of GUI Designer.
I had a look at that kotlin UI page, but its not clear to me how i actually invoke the UI to open? with a .form file, i can refer to components inside of it to display it with setContentPane. How do i access the form specified in a .kt file?
You just return a panel created with
panel {}
fromcreateCenterPanel()
.Forgive me if this question is silly, i cant make sense of a lot of what youre linking.
I was able to use some kotlin dsl ui from the demo like this:
This works if i import demoBasics from the builtin ui dsl showcase
However, if i make my own kotlin file with the exact same code:
then i get an error importing it when i try to build and run the project:
I don't understand what the error reason is (it is about `TestFormKt`, but I don't see it used in your code), but you shouldn't import any demo code. Just create your own components in your plugin project by copying what's in the demo and adjusting to your needs.
Thats what i did - I created a file "TestForm.kt", copied the demo code a function "demoBasics" into it (minus the @Demo annotation), imported it using:
into my class "TestKtUi" that extends JFrame, and set it as the content pane of the frame. The editor detected no issues. But when i tried to build, i got the error in my previous comment. but if i changed the import statement to (which i understand i shouldnt, was just doing for testing purposes):
then the plugin built and ran without issues.