Professional Documents
Culture Documents
Learning Objectives
This session explores the advanced features of Swing Timer, usage, and its
practical implementation of Timers, along with Key Bindings and demonstrate
the various steps involved in creating a Manifest FIle
Swing Timers are a feature in the Swing library that allow user to schedule a
task to be performed repeatedly after a specified delay. They are
implemented using the Timer class in Java, which is part of the javax.swing
package. Swing Timers are useful for creating animations, updating displays,
and performing other periodic tasks in a Swing application.
10.1.1 Timer Class:
The first constructor creates a Timer that fires an ActionEvent after the
specified delay. The second constructor creates a Timer that fires an
ActionEvent after the specified delay, but also has an initial delay before the
first event is fired. This can be useful for delaying the start of the Timer.
Following Code Snippet 1 shows creating a Timer object with an initial delay
of 1000 milliseconds (1 second) and an ActionListener that prints a message.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.*;
import java.awt.event.*;
In the constructor, the JLabel and Timer objects are created, and the Timer's
ActionListener updates the label text and stops the timer when the remaining
time reaches 0.
Code Snippet 3 shows practical usage of timer for Bouncing a Ball
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public BouncingBallExample() {
Timer timer = new Timer(5, this);
timer.start();
}
In the constructor, the x, y, dx, and dy variables are initialized, and the Timer's
ActionListener updates the x and y position of the ball and changes the
direction of movement if the ball hits the edge of its parent container. The
repaint() method is called to update the display.
In the paintComponent() method, the ball is drawn as a red oval using the
current x and y position and the BALL_SIZE constant.
import javax.swing.*;
import java.awt.event.*;
public ProgressBarExample() {
// Set up the frame
setTitle("Progress Bar Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 100);
Code Snippet 4 creates a progress bar that can display a specific string at certain
progress values. The setProgressString(String progressString) method sets the string
that is displayed on the progress bar, and the updateProgress(int progress) method
updates the progress bar and label with the specified progress value (between 0 and 100).
The createPanel() method returns a JPanel that contains the progress bar and label.
a JFrame to display the panel, and sets the content pane of the frame to the panel. It
a progress string at specific progress values. In this example, the progress string is
set to "Starting..." at 10% progress, "Halfway there!" at 50% progress, "Almost done..."
In Code snippet 5 we create a JPanel with a JButton and a JLabel that will
display the output of our action. We define a key binding that maps the "Ctrl
+ S" key combination to an action that sets the text of the label to "Save
button clicked!"
10.2.1 InputMap
The InputMap maps a key or key combination and an input event. This event
can be triggered by the user pressing the key or key combination or by some
other action, such as a timer firing. In addition, the InputMap class provides
methods for adding, removing, and looking up key bindings.
For example, to map the "Ctrl + S" key combination to the "Save" action in an
application, you could use the following code:
InputMap inputMap =
myComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_DOWN_MASK);
inputMap.put(keyStroke, "saveAction");
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
InputMap inputMap =
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
panel.add(button);
getContentPane().add(panel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
10.2.2 ActionMap
The ActionMap is a mapping between a key and an action. Action is
typically an object that implements the Action interface, which defines a
method for performing the action. The ActionMap class provides methods for
adding and looking up actions.
For example, to map the "saveAction" key to a SaveAction object, you could
use the following code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public ActionMapExample() {
super("ActionMap Example");
button.setActionCommand("saveAction");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
Action action = actionMap.get(actionCommand);
action.actionPerformed(e);
}
});
panel.add(button);
getContentPane().add(panel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
The ActionMap is retrieved from the panel using the getActionMap() method,
and the put() method is used to associate the string identifier "saveAction"
with the action.
Manifest-Version: 1.0
Main-Class: com.example.Main
Class-Path: lib/library1.jar lib/library2.jar
⮚ Create a manifest file named MANIFEST.MF with the required attributes, and
place it in the META-INF directory.
⮚ Compile your application code using the javac command.
⮚ Create a JAR file using the jar command, including the -cvmf options to specify
the manifest file and the main class.
MANIFEST.MF is the manifest file, myapp.jar is the name of the output JAR file,
and *.class specifies all the class files to be included in the JAR file.
Once the JAR file is created, it can be executed using the java -jar
command, like this:
10.4 Summary
⮚ In Java Swing, the Timer class is a useful tool for creating animations and other
time-based events
⮚ The Timer class generates action events at specified intervals, which can be used
to update the display of a Swing component, change the state of an object, or
perform other actions.
⮚ The practical usage of the Timer class in Java Swing includes creating animations,
such as bouncing balls or moving components, as well as updating the display of
a Swing component at regular intervals.
⮚ The Timer class is a versatile and powerful tool for creating time-based events
and animations in Java Swing, and is an essential component in many Java GUI
applications.
⮚ Key bindings in Java Swing allow you to associate actions with keyboard events,
such as pressing a key or a key combination.
⮚ There are two important maps used in key bindings: InputMap and ActionMap.
The InputMap maps KeyStroke objects to a unique string identifier, while the
ActionMap maps the string identifier to an Action object.
⮚ By combining these maps, you can create key bindings that trigger specific
actions in your application.
⮚ The manifest file is a special file that contains metadata about the JAR file, such
as the main class and classpath.
⮚ The manifest file is located in the META-INF directory of the JAR file and is named
MANIFEST.MF.
⮚ Once the manifest file is created, you can use the jar command to create an
executable JAR file, which can be launched using the java -jar command.
1. javax.swing
2. Timer(int delay, ActionListener listener)
3. A class used to create animations and timed events
4. By calling the constructor of the Timer class
5. By calling the actionPerformed() method of the Timer class
References:
https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html#:~:text=A
%20Swing%20timer%20(an%20instance,timer%20facility%20in%20the%20java.
https://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html
http://www.java2s.com/Tutorial/Java/0240__Swing/SwingTimers.htm
https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
https://forums.oracle.com/ords/r/apexds/community/q?question=key-
bindings-swing-timer-7060
https://www.developer.com/java/java-jar-manifest-file/#:~:text=In%20the
%20Java%20programming%20language,to%20attributes%20of%20security
%20properties.
https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html