Creating a new Java visual class

You can use the New Java Visual Class wizard to quickly create a Java visual class for use within the visual editor for Java.

Before you can create a new Java visual class, you need to have a Java project and a package where you can place the visual class.

When you are developing a Java application with a graphical interface, you can use the New Java Visual Class wizard to create new visual classes. The wizard uses templates based on your selections on the wizard to create an initial class that you can then edit visually.

For example, with the wizard you can specify the toolkit or style that you want to use (SWT, Swing, or AWT), the initial container or composite that you want the visual class to instantiate (for example, a SWT composite or a Swing JPanel), and whether you want the visual class to be an application that includes a public static void main(String[] args) method.

To create a new Java visual class for use in the visual editor for Java:

  1. In the Java perspective, click File > New > Visual Class. The New Java Visual Class wizard opens.

  2. In the Source Folder field, enter the workspace folder where the class will be saved. This will default to your current project.

  3. In the Package field, enter the name of the Java package where you want the visual Java class to be packaged. Tip: If you open the wizard from the pop-up menu of the package, this field defaults to that package name.

  4. Ensure that the Enclosing type check box is cleared. Selecting this option creates an inner class in another class that you specify, so no new visual class is created.

  5. In the Name field, enter the name for the new Java visual class.

  6. Select one of the following modifiers to specify access control for the class:

    • public - makes the class completely available to any other class that wants to use it

    • default - sets no modifier, making it available to any other class in the same package

  7. Optional: Select one of the following modifiers for the class:

    • abstract - indicates that the class will serve in a superclass role

    • final - indicates that the class cannot be subclassed

  8. In the Style list, select the toolkit and visual element that you want your new visual class to extend. For example, you can select SWT composite or Swing JPanel. The Superclass field displays the appropriate class name. If you select Other as the style, you need to specify the superclass that you want to extend or accept the default java.lang.Object. The java.lang.Object superclass can be used, for example, for a SWT application that uses a SWT Shell.

  9. Optional: To import and implement an additional interface in your new visual class, click Add and select the interface, then click OK. The interfaces to be implemented are listed in the Interfaces field. Repeat this step for each interface.

  10. Optional: Select any of the following check boxes to generate additional method stubs in your new visual class:

    • public static void main(String[] args) - generates a stub main() method for running the class as a Java application. For SWT applications, the visual editor also generates the necessary display loop in the main method.

    • Constructors from superclass - generates constructor stubs to initialize methods inherited from the superclass.

    • Inherited abstract methods - generates stubs for additional abstract methods inherited from the superclass.

  11. Click Finish.

The wizard generates a new .java file that includes the new visual class, and the class opens in the visual editor for Java

 

Parent topic

Developing Java classes with the visual editor

 

Related tasks

Opening an existing Java class in the visual editor