How to Add Functionality to NetBeans GUI: Java Programming

December 18, 2013 , , 0 Comments

Programmer can know, to add graphical components in a frame and how to set their properties. But doing much is not sufficient in java programming, because the graphical controls that you add to frame can’t do anything on their own.

In other words, they have the look but not any feel. To add feel i.e. functionality or behaviour to them, you must also know about something called events and listeners. The three major players that add functionality to GUI are:

  • The Event: An event is an objects that gets generated when user does something such as mouse click, dragging, pressing a key on the keyboard etc.
  • Source of the Event: The component where the event has occurred, is the source of the event. For example, if a user clicks (the Event) on a Submit button then the source of this event is Submit button.
  • Event Listener: An event listener is attached to a component and contains the methods/functions that will be executed in response to an event. For example, if a user click on a button, then the buttons event listener will executed some code in response to this event.
    Listener Interface. An event Listener stores all the methods that it will implement in response to events, inside the listener interface. So, you can say that a listener interface stores all event-response-methods or event-handler methods of an event listener.

Commonly used Events and Listeners.

  • ActionEvents: of type ActionListener used to gets activated when the user performs an action with a button or other components. Usually, a user invokes the button by clicking over it. However, the user can also invoke a button action tabbing to the button and pressing the Enter key.
  • ItemEvent of type ItemListener used to gets activated when the selected item in a list control, such as a combo box or list box, is changed.
  • AdjustmentEvent of type AdgustListener used to gets activated when the user drags or moves knob of a scroll bar.
  • ChangeEvent of type ChangeListener used to gets active when the properties of a slider change.
  • KeyEvents of type KeyListener used to gets activate when the user press a key on the keyboard. You can use this event to watch for specific keystrokes entered by the user.
  • ListSelection of type ListSelectionListener used to get activated when an item is selected/deselected from list (JList).
  • MouseEvent of type MouseEventListener used to gets activated when the user does something with the mouse, such as clicks one of the buttons, drags the mouse, or simply moves over another objects.
  • FocusEvent of type FocusEventListener used to gets activated when a components receives or lose focus. Focus is ability to receive input, e.g., you can select from a list box only when it has focus, you can type in a text field only if it has focus.  

Each of the listeners listed above have multiple methods stored in their listener interfaces to respond to different type of events. You are armed with some basic knowledge of GUI functioning in Java so in further articles i will let you know about to create a GUI application using NetBeans.

Anil Swami

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google