A Very Graphic Story — Chapter 12

Upulie Handalage
2 min readJul 25, 2022

374–416

A graphical interface is a must to bring in the user friendliness to an application. Command line applications can be powerful yet inflexible, and unfriendly. This chapter brings in UI features in Java, such as how to include buttons on a screen, and making it all pretty.

Few important facts about creating GUI from Java

  1. A listener interlace is the bridge between the listener (you) and event source (the button). When you implement a listener interface, you. give the button a way to call you hack. The interface is where the call-hack method is declared.
  2. To make a GUI, start with a window, (usually a JFrame

JFrame frame = new JFrame();

3. You can add widgets (buttons, text fields, etc.) to the JFrame using:

frame.getcontentPane().add(button);

4. Unlike most other components, the JFrame doesn’t let you add to it directly, so you must add to the JFrame’s content pane.

5. To make the window (JFrame) display, you must give it a size and tell it be visible:

frama.setSize(300,300);

frame.sBtVisihle(true);

6. To know when the user clicks a button (or takes some other action on the user interface) you need to listen for a GUI event.

7. To listen for an event, you must register your interest with an event source. An event source is the thing (button, checkbox,etc.) that ‘fires’ an event based on user interaction.

8. The listener Interface gives the event source away to call you back, because the Interface defines the methods the event source will call when an event happens.

9. To register for events with a source, call the source’s registration method. add<EventType>Listener

10. To register for a button’s ActionEvents

button.addActionListener(this);

11. Implement the listener interface by implementing all of the interface’s event-handling methods. Put your event handling code in the listener call-back method.

12. For ActionEvents, the method is:

public void actionPerformed(ActionEvent event) {

button. setText (“you elicked! “) ;

}

13. The event object passed into the event-handler method carries information about the event, Including the source of the event.

--

--

Upulie Handalage

Everything in my point of view. Here for you to read on....