Make Table with Update,Delete,Reset Buttons in Java Swing
Make Table with Update,Delete,Reset Buttons in Java Swing:
https://youtu.be/Gk97GrM1z-M ________________________________________________ How to make Table with Update,Delete,Reset Buttons in Java Swing?package Test;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class InformationTable {
public static void main(String[] args){
JFrame frame = new JFrame("Information Table");
frame.setBounds(600,100,500,300);
frame.setVisible(true);
frame.setLayout(null);
JTable table = new JTable();
Object[] columns = {"Id","First Name","Last Name","Age"};
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(columns);
table.setModel(model);
Object[] row = new Object[4];
JScrollPane sp = new JScrollPane(table);
sp.setBounds(0, 0, 480, 100);
JLabel lblId = new JLabel("Id:");
JLabel lblFname = new JLabel("First name:");
JLabel lblLname = new JLabel("Last name:");
JLabel lblAge = new JLabel("Age:");
lblId.setBounds(10,130,15,25);
lblFname.setBounds(10,160,100,25);
lblLname.setBounds(10,190,100,25);
lblAge.setBounds(10,220,100,25);
JTextField txtId = new JTextField();
JTextField txtFname = new JTextField();
JTextField txtLname = new JTextField();
JTextField txtAge = new JTextField();
txtId.setBounds(100, 130, 100, 25);
txtFname.setBounds(100, 160, 100, 25);
txtLname.setBounds(100, 190, 100, 25);
txtAge.setBounds(100, 220, 100, 25);
JButton btnAdd = new JButton("Add");
JButton btnDelete = new JButton("Delete");
JButton btnUpdate = new JButton("Update");
JButton btnReset = new JButton("Reset");
btnAdd.setBounds(220, 130, 100, 25);
btnUpdate.setBounds(220, 160, 100, 25);
btnDelete.setBounds(220, 190, 100, 25);
btnReset.setBounds(220,220,100,25);
btnAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
row[0] = txtId.getText();
row[1] = txtFname.getText();
row[2] = txtLname.getText();
row[3] = txtAge.getText();
model.addRow(row);
}
});
btnDelete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int i = table.getSelectedRow();
if(i >= 0){
model.removeRow(i);
}
else{
System.out.println("Delete Error");
}
}
});
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
int i = table.getSelectedRow();
txtId.setText(model.getValueAt(i, 0).toString());
txtFname.setText(model.getValueAt(i, 1).toString());
txtLname.setText(model.getValueAt(i, 2).toString());
txtAge.setText(model.getValueAt(i, 3).toString());
}
});
btnUpdate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int i = table.getSelectedRow();
if(i >= 0)
{
model.setValueAt(txtId.getText(), i, 0);
model.setValueAt(txtFname.getText(), i, 1);
model.setValueAt(txtLname.getText(), i, 2);
model.setValueAt(txtAge.getText(), i, 3);
}
else{
System.out.println("Update Error");
}
}
});
btnReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtId.setText("");
txtFname.setText("");
txtLname.setText("");
txtAge.setText("");
}
});
frame.add(sp);
frame.add(lblId);
frame.add(lblFname);
frame.add(lblLname);
frame.add(lblAge);
frame.add(txtId);
frame.add(txtFname);
frame.add(txtLname);
frame.add(txtAge);
frame.add(btnAdd);
frame.add(btnDelete);
frame.add(btnUpdate);
frame.add(btnReset);
}
}
Dark Hers
YouTube
https://instagram.com/darkhers
https://www.facebook.com/darkhers
TikTok
https://www.tiktok.com/@darkhers
https://www.pinterest.co.uk/darkhers
Comments
Post a Comment