Get User Input From a JTextField and Validate it Using Java (GUI JFrame)

preview_player
Показать описание
Get User Input From a JTextField and Validate it Using Java (GUI JFrame)

Greetings, in this Java tutorial, we shall be looking at how to get user input from a Java JTextField on a JFrame, validating that user input and telling the user the result of that validation.

A JTextField is basically a textbox.

You will learn about action listeners, events, overriding, inheriting and more in this short Java GUI Jframe tutorial.

Thanks for watching this Java tutorial on how to get user input from a JTextField and validating it.

Get User Input From a JTextField and Validate it Using Java (GUI JFrame)
Рекомендации по теме
Комментарии
Автор

i couldnt focus on learning because of all the screeming

santamak
Автор

Mr max o sir
Please do a video about this code .


import java.util.Scanner;
import javax.swing.JOptionPane;

public class EasyKanban {

private static final String WELCOME_MESSAGE = "Welcome to EasyKanban";
private static final String[] TASK_STATUS_OPTIONS = {"To Do", "Done", "Doing"};
private static int numTasks;

public static void main(String[] args) {
JOptionPane.showMessageDialog(null, WELCOME_MESSAGE);
login();
numTasks = readNumTasks();
Task[] tasks = new Task[numTasks];
for (int i = 0; i < numTasks; i++) {
tasks[i] = readTask(i);
JOptionPane.showMessageDialog(null, tasks[i].printTaskDetails());
}
JOptionPane.showMessageDialog(null, "Total hours: " + getTotalHours(tasks));
}

private static void login() {
boolean success = false;
while (!success) {
String username = JOptionPane.showInputDialog(null, "Enter username:");
String password = JOptionPane.showInputDialog(null, "Enter password:");
if (username.equals("admin") && password.equals("password")) {
success = true;
} else {
JOptionPane.showMessageDialog(null, "Invalid login, please try again.");
}
}
}

private static int readNumTasks() {
String input = JOptionPane.showInputDialog(null, "Enter number of tasks:");
return Integer.parseInt(input);
}

private static Task readTask(int taskNumber) {
String taskName = JOptionPane.showInputDialog(null, "Enter task name for Task " + taskNumber + ":");
String taskDescription = "";
boolean valid = false;
while (!valid) {
taskDescription = JOptionPane.showInputDialog(null, "Enter task description for Task " + taskNumber + ":");
valid =
if (!valid) {
JOptionPane.showMessageDialog(null, "Please enter a task description of less than 50 characters");
}
}
String developerDetails = JOptionPane.showInputDialog(null, "Enter developer details for Task " + taskNumber + ":");
String taskDuration = JOptionPane.showInputDialog(null, "Enter task duration (hours) for Task " + taskNumber + ":");
Task task = new Task(taskName, taskDescription, developerDetails,
return task;
}

private static int getTotalHours(Task[] tasks) {
int totalHours = 0;
for (Task task : tasks) {
totalHours += task.getTaskDuration();
}
return totalHours;
}

private static class Task {

private static int nextTaskNumber = 0;
private String taskName;
private int taskNumber;
private String taskDescription;
private String developerDetails;
private int taskDuration;
private String taskID;
private String taskStatus;

public Task(String taskName, String taskDescription, String developerDetails, int taskDuration) {
this.taskName = taskName;
this.taskNumber = nextTaskNumber++;
this.taskDescription = taskDescription;
this.developerDetails = developerDetails;
this.taskDuration = taskDuration;
this.taskID = createTaskID();
this.taskStatus = TASK_STATUS_OPTIONS[0];
}

public static boolean checkTaskDescription(String taskDescription) {
return taskDescription.length() <= 50;
}

public String createTaskID() {
String firstTwoLetters = taskName.substring(0, Math.min(taskName.length(), 2));
String lastThreeLetters = - 3


It show an error when user click CLOSE button

kmow_illonesotho
welcome to shbcf.ru