I would like to add a Jtable into my ordering system. And the function if the page should be like this :
Staff need to enter the details in text field to add a customer, when the staff successfully enter the data of the customer, the data will auto insert to table and into the text file. If the staff wants to update a customer account, the staff need to click the user details from the table, when they click the details of the customer in the table, the data will auto enter to the text field and staff can modify the details and click on update button. So the text file also will be updated.
What I have tried:
import javax.swing.*;
import java.awt.event.*;
import java.io.FileWriter;
import javafx.scene.paint.Color;
import javax.swing.table.DefaultTableModel;
public class customer extends JFrame{
public static void main(String[] args) {
customer frameTabel = new customer();
}
JButton add = new JButton("Add");
JButton update = new JButton("Update");
JButton back = new JButton("Back");
JPanel panel = new JPanel();
JLabel cid = new JLabel("Customer ID:");
JLabel name = new JLabel("Name:");
JLabel number = new JLabel("Contact Number:");
JTextField textcid = new JTextField(15);
JTextField textname = new JTextField(15);
JTextField textnumber = new JTextField(15);
customer(){
super("Customer Account");
setSize(400,400);
setLocation(500,280);
setResizable(false);
panel.setLayout(null);
cid.setBounds(70, 20, 100, 30);
name.setBounds(70, 50, 100, 30);
number.setBounds(70, 80, 100, 30);
textcid.setBounds(170, 25, 150, 20);
textname.setBounds(170, 55, 150, 20);
textnumber.setBounds(170, 85, 150, 20);
add.setBounds(70, 120, 80, 20);
update.setBounds(160, 120, 80, 20);
back.setBounds(270, 330, 80, 20);
panel.add(add);
panel.add(update);
panel.add(back);
panel.add(cid);
panel.add(name);
panel.add(number);
panel.add(textcid);
panel.add(textname);
panel.add(textnumber);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionadd();
actionback();
}
public void actionback(){
back.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
setVisible(false);
new main();
}});}
public void actionadd(){
add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String cid = textcid.getText().toString();
String name = textname.getText().toString();
String number = textnumber.getText().toString();
try {
FileWriter writer = new FileWriter("customer.txt", true);
writer.write(cid);
writer.write(System.getProperty("line.separator"));
writer.write(name);
writer.write(System.getProperty("line.separator"));
writer.write(number);
writer.write(System.getProperty("line.separator"));
writer.close();
JOptionPane.showMessageDialog(rootPane, "Success");
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, "Error");
}
}
});
}
}