Question

In: Computer Science

Develop a GUI to get unique names from the user.


in JAVA

Develop a GUI to get unique names from the user. 

. In the first frame use a label ("Name") a textfield and 2 buttons (ext' and 'inished") 

. In the second frame there should be a list and a scrollpane as the components 

. Use the components on the first frame to get the name value from the user. 

. When the user enters a name make sure it is not empty and has no spaces and dicks the next' button, reset the textfield and add the name to a list (Array or Arraylist up to you . 

. The user can enter as many names as he/she wishes as long as there are no duplicate names uppercase (java) and lowercase Cava") names should be considered the same . 

. If there is a duplicate, gve a warning to the user using a message box and do not add it to the list . 

. When the user clicks the finished' button open the second frame to display the list of names that are entered . 

. If needed, use some exception handling methods along the way . 

. You are not allowed to use the window builder for the GUI questions.

Solutions

Expert Solution

NamesList.java (Model class)

import java.util.ArrayList;

public class NamesList {
private static ArrayList names = new ArrayList<>();

public static ArrayList getNames() {
return names;
}
  
public static boolean add(String name)
{
if(find(name) == -1) // name found
{
names.add(name);
return true;
}
return false;
}
  
private static int find(String name)
{
int index = -1;
for(int i = 0; i < names.size(); i++)
{
if(names.get(i).equalsIgnoreCase(name))
{
index = i;
break;
}
}
return index;
}
}

InputNamesFrame.java (Frame 1)

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class InputNamesFrame extends JFrame {
private static JPanel mainPanel, inpPanel, buttonPanel;
private static JLabel nameLabel;
private static JTextField nameField;
private static JButton nextButton, finishedButton;
private static NamesList namesList;
  
public InputNamesFrame()
{
namesList = new NamesList();
  
mainPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
  
inpPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
nameLabel = new JLabel("Name: ");
nameField = new JTextField(20);
inpPanel.add(nameLabel);
inpPanel.add(nameField);
  
buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
nextButton = new JButton("Next");
finishedButton = new JButton("Finished");
buttonPanel.add(nextButton);
buttonPanel.add(finishedButton);
  
mainPanel.add(inpPanel);
mainPanel.add(buttonPanel);
  
add(mainPanel);
setTitle("Input Names");
setSize(300, 120);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
  
// action listener for buttons
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(nameField.getText().equals("") || nameField.getText().contains(" "))
{
JOptionPane.showMessageDialog(null, "Please enter a name (not empty and should not "
+ "contain any spaces)!");
return;
}
String name = nameField.getText().trim();
boolean status = NamesList.add(name);
if(!status)
JOptionPane.showMessageDialog(null, name + " is already present in the list!");
nameField.setText("");
}
});
  
finishedButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(NamesList.getNames().isEmpty())
{
JOptionPane.showMessageDialog(null, "List is empty! Please enter some names to"
+ " naviage to the next frame.");
return;
}
dispose();
new ViewNamesFrame().setVisible(true);
}
});
}
}

ViewNamesFrame.java (Frame 2)

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ViewNamesFrame extends JFrame {
  
private static JPanel mainPanel;
private static JList listView;
private static JScrollPane scrollPane;
private static JButton backButton, exitButton;
  
public ViewNamesFrame()
{
NamesList namesList = new NamesList();
mainPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
ArrayList names = namesList.getNames();
  
listView = new JList(names.toArray());
scrollPane = new JScrollPane(listView);
scrollPane.setPreferredSize(new Dimension(200, 200));
  
JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER));
backButton = new JButton("Back");
exitButton = new JButton("Exit");
p.add(backButton);
p.add(exitButton);
  
mainPanel.add(scrollPane);
mainPanel.add(p);
  
add(mainPanel);
setSize(250, 300);
setTitle("View Names");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
  
// action listener for the buttons
backButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
new InputNamesFrame().setVisible(true);
}
});
  
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}

NamesListViewerMain.java (Driver class)

public class NamesListViewerMain {
  
public static void main(String[] args) {
new InputNamesFrame();
}
}

**************************************************************** SCREENSHOT ***********************************************************

FRAME 1 :

Attempting to add a duplicate name:

Attempting to add a name containing space:

FRAME 2 :

The Back button navigates back to Frame 1 and you can continue adding names to the list.


Related Solutions

Develop a JavaFX GUI application called Registration that implements a user interface for registering for a...
Develop a JavaFX GUI application called Registration that implements a user interface for registering for a web site. The application should have labeled text fields for the Full Name, User Name, Password, Student Id, and a TextAreafor About Me. Include a button labeled Send. When the Send button is clicked, your program should print the contents of all fields (with labels) to standard output using println()statements.
For this assignment, you will develop working examples of a graphical user interface (GUI) and event...
For this assignment, you will develop working examples of a graphical user interface (GUI) and event handling and that demonstrate the following: Working code with screenshots of a Python GUI application that includes 5 design widgets of your choosing Working code with screenshots of event handling in Python based on 3 events of your choosing Be sure to include a brief narrative of your code where you explain what the code is doing. Documentation Guidelines: Use good programming style (e.g.,...
Write a program called listful.py, in which the user inputs names, which get added to a...
Write a program called listful.py, in which the user inputs names, which get added to a list. Your program should: · Include a comment in the first line with your name. · Include comments describing each major section of code. · Create a list. · Ask the user to input a first name or hit enter to end the list. · If the user adds a first name (i.e., anything other than a blank value): o Add the name to...
modify the code below to create a GUI program that accepts a String from the user...
modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above. N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make...
in c, write a probram thats will read from the user: id, names, and marks for...
in c, write a probram thats will read from the user: id, names, and marks for a chosen number of students. the program should he able to read the total number of students from the user. Then should be able to read the id, name, and marks for each student and store it into three arrays, where each record is given in a line of text, with id, name, and marks seperated by a comma. The use of scanf is...
. Use a validation loop to prompt for and get from the user a string that...
. Use a validation loop to prompt for and get from the user a string that is up to ten characters in length. Create value function encodedString that takes the string as input and returns the equivalent encoded string. Start with an empty encoded string. Use a for loop to encode each character of the input string. Use the following algorithm to encode a character and append the encoded character to the encoded string:
Write a java code to (A) Enter the number of children, (B) Insert names from user,...
Write a java code to (A) Enter the number of children, (B) Insert names from user, (C) Print these names. upload source code with a screenshotfor output in one file with cover page
(write a program that get the numbers from user and search the file numbers.text for that...
(write a program that get the numbers from user and search the file numbers.text for that value. in C++) numbers.txt: 10 23 43 5 12 23 9 8 10 1 16 9 you must to have the exact output: Enter a number: 10 10 last appears in the file at position 9 Enter a number: 29 29 does not appear in the file Enter a number: 9 9 last appears in the file at position 12 Enter a number:
Prompt the user for their name, get and store the user input. Prompt the user for...
Prompt the user for their name, get and store the user input. Prompt the user for their age, get and store the user input. We will assume that the user will enter a positive integer and will do no error checking for valid input. Determine and store a movie ticket price based on the user's age. If their age is 12 or under, the ticket price is $5. If their age is between 13 and 64, inclusive, the ticket price...
Write a program that takes a string from the user, identifies and counts all unique characters...
Write a program that takes a string from the user, identifies and counts all unique characters in that given string. You are bound to use only built-in string functions where necessary. For identification of unique characters and for counting of the characters make separate functions. For character identification Develop a program that takes a string argument, and returns an array containing all unique characters. For character counting Develop a program that takes an array returned from above function as an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT