if99.net

IF99 ITB

Archive for the ‘visual editor’ Category

Visual Editor in Eclipse Galileo

without comments

For a long time I have tried to install Visual Editor into my Eclipse Galileo and never get it work until recently.

The secret is explained in the Wiki:

With “Eclipse IDE for Java EE Developers”, you should NOT check the Java EMF Model Utilities (org.eclipse.jem.util) plugins since there are already installed.

Ugh… I think I have tried it before but why only now it is working? Anyway, I’m glad that I have it.

For those who are not aware, Visual Editor is an GUI editor for Eclipse. It can be used to assist Swing or SWT application creation. I never like Netbeans Matisse or SWT Designer because I can’t modify the code like I want. I know that Visual Editor is pretty slow, but to get a code that I can enhance manually tastes better than the alternative.

And more importantly, I like the way it codes my Swing application. Here’s an example:

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class TTT extends JFrame {

   private static final long serialVersionUID = 1L;
   private JPanel jContentPane = null;
   private JLabel jLabel = null;
   private JButton jButton = null;
   private JTextField jTextField = null;
   private JSlider jSlider = null;

   /**
    * This is the default constructor
    */
   public TTT() {
      super();
      initialize();
   }

   /**
    * This method initializes this
    *
    * @return void
    */
   private void initialize() {
      this.setSize(300, 196);
      this.setContentPane(getJContentPane());
      this.setTitle("JFrame");
   }

   /**
    * This method initializes jContentPane
    *
    * @return javax.swing.JPanel
    */
   private JPanel getJContentPane() {
      if (jContentPane == null) {
         jLabel = new JLabel();
         jLabel.setText("JLabel");
         jContentPane = new JPanel();
         jContentPane.setLayout(new BorderLayout());
         jContentPane.add(jLabel, BorderLayout.CENTER);
         jContentPane.add(getJButton(), BorderLayout.EAST);
         jContentPane.add(getJTextField(), BorderLayout.SOUTH);
         jContentPane.add(getJSlider(), BorderLayout.NORTH);
      }
      return jContentPane;
   }

   /**
    * This method initializes jButton
    *
    * @return javax.swing.JButton
    */
   private JButton getJButton() {
      if (jButton == null) {
         jButton = new JButton();
         jButton.setHorizontalAlignment(SwingConstants.TRAILING);
         jButton.setText("Test");
         jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
               System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
            }
         });
      }
      return jButton;
   }

   /**
    * This method initializes jTextField
    *
    * @return javax.swing.JTextField
    */
   private JTextField getJTextField() {
      if (jTextField == null) {
         jTextField = new JTextField();
         jTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override public void keyTyped(java.awt.event.KeyEvent e) {
               System.out.println("keyTyped()"); // TODO Auto-generated Event stub keyTyped()
            }
         });
      }
      return jTextField;
   }

   /**
    * This method initializes jSlider
    *
    * @return javax.swing.JSlider
    */
   private JSlider getJSlider() {
      if (jSlider == null) {
         jSlider = new JSlider();
      }
      return jSlider;
   }

} // @jve:decl-index=0:visual-constraint="10,10"

See how it creates a getter for every components? The getter should prepare a component with its properties and also its listener initialization. It makes every method pretty small and readable. There is no long methods with several components and listeners initialization.

Related posts:

  1. Eclipse plugin: Introduce Static Imports
  2. SWT: Composite as filler
  3. What are the important improvements on Eclipse Galileo 3.5 (for me)

Visual Editor in Eclipse Galileo originally appeared on satukubik on April 23, 2010.

Written by Nanda Firdausi

April 23rd, 2010 at 8:04 pm