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.
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...
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...
• 2. Get all the information from the user using methods Java • B. if the...
• 2. Get all the information from the user using methods Java • B. if the inputs are not given in the proper format the program should prompt user to give the proper input (eg. Name cannot be numbers, age cannot be String)
Why does the laser get a unique wavelength?
Why does the laser get a unique wavelength?
Create in java an interactive GUI application program that will prompt the user to use one...
Create in java an interactive GUI application program that will prompt the user to use one of three calculators, label project name MEDCALC. You can use any color you want for your background other than grey. Be sure to label main java box with “MEDCALC” and include text of “For use only by students” On all three calculators create an alert and signature area for each user. The alert should be something like “All calculations must be confirmed by user...
The purpose of this problem is to use graphic user interface (GUI) to interactively store grades...
The purpose of this problem is to use graphic user interface (GUI) to interactively store grades in a text file, rather than adding them manually to a script. Follow these steps to complete the program: Step 1. Use a question dialog box and ask the user “Do you want to run this Program?” with two options for “yes” and “no”. If the user’s answer is “yes”, go to the next step, otherwise, go to Step 6. Step 2. Create a...
Write a GUI that allows the user to do the following: Create a new Patient Database...
Write a GUI that allows the user to do the following: Create a new Patient Database if it doesn’t exist yet by the click of a button. Create a second button that populates the database with the appropriate Asset table if it does not exist yet, and fill the table with at least 10 patients. Connect to the Patient Database and display all current patients in a List by default. You will have to create a Patient Class. This class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT