Musings about Coding, Business and other Geek Stuff Live and Direct from somewhere on the planet
February 17, 2004
GUI tools in IDEA 4

I have been steadfastly ignoring the GUI tools in IDEA4 throughout the whole Aurora dev phase. I dont really do much in the way of gui work and have generally found that its best to do it straight in code, when I’ve had to.

I did think it would be an interesting excercise though to try it out. After all this is IDEA. It should be quite good.

Form Designer

The above shows the form designer. I recommend that you go through the live demos on the IntelliJ site. They explained a few things that I wasnt aware of. But its really pretty much the same as is in the online help.

The functionality is very similar to that of QT Designer which should be installed on just about any Linux box. This is good as qt designer is very intuitive tool.

To create a form you go through the following steps:

  1. Create new form
  2. Drop components on form with rough positioning
  3. Group certain related components together using vertical or horizontal layouts
  4. Add Spacers to hint at how you want the form formatted
  5. Group everything into a grid layout

This is all really very simple and intuitive. You can get a nicely looking form done in no time.

Binding

Binding is the process of linking the gui to code. You simply select the Form object and ad a Class name to the “bind to class” property. If the class doesnt exist a “Create new Class” intent is shown. This is one thing I think they’ve done wrong. It shows the Intent in the object tree next to the Form object. Which isnt wrong, but it doesnt show it in the property box, where your cursor is. This means you have to click up on the intent to select it manually as opposed to - that we normally use.

You then bind the interesting components the same way to the class by writing variable names in the “binding” property of each one.

Really its simple, easy and creates clean code:

package org.talk;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PassphraseDialog {
     private JPanel panel;
     private JPasswordField password;
     private JLabel user;
     private JButton btnCancel;
     private JButton btnOk;
}

All you need to do now is to instantiate it and create the enabling code.

Enabling code

The main thing you need to do in your class now is to create a JFrame or whatever and add the panel to it, as well as of course add listeners etc.

This simple example instantiates a JFrame adds the contents of the form and a bit of naive form handling.

package org.talk;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PassphraseDialog {
     private JPanel panel;
     private JPasswordField password;
     private JLabel user;
     private JButton btnCancel;
     private JButton btnOk;
     private JFrame frame;
 
     public PassphraseDialog() {
          frame=new JFrame("Please enter Passphrase...");
          frame.setContentPane(panel);
          btnCancel.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                    frame.hide();
                    frame.dispose();
                }
           });
          btnOk.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e) {
                    frame.hide();
                    frame.dispose();
                    System.out.println("passphrase is: "+new String(password.getPassword()));
                }
           });
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      }
 
     public  void  show(String name){
          user.setText(name);
          frame.pack();
          frame.show();
      }
 
     public void dispose(){
          frame.dispose();
      }
 
     public  static void main (String args[]){
          PassphraseDialog dialog=new PassphraseDialog();
          dialog.show("bob");
      }
}

As you can see the code is simple and doesnt have anything strange in it. However that is the strange thing and the thing that I really dont like about IDEA gui designer.

It hides to much from us. To see what is actually going on, go into “Settings…”, click “GUI Designer” and select “Generate GUI into java source code”.

This immediately shows us all the ugly stuff that always ends up in interactive gui designers.

This adds all of this frightfull stuff:

 {
 // GUI initializer generated by IntelliJ IDEA GUI Designer
 // >>> IMPORTANT!! <<<
 // DO NOT EDIT OR ADD ANY CODE HERE!
         $$$setupUI$$$();
     }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     */
    private void $$$setupUI$$$() {
         final JPanel _1;
         _1 = new JPanel();
         panel = _1;
         _1.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(4, 3, new java.awt.Insets(0, 0, 0, 0), -1, -1));
         final JPanel _2;
         _2 = new JPanel();
         _2.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 2, new java.awt.Insets(0, 0, 0, 0), -1, -1));
         _1.add(_2, new com.intellij.uiDesigner.core.GridConstraints(3, 1, 1, 2, 0, 3, 3, 3, null, null, null));
         final JButton _3;
         _3 = new JButton();
         btnOk = _3;
         _3.setText("OK");
         _2.add(_3, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, 0, 1, 3, 0, null, null, null));
         final JButton _4;
         _4 = new JButton();
         btnCancel = _4;
         _4.setText("Cancel");
         _2.add(_4, new com.intellij.uiDesigner.core.GridConstraints(0, 1, 1, 1, 0, 1, 3, 0, null, null, null));
         final com.intellij.uiDesigner.core.Spacer _5;
         _5 = new com.intellij.uiDesigner.core.Spacer();
         _1.add(_5, new com.intellij.uiDesigner.core.GridConstraints(3, 0, 1, 1, 0, 1, 6, 1, null, null, null));
         final JLabel _6;
         _6 = new JLabel();
         _6.setText("Please enter Passphrase for");
         _1.add(_6, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 2, 8, 0, 0, 0, null, null, null));
         final JLabel _7;
         _7 = new JLabel();
         user = _7;
         _7.setText("name");
         _1.add(_7, new com.intellij.uiDesigner.core.GridConstraints(1, 0, 1, 1, 8, 0, 0, 0, null, null, null));
         final JPanel _8;
         _8 = new JPanel();
         _8.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 2, new java.awt.Insets(0, 0, 0, 0), -1, -1));
         _1.add(_8, new com.intellij.uiDesigner.core.GridConstraints(2, 0, 1, 3, 0, 3, 3, 3, null, null, null));
         final JLabel _9;
         _9 = new JLabel();
         _9.setText("Passphrase");
         _8.add(_9, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, 8, 0, 0, 0, null, null, null));
         final JPasswordField _10;
         _10 = new JPasswordField();
         password = _10;
         _8.add(_10, new com.intellij.uiDesigner.core.GridConstraints(0, 1, 1, 1, 8, 1, 6, 0, null, new java.awt.Dimension(150, -1), null));
         final com.intellij.uiDesigner.core.Spacer _11;
         _11 = new com.intellij.uiDesigner.core.Spacer();
         _1.add(_11, new com.intellij.uiDesigner.core.GridConstraints(0, 2, 1, 1, 0, 1, 6, 1, null, null, null));
         final com.intellij.uiDesigner.core.Spacer _12;
         _12 = new com.intellij.uiDesigner.core.Spacer();
         _1.add(_12, new com.intellij.uiDesigner.core.GridConstraints(1, 2, 1, 1, 0, 1, 6, 1, null, null, null));
     }

Which should be enough to scare the living daylights out of anyone. Besides the ugly code, that really isnt that much of a problem, the big deal for me is that now you need to include the com.intellij.uiDesigner.* stuff in your classpath. Which it automatically does when you build it. But this really makes it pretty much useless for OSS development.

Is there really no other way of doing this than using custom classes? I have no idea, and I supose that I really dont care too much. But howabout serialized forms or something, that you can initialize with one line of standard java?

Posted by pelleb at February 17, 2004 10:40 AM
This entry was posted in the following Categories: Java
Comments

Try extending a class, say JTable and using it in the UI Designer. It's a huge pain in the ass - nearly impossible to do. You can't simply the JTable as your subclass. That would be too easy. Instead, you need to create your JTable, edit a few XML files, add it as a GUI bean to the project, configure a new menubar for it, etc. The problem is that it's impossible to debug a new component this way since you have to go through the whole process again.

Posted by: allowAnonymous on February 17, 2004 12:50 PM

I think you can distribute the com.intellij classes with your application without restriction. The custom classes are to avoid the dozens of GridBagLayout bugs. I don't think adding a few kb to a project's download is such a big deal.

Personally I like that it hides the UI code from you. I think it's the way it should be, with a UI designer as powerful as IDEA's, you don't need to see the code, you can trust it works.

I also wish it was possible to add custom components to a form more easily, but this might be coming in a later release (like IDEA 4.1, due out in a few months).

Posted by: Keith Lea on February 17, 2004 11:01 PM

Yup, that code sure is ugly. Have you ever checked out the code that a JSP is turned into? It appears that JetBrains took a hint from IronFlare (makers of Orion Application Server) and made it ugly but pretty at the same time. I mean, the results are great are they not?

Thank you for the excellent example. I am not a Swing guy myself, but you have provided a great starting point for learning more.

Posted by: Carl Fyffe on February 18, 2004 12:53 AM

i have encounter an error :

package com.intellij.uiDesigner.core doesn not exist

what does it mean? what could have prompt this error? pls reply

Posted by: daniel on May 12, 2004 04:22 AM

Can you tell me what the word "Final" means when declaring the JPanel?

Posted by: Patti on June 10, 2004 03:24 PM
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?