Java
/
Swing
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
R
Q
Listener
The Swing GUI components are event sources. An event source is an object that can turm user actions into events.
copy
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);
}
}
Window opened
Focus input text on window opened.
copy
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);
}
}
Button Listener
Use anonymous inner classes for each button..
copy
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);
}
}
Key Listener
The KeyListener is notified whenever you change the state of key.
copy
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);
}
}
Mouse Listener
MouseAdapter is an abstract adapter class for receiving mouse events.
copy
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);
}
}
➥ Questions
Last update: 520 days ago