PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   26-07-2021

Listener

The Swing GUI components are event sources. An event source is an object that can turm user actions into events.
 
import javax.swing.*;
import java.awt.event.*;

/**
 * Add event on button (on action performed)
 */
public class App {

    public static void main(String[] args) {
    
        SimpleGui gui = new SimpleGui();
        gui.run();
    }
}

// Our GUI implements the listener interface

class SimpleGui implements ActionListener {

    JFrame frame = new JFrame(); 
    JButton button = new JButton("click me");
    
    @Override
    public void actionPerformed(ActionEvent event) {
        
        button.setText("I've been clicked!");
        
        // On button click the button text is changed
    }
    
    public void run() {

        button.addActionListener(this);
        
        // Add Gui to button list of listeners
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        frame.getContentPane().add(button); 
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}
... 23 lines
 

Window opened

Focus input text on window opened.
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Focus input text (on window open)
 */
public class App extends JFrame {

    public static void main(String[] args) {
        
        JFrame frame = new App();
        frame.setBounds(200, 200, 300, 200);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public App() {
        
        JTextField inputText = new JTextField("No focus", 10);
        final JTextField inputText2 = new JTextField(15);
        
        addWindowListener( new WindowAdapter() {
            @Override
            public void windowOpened( WindowEvent e ){
                inputText2.requestFocus();
            }
        });

        JPanel panel = new JPanel();
        panel.add(inputText);
        panel.add(inputText2);
        setLayout(new FlowLayout());
        add(panel);
    }
}
... 25 lines
 
Button Listener    (2/2)

Button Listener

Use anonymous inner classes for each button..
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class App extends JFrame {

    public static void main(String[] args) {
        
        JFrame frame = new App();
        frame.setBounds(200, 200, 300, 200);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public App() {
        
        JButton startButton = new JButton("Start");
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Button Clicked!");
            }
        });
        
        JPanel panel = new JPanel();
        panel.add(startButton);
        setLayout(new FlowLayout());
        add(panel);
    }
}
... 17 lines
 

Key Listener

The KeyListener is notified whenever you change the state of key.
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class App extends JFrame {

    public static void main(String[] args) {

        JFrame frame = new App();
        frame.setBounds(200, 200, 300, 200);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public App() {
        
        JTextField inputText = new JTextField(20);
        inputText.setText("Press F1 to see the Dialog");
        
        inputText.addKeyListener(new KeyAdapter() {
            
            @Override
            public void keyPressed(KeyEvent e) {
                
                if (e.getKeyCode() == KeyEvent.VK_F1) { // Look Here
                    JOptionPane.showMessageDialog(null, "F1 pressed");
                } // Alerts: "F1 pressed"

                if (e.isControlDown()) { // Ctrl pressed
                    switch(e.getKeyCode()) {
                        case KeyEvent.VK_0:
                        JOptionPane.showMessageDialog(null, "Ctrl + 0 pressed");
                                // Alerts: "Ctrl + 0 pressed"
                            break;
                        case KeyEvent.VK_1:
                        JOptionPane.showMessageDialog(null, "Ctrl + 1 pressed");
                                // Alerts: "Ctrl + 1 pressed"
                            break;
                        default:
                    }
                }
            }
        });
        
        JPanel panel = new JPanel();
        panel.add(inputText);
        setLayout(new FlowLayout());
        add(panel);
    }
}
... 37 lines
 

Mouse Listener

MouseAdapter is an abstract adapter class for receiving mouse events.
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class App extends JFrame {

    public static void main(String[] args) {
        
        JFrame frame = new App();
        frame.setBounds(200, 200, 300, 200);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public App() {
        
        JLabel label = new JLabel("Hello");

        label.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(null, "Label Clicked!");
            } // Alerts: Label Clicked!
        });
        
        JPanel panel = new JPanel();
        panel.add(label);
        setLayout(new FlowLayout());
        add(panel);
    }
}
... 20 lines
 

Questions    
Combo Box

        A B C D E F
🔔
1/2