How to resolve the algorithm GUI component interaction step by step in the Java programming language
How to resolve the algorithm GUI component interaction step by step in the Java programming language
Table of Contents
Problem Statement
Almost every application needs to communicate with the user in some way. Therefore, a substantial part of the code deals with the interaction of program logic with GUI components. Typically, the following is needed:
For a minimal "application", write a program that presents a form with three components to the user:
The field is initialized to zero. The user may manually enter a new value into the field, or increment its value with the "increment" button. Entering a non-numeric value should be either impossible, or issue an error message. Pressing the "random" button presents a confirmation dialog, and resets the field's value to a random value if the answer is "Yes". (This task may be regarded as an extension of the task Simple windowed application).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm GUI component interaction step by step in the Java programming language
The code is written in Java and it is the definition of a class called Interact. The class has four fields:
numberField
is a text field where the user can enter a number.incButton
is a button that increments the number in the text field by 1.randButton
is a button that sets the number in the text field to a random number.FRAME
is a JFrame that contains the text field and the two buttons.
The constructor for the class sets up the GUI components and adds them to the JFrame.
It also adds listeners to the text field and the two buttons.
The setDefaultCloseOperation
method sets the default close operation for the JFrame to EXIT_ON_CLOSE, which means that the program will exit when the user clicks the X button in the top right corner of the window.
The setText
method sets the text in the text field to the specified string.
The setEditable
method sets whether or not the text field is editable.
The isDigitKeyChar
method returns true if the specified key event is a digit key.
The keyTyped
method listens for key events on the text field and consumes any events that are not digit keys.
The addKeyListener
method adds a key listener to the text field.
The mapText
method maps the text in the text field to a new string.
The actionPerformedOnIncrementButton
method is called when the increment button is clicked.
It increments the number in the text field by 1.
The addActionListenerToIncrementButton
method adds an action listener to the increment button.
The addIncrementButton
method adds the increment button to the specified panel.
The showConfirmDialog
method shows a confirm dialog with the specified message and returns the user's choice.
The setFieldText
method sets the text in the text field to the specified integer.
The actionPerformedOnRandomButton
method is called when the random button is clicked.
It sets the number in the text field to a random number.
The addActionListenerToRandomButton
method adds an action listener to the random button.
The addRandomButton
method adds the random button to the specified panel.
The acceptField
method accepts a consumer of a text field.
The prepareField
method prepares the text field by setting its editability, text, and key listener.
The addField
method adds the text field to the specified frame.
The acceptPanel
method accepts a consumer of a panel.
The processPanel
method processes the panel by setting its layout and adding the increment and random buttons.
The addPanel
method adds the panel to the specified frame.
The setLayout
method sets the layout of the specified frame or panel.
The setVisible
method sets the visibility of the specified frame to true.
The acceptFrame
method accepts a consumer of a frame.
The processField
method processes the frame by setting its default close operation, layout, field, panel, and visibility.
The main
method creates an instance of the Interact class and makes it visible.
Source code in the java programming language
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Interact extends JFrame{
final JTextField numberField;
final JButton incButton, randButton;
public Interact(){
//stop the GUI threads when the user hits the X button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
numberField = new JTextField();
incButton = new JButton("Increment");
randButton = new JButton("Random");
numberField.setText("0");//start at 0
//listen for button presses in the text field
numberField.addKeyListener(new KeyListener(){
@Override
public void keyTyped(KeyEvent e) {
//if the entered character is not a digit
if(!Character.isDigit(e.getKeyChar())){
//eat the event (i.e. stop it from being processed)
e.consume();
}
}
@Override
public void keyReleased(KeyEvent e){}
@Override
public void keyPressed(KeyEvent e){}
});
//listen for button clicks on the increment button
incButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String text = numberField.getText();
if(text.isEmpty()){
numberField.setText("1");
}else{
numberField.setText((Long.valueOf(text) + 1) + "");
}
}
});
//listen for button clicks on the random button
randButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
//show a dialog and if they answer "Yes"
if(JOptionPane.showConfirmDialog(null, "Are you sure?") ==
JOptionPane.YES_OPTION){
//set the text field text to a random positive long
numberField.setText(Long.toString((long)(Math.random()
* Long.MAX_VALUE)));
}
}
});
//arrange the components in a grid with 2 rows and 1 column
setLayout(new GridLayout(2, 1));
//a secondary panel for arranging both buttons in one grid space in the window
JPanel buttonPanel = new JPanel();
//the buttons are in a grid with 1 row and 2 columns
buttonPanel.setLayout(new GridLayout(1, 2));
//add the buttons
buttonPanel.add(incButton);
buttonPanel.add(randButton);
//put the number field on top of the buttons
add(numberField);
add(buttonPanel);
//size the window appropriately
pack();
}
public static void main(String[] args){
new Interact().setVisible(true);
}
}
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public interface FunctionalKeyListener extends KeyListener {
@Override
public default void keyPressed(KeyEvent event) {}
@Override
public default void keyTyped(KeyEvent event) {}
@Override
public default void keyReleased(KeyEvent event) {}
@FunctionalInterface
public static interface Pressed extends FunctionalKeyListener {
@Override
public void keyPressed(KeyEvent event);
}
@FunctionalInterface
public static interface Typed extends FunctionalKeyListener {
@Override
public void keyTyped(KeyEvent event);
}
@FunctionalInterface
public static interface Released extends FunctionalKeyListener {
@Override
public void keyReleased(KeyEvent event);
}
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public interface Interact {
public static final JFrame FRAME = new JFrame();
public static final JTextField FIELD = new JTextField();
public static final JPanel PANEL = new JPanel();
public static void setDefaultCloseOperation(JFrame frame) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void setText(JTextField field) {
field.setText("0");
}
public static void setEditable(JTextField field) {
field.setEditable(false);
}
public static boolean isDigitKeyChar(KeyEvent event) {
return !Character.isDigit(event.getKeyChar());
}
public static void keyTyped(KeyEvent event) {
Stream.of(event)
.parallel()
.filter(Interact::isDigitKeyChar)
.forEach(KeyEvent::consume)
;
}
public static void addKeyListener(JTextField field) {
field.addKeyListener((FunctionalKeyListener.Typed) Interact::keyTyped);
}
public static String mapText(String text) {
return text.isEmpty()
? "1"
: String.valueOf(Long.valueOf(text) + 1)
;
}
public static void actionPerformedOnIncrementButton(ActionEvent event) {
Stream.of(FIELD)
.parallel()
.map(JTextField::getText)
.map(Interact::mapText)
.forEach(FIELD::setText)
;
}
public static void addActionListenerToIncrementButton(JButton button) {
button.addActionListener(Interact::actionPerformedOnIncrementButton);
}
public static void addIncrementButton(JPanel panel) {
Stream.of("Increment")
.parallel()
.map(JButton::new)
.peek(Interact::addActionListenerToIncrementButton)
.forEach(panel::add)
;
}
public static int showConfirmDialog(String question) {
return JOptionPane.showConfirmDialog(null, question);
}
public static void setFieldText(int integer) {
FIELD.setText(
String.valueOf(
(long) (Math.random() * Long.MAX_VALUE))
)
;
}
public static void actionPerformedOnRandomButton(ActionEvent event) {
Stream.of("Are you sure?")
.parallel()
.map(Interact::showConfirmDialog)
.filter(Predicate.isEqual(JOptionPane.YES_OPTION))
.forEach(Interact::setFieldText)
;
}
public static void addActionListenerToRandomButton(JButton button) {
button.addActionListener(Interact::actionPerformedOnRandomButton);
}
public static void addRandomButton(JPanel panel) {
Stream.of("Random")
.parallel()
.map(JButton::new)
.peek(Interact::addActionListenerToRandomButton)
.forEach(panel::add)
;
}
public static void acceptField(Consumer<JTextField> consumer) {
consumer.accept(FIELD);
}
public static void prepareField(JTextField field) {
Stream.<Consumer<JTextField>>of(
Interact::setEditable,
Interact::setText,
Interact::addKeyListener
)
.parallel()
.forEach(Interact::acceptField)
;
}
public static void addField(JFrame frame) {
Stream.of(FIELD)
.parallel()
.peek(Interact::prepareField)
.forEach(frame::add)
;
}
public static void acceptPanel(Consumer<JPanel> consumer) {
consumer.accept(PANEL);
}
public static void processPanel(JPanel panel) {
Stream.<Consumer<JPanel>>of(
Interact::setLayout,
Interact::addIncrementButton,
Interact::addRandomButton
)
.parallel()
.forEach(Interact::acceptPanel)
;
}
public static void addPanel(JFrame frame) {
Stream.of(PANEL)
.parallel()
.peek(Interact::processPanel)
.forEach(frame::add)
;
}
public static void setLayout(JFrame frame) {
frame.setLayout(new GridLayout(2, 1));
}
public static void setLayout(JPanel panel) {
panel.setLayout(new GridLayout(1, 2));
}
public static void setVisible(JFrame frame) {
frame.setVisible(true);
}
public static void acceptFrame(Consumer<JFrame> consumer) {
consumer.accept(FRAME);
}
public static void processField(JFrame frame) {
Stream.<Consumer<JFrame>>of(
Interact::setDefaultCloseOperation,
Interact::setLayout,
Interact::addField,
Interact::addPanel,
Interact::setVisible
)
.parallel()
.forEach(Interact::acceptFrame)
;
}
public static void main(String... arguments) {
Stream.of(FRAME)
.parallel()
.peek(Interact::processField)
.forEach(JFrame::pack)
;
}
}
You may also check:How to resolve the algorithm String case step by step in the min programming language
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the C# programming language
You may also check:How to resolve the algorithm Closest-pair problem step by step in the Ruby programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the Relation programming language
You may also check:How to resolve the algorithm Factorial step by step in the SASL programming language