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

0
11 comments

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.

0

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? 

0

By that, do you mean i should only try to open a gui from the AnAction method i use in my plugin?

Yes, not every element is visible in the GUI Designer preview.

 

Are the elements in the palette the same ones intellij uses for its UI? or do i need to somehow import a custom set?

No, in the default project setup, the palette will be limited to plain Swing components. You can customize it, though.

 

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? 

If you build a component programmatically, you can view it in the running IDE. You can optimize your workflow by enabling auto-reload.

0

Thanks for you help. With the auto reload, i disabled buildSearchableOptions, but when i run ./gradlew buildPlugin, it fails with error:

> Could not copy file '<project root>\build\libs\<project name>-1.0-SNAPSHOT.jar' to '<project root>\build\idea-sandbox\plugins\<project name>\lib\<project name>-1.0-SNAPSHOT.jar'.
> <project root>\build\idea-sandbox\plugins\<project name>\lib\<project name>-1.0-SNAPSHOT.jar (The requested operation cannot be performed on a file with a user-mapped section open)
0

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.

0

By the way, If you develop UI forms, consider using Kotlin UI DSL instead of GUI Designer.

0

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?

0

You just return a panel created with panel {} from createCenterPanel().

​Please see examples in the platform code and in UI DSL Showcase Tab mentioned in the docs.
0

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:

public class TestKtUi extends JFrame {
public TestKtUi() {
super();
initComponents();
}

public void initComponents() {
this.setContentPane(demoBasics());
this.setPreferredSize(new Dimension(1000, 500));
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}

This works if i import demoBasics from the builtin ui dsl showcase

import static com.intellij.internal.ui.uiDslShowcase.DemoBasicsKt.demoBasics;

However, if i make my own kotlin file with the exact same code:

import com.intellij.openapi.ui.DialogPanel
import com.intellij.ui.dsl.builder.RowLayout
import com.intellij.ui.dsl.builder.panel


fun demoBasics(): DialogPanel {
return panel {
row("Row1 label:") {
textField()
label("Some text")
}

row("Row2:") {
label("This text is aligned with previous row")
}

row("Row3:") {
label("Rows 3 and 4 are in common parent grid")
textField()
}.layout(RowLayout.PARENT_GRID)

row("Row4:") {
textField()
label("Rows 3 and 4 are in common parent grid")
}.layout(RowLayout.PARENT_GRID)
}
}

 then i get an error importing it when i try to build and run the project:

error: cannot find symbol
import static <package path>.gui.TestFormKt.demoBasics;
                              ^
  symbol:   class TestFormKt
location: package <package path>.gui

 

0

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.

0

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:

import static <package path>.gui.TestFormKt.demoBasics;

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):

import static com.intellij.internal.ui.uiDslShowcase.DemoBasicsKt.demoBasics;

then the plugin built and ran without issues.

0

Please sign in to leave a comment.