Skip to main content

I want to add, search, edit, delete record of employee

2 replies [Last post]
jacobpham
Offline
Joined: 2010-07-15

Hi,

I want to add record of employees save to file .txt, then search edit and delete
but i don't know how to do it
Code:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;

public class manager {
private JFrame f;
private JTabbedPane tp;
private JPanel p1, p2, p3, p4;
private JButton btn1, btn2;
private JLabel l1, l2, l3;
private JTextField t1, t2, t3;

manager()
{
p1 = new JPanel(new GridLayout(4,2));
p2 = new JPanel(new GridLayout(3,3));
p3 = new JPanel(new GridLayout(3,3));
p4 = new JPanel(new GridLayout(3,3));

btn1 = new JButton("Submit");
btn2 = new JButton("Reset");

p1.add(l1 = new JLabel("Employee ID"));
p1.add(t1 = new JTextField(10));
p1.add(l2 = new JLabel("Employee Name"));
p1.add(t2 = new JTextField(10));
p1.add(l3 = new JLabel("Employee Address"));
p1.add(t3 = new JTextField(10));
p1.add(btn1);
p1.add(btn2);

btn1.addActionListener(new ActionListener()
{
public void ActionPerformed(ActionEvent ae)
{
String s1 = t1.getText();
String s2 = t2.getText();
String s3 = t3.getText();

if(s1=="" || s2=="" || s3=="")
{
return;
}

}
});

btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
t1.setText("");
t2.setText("");
t3.setText("");
}
});

}

public void launchFrame()
{
JFrame.setDefaultLookAndFeelDecorated(true);
f = new JFrame("Hello khakhakha!");

tp = new JTabbedPane();

Container c = f.getContentPane();
c.add(tp);

tp.addTab("Add record", p1);
tp.addTab("Edit record", p2);
tp.addTab("Search record", p3);
tp.addTab("Delete record", p4);

f.pack();
//f.setSize(400,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String args[]) {
manager guiWindow = new manager();
guiWindow.launchFrame();
}
}

Can you help me!
Thanks a lot.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
simpleprogrammer
Offline
Joined: 2010-03-16

You got all the GUI. The next stage seems creating the file and then inserting the data into it.
http://www.abbeyworkshop.com/howto/java/writeText/index.html
http://www.javafaq.nu/java-example-code-126.html
I don't know how good it is to utilize text file to store employee information, if you're building such a thing. Also I do think that it will be cumbersome to write code to access retrieve and maintain such a file. (Sorry no experience in such a way and never tried it) It's better to have a database and then create tables in it. Then write code for saving the GUI data in database and your app is ready.
Something like this
http://www.javaworkspace.com/connectdatabase/connectMysql.do

wingetr
Offline
Joined: 2004-01-19

I'd suggest using a CMV (Control, Model, View) pattern. The Control contains the business logic, the Model represents your data, and the View is the GUI.

I like starting with the Model. In your case, it looks like you need an Employee class with id, name, and name as fields. Never access the fields directly - use getters and setters.

Your Control will handle all file operations - adding a new employee, searching for an employee, updating an employee, and deleting an employee.

You've already starting on the View, but it is customary to use TitleCase for classes and camelCase for instance variables.

Your plea sounds a lot like homework, so I'll let you try figure some stuff out using my answer as a guideline. if you have specific questions, go ahead and ask in this thread.