SWING

To start building our GUI with swing we first import it:

import javax.swing.*

While at it, we can import a few more useful packages and classes that we will use:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

java.awt.* contains classes for creating GUI components like buttons, windows etc.

java.awt.event.ActionEvent classrepresents an action event that happens when a user interacts with a GUI component, like clicking a button.

java.awt.event.ActionListener interface is used to handle such action events. When a component, such as a button, generates an action event, the ActionListener's actionPerformed method is called. This method contains the code that responds to the event, allowing you to define what should happen when the user interacts with the component.

So when import what we need, under the class line we should create the following objects witch are used for creating the GUI window. All of this objects have to be used for a GUI.

The objects that can be used for creating a simple GUI with swing are JFrame, JPanel, JButton, JLabel.

public class CoffeeShopGUI
    static private JFrame frame;
    static private JPanel panel;
    static private JButton viewMenuButton;
    static private JButton orderButton;
    static private JButton seeBasketButton;
    static private JButton seeBillButton;
    static private JButton payAndExitButton;

Here, frame is the window, panel is a box in the window that holds the buttons, and a button is a button🙂 A good practice is to call the objects like this at the top of the class, and use private and static.

Next thing to remember is that everything related to the window settings is written in the java constructor of the class, because the constructor is where you initialize the frame and set its properties before making it visible.

Window Setting-up

In this code snippet, this refers to the current object, which is assumed to implement the ActionListener interface. The addActionListener method is used to register the current object as an action listener for multiple buttons.

When a button is clicked, an action event is generated, and the actionPerformed method of the action listener (in this case, the current object) is called. By registering the current object as the action listener for multiple buttons, you can handle the action events from these buttons in a single actionPerformed method, which can differentiate between the buttons based on the event source.

Here the buttons are added to the panel, dont mind the first 2 rows i dont getthe mesurements as well, saw a lot of people just writing this numbers.

Functionality

In the main method of the GUI create a new constructor.

The actionPerformed method is called when an action event occurs, such as a button click. It takes an ActionEvent parameter (e), which provides information about the event. The getActionCommand() method of the ActionEvent class retrieves the command string associated with the event, which is often the text displayed on a button or the action command set for other components. This allows you to determine which component triggered the event and take appropriate action based on that information.

Now we can use a switch statement to call the appropriate function depending on what the user clicks.

the last thing left is to use the object from the main coffee shop code and call the functions that we defined from there.

And like that, using swing we made our simple GUI for our Coffe Shop.


CheetSheets

chevron-rightWindowhashtag

Creating the window of the GUI in swing is done with JFrame.

chevron-rightPanelhashtag

Panel is the place where we put button, labels… And is created with JPanel.

chevron-rightButtonhashtag

Buttons take action when clicked thanks to the ActionListener and perform specific task defined in the ActionListener method, and its created with JButton.

chevron-rightLabelhashtag

Label is a field where we can put some text or pictures on the panel, and is created with JLabel.

chevron-rightText Input Boxhashtag

Text box is a box in whitch you can write anything. And it is created with the JTextField.

chevron-rightPassword Input Boxhashtag

Password box is a box in whitch you can write anything but the difference is that dots are displayed insead of the letters. This is thanks to the java developers that created the swing library and the JpasswordField method that we used. And yeah, it is created with the JPasswordField.

chevron-rightImagehashtag

Images can be added using swing using the JLabel objects and with this command: label.setIcon(ImageIcon variable name that holds the image path)

Last updated