package Project;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.lang.*;
/**
* This program will recieve the IP, PCI and client name of client computers and save them to a text file.
*
* @author Marko Klesnik
* @version 1.0
*/
public class project implements ActionListener
{
// Variable Declarations
// Declare JButtons
public JButton btnNew, btnSave, btnFind, btnDelete, btnExit;
// Declare JLabels
public JLabel lblName, lblIP, lblPCI, lblDesc, lblDesc2, lblDesc3;
// Declare JTextFields
public JTextField txtName, txtIP, txtPCI;
// Declare a scanner
private Scanner scanRead;
// Declare a formatter
private Formatter formatWrite;
// Create a string array
public String [] clientList;
public static void main(String[] args)
{
project test = new project();
test.init();
}
public void init ()
{
// Define default JButton data
btnNew = new JButton("New Entry ");
btnNew.setBackground(Color.BLACK);
btnNew.setForeground(Color.GREEN);
btnSave = new JButton("Save Entry");
btnSave.setBackground(Color.BLACK);
btnSave.setForeground(Color.GREEN);
btnFind = new JButton("Find Entry");
btnFind.setBackground(Color.BLACK);
btnFind.setForeground(Color.GREEN);
btnDelete = new JButton("Delete Entry");
btnDelete.setBackground(Color.BLACK);
btnDelete.setForeground(Color.GREEN);
btnExit = new JButton("Exit System");
btnExit.setBackground(Color.BLACK);
btnExit.setForeground(Color.RED);
// Define default JLabel data
lblName = new JLabel("Client Name:");
lblName.setForeground(Color.WHITE);
lblIP = new JLabel("IP Address:");
lblIP.setForeground(Color.WHITE);
lblPCI = new JLabel("PC Identifier:");
lblPCI.setForeground(Color.WHITE);
lblDesc = new JLabel(" This program will allow the user to add ");
lblDesc.setForeground(Color.WHITE);
lblDesc2 = new JLabel(" edit or delete Users.their PC Identifiers ");
lblDesc2.setForeground(Color.WHITE);
lblDesc3 = new JLabel(" and their PC's IP Address. ");
lblDesc3.setForeground(Color.WHITE);
//Define default JTextField data
txtName = new JTextField("Client1", 10);
txtName.setBackground(Color.BLACK);
txtName.setForeground(Color.GREEN);
txtIP = new JTextField("", 10);
txtIP.setBackground(Color.BLACK);
txtIP.setForeground(Color.GREEN);
txtPCI = new JTextField("", 10);
txtPCI.setBackground(Color.BLACK);
txtPCI.setForeground(Color.GREEN);
//Create container + Layout
SpringLayout scnLayout = new SpringLayout();
// Create a frame and apply settings
JFrame progFrame = new JFrame("[COMPANY NAME] Client Recording Software");
progFrame.setVisible(true);
progFrame.setSize(900, 800);
progFrame.setResizable(false);
// Create a JPanel and apply settings
JPanel panel1 = new JPanel();
panel1.setVisible(true);
panel1.setSize(200, 300);
panel1.setBackground(Color.black);
panel1.setLayout(scnLayout);
// Add panels to frame
progFrame.add(panel1);
// Add components to panels
panel1.add(lblName);
panel1.add(txtName);
panel1.add(btnNew);
panel1.add(lblPCI);
panel1.add(txtPCI);
panel1.add(lblIP);
panel1.add(txtIP);
panel1.add(btnSave);
panel1.add(btnFind);
panel1.add(btnDelete);
panel1.add(btnExit);
panel1.add(lblDesc);
panel1.add(lblDesc2);
panel1.add(lblDesc3);
// Set locations of panel1 components
scnLayout.putConstraint(SpringLayout.WEST, lblName, 350, SpringLayout.WEST, panel1);
scnLayout.putConstraint(SpringLayout.NORTH, lblName, 200, SpringLayout.NORTH, panel1);
scnLayout.putConstraint(SpringLayout.NORTH, txtName, 200, SpringLayout.NORTH, panel1);
scnLayout.putConstraint(SpringLayout.WEST, txtName, 35, SpringLayout.EAST, lblName);
scnLayout.putConstraint(SpringLayout.NORTH, btnNew, 10, SpringLayout.NORTH, panel1);
scnLayout.putConstraint(SpringLayout.WEST, btnNew, 25, SpringLayout.WEST, panel1);
scnLayout.putConstraint(SpringLayout.NORTH, lblPCI, 250, SpringLayout.NORTH, panel1);
scnLayout.putConstraint(SpringLayout.WEST, lblPCI, 350, SpringLayout.WEST, panel1);
scnLayout.putConstraint(SpringLayout.NORTH, txtPCI, 250, SpringLayout.NORTH, panel1);
scnLayout.putConstraint(SpringLayout.WEST, txtPCI, 35, SpringLayout.EAST, lblPCI);
scnLayout.putConstraint(SpringLayout.NORTH, lblIP, 300, SpringLayout.NORTH, panel1);
scnLayout.putConstraint(SpringLayout.WEST, lblIP, 350, SpringLayout.WEST, panel1);
scnLayout.putConstraint(SpringLayout.NORTH, txtIP, 300, SpringLayout.NORTH, panel1);
scnLayout.putConstraint(SpringLayout.WEST, txtIP, 45, SpringLayout.EAST, lblIP);
scnLayout.putConstraint(SpringLayout.NORTH, btnSave, 10, SpringLayout.NORTH, panel1);
scnLayout.putConstraint(SpringLayout.WEST, btnSave, 150, SpringLayout.EAST, btnNew);
scnLayout.putConstraint(SpringLayout.NORTH, btnFind, 10, SpringLayout.NORTH, panel1);
scnLayout.putConstraint(SpringLayout.EAST, btnFind, -150, SpringLayout.WEST, btnDelete);
scnLayout.putConstraint(SpringLayout.NORTH, btnDelete, 10, SpringLayout.NORTH, panel1);
scnLayout.putConstraint(SpringLayout.EAST, btnDelete, -25, SpringLayout.EAST, panel1);
scnLayout.putConstraint(SpringLayout.EAST, btnExit, -25, SpringLayout.EAST, panel1);
scnLayout.putConstraint(SpringLayout.SOUTH, btnExit, -20, SpringLayout.SOUTH, panel1);
scnLayout.putConstraint(SpringLayout.WEST, lblDesc, 350, SpringLayout.WEST, panel1);
scnLayout.putConstraint(SpringLayout.NORTH, lblDesc, 80, SpringLayout.NORTH, panel1);
scnLayout.putConstraint(SpringLayout.WEST, lblDesc2, 350, SpringLayout.WEST, panel1);
scnLayout.putConstraint(SpringLayout.NORTH, lblDesc2, 10, SpringLayout.SOUTH, lblDesc);
scnLayout.putConstraint(SpringLayout.WEST, lblDesc3, 385, SpringLayout.WEST, panel1);
scnLayout.putConstraint(SpringLayout.NORTH, lblDesc3, 10, SpringLayout.SOUTH, lblDesc2);
btnExit.addActionListener(this);
btnNew.addActionListener(this);
btnSave.addActionListener(this);
btnFind.addActionListener(this);
btnDelete.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
// Set what haphens on button clicks
//New Entry button clicked
if (btnNew.hasFocus())
{
txtName.setText("");
txtPCI.setText("");
txtIP.setText("");
}
//Save Entry button clicked
if (btnSave.hasFocus())
{
try
{
formatWrite = new Formatter("ipaddress.txt");
}
catch (Exception outputTC)
{
System.out.println("Cannot write to file");
}
formatWrite.format("%s%s%s", txtName.getText() + " ", txtPCI.getText() + " ", txtIP.getText());
formatWrite.close();
}
//Find Entry button clicked
if (btnFind.hasFocus())
{
try
{
scanRead = new Scanner(new File("ipaddress.txt"));
}
catch(Exception inTC)
{
System.out.println("Could not find file");
}
while(scanRead.hasNext())
{
String readA = scanRead.next();
String readB = scanRead.next();
String readC = scanRead.next();
txtName.setText(readA);
txtPCI.setText(readB);
txtIP.setText(readC);
}
scanRead.close();
}
//Delete Entry button clicked
if (btnDelete.hasFocus())
{
}
//Exit button clicked
if (btnExit.hasFocus()) { System.exit(0); }
}
}