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
Clock Timer
Swing timer fires events after a specific delay. You might use the general purpose timer (from java.util) if you don't use GUI.
copy
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.TimeZone;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
class Appexample extends JFrame {
private JTextField textField;
public Appexample() {
textField = new JTextField(5);
textField.setEditable(false);
textField.setFont(new Font("sansserif", Font.PLAIN, 48));
textField.setBorder(new EmptyBorder(0,0,0,0));
JPanel panel = new JPanel();
panel.add(textField);
add(panel);
// Look Here - one second
javax.swing.Timer timer =
new javax.swing.Timer(1000, new ClockListener());
timer.start();
}
public static void main(String[] args) {
JFrame App = new Appexample();
App.setBounds(200, 200, 300, 200);
App.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
App.setVisible(true);
}
class ClockListener implements ActionListener { // --- Look Here --- //
@Override public void actionPerformed(ActionEvent e) {
Calendar now = Calendar.getInstance();
now.setTimeZone(TimeZone.getTimeZone("Europe/Bucharest"));
int h = now.get(Calendar.HOUR_OF_DAY);
int m = now.get(Calendar.MINUTE);
int s = now.get(Calendar.SECOND);
textField.setText(h + ":" + m + ":" + s);
}
}
}
Write text char by char
copy
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
class App extends JFrame {
private JTextField textField;
private int i = 0;
private String str = "I idly turn the leaves";
private javax.swing.Timer timer;
public App() {
textField = new JTextField(12);
textField.setEditable(false);
textField.setFont(new Font("sansserif", Font.PLAIN, 30));
textField.setBorder(new EmptyBorder(0,0,0,0));
JPanel panel = new JPanel();
panel.add(textField);
add(panel);
/**
* Use full qualification to avoid
* potential conflicts with java.util.Timer
*/
timer = new javax.swing.Timer(100, new ClockListener()); // Look Here
timer.start();
}
public static void main(String[] args) {
JFrame App = new App();
App.setBounds(200, 200, 400, 300);
App.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
App.setVisible(true);
}
class ClockListener implements ActionListener { // Look Here
@Override public void actionPerformed(ActionEvent e) {
if (++i >= str.length()) timer.stop();
textField.setText(str.substring(0, i));
}
}
}
➥ Questions
Last update: 60 days ago