In: Computer Science
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.
NamesList.java (Model class)
import java.util.ArrayList;
public class NamesList {
private static ArrayList
public static ArrayList
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
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.