Skip to content

IntelliJ Introduction to Swing

Amir Sagiv edited this page Jul 1, 2017 · 1 revision

Swing

Swing is a GUI widget toolkit for Java. Swing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit (AWT).

Though Swing is intended to be replaced by JavaFX, it will remain part of the Java SE specification for the foreseeable future.

Creating a Swing Application

In order to create a Swing application you will have to complete the following steps:

  • Create a new project.

  • Right click on the wanted package and choose New | GUI Form

  • In the created .form file you can now add elements, such as buttons.

  • Add names to the elements you would like to be able to change.

    • Notice that every named element appears as a field in a class in the corresponding .java file
  • To add functionality to your buttons you should add a listener for each of them. You can easily do that by right clicking the button (in the .form file) and choosing Create Listener and choose the type of listener you would like to add. You will usually want to add an Action Listener (it is called when the button is pressed), but there are different types of listeners. For more information about listeners see the links below.

  • Now for the window to appear you will have to create a Jframe and set some things (can be seen in the tutorials linked below).

    • You can either do it in your action class by doing something like this:

      image

    • or you can extend Jframe in the created class and just add to the constructor something like this:

      image

      In this case you just need to create an instance of the class in order to open the window.

Links

Clone this wiki locally