plug and run(copy and paste)

edit ken delete
 avatar
unknown
java
16 days ago
4.2 kB
17
Indexable
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

class TaenaTable {
  public static int lastElement = 1;
  public static DefaultTableModel model = new DefaultTableModel();

  public static void main(String[] args) {
    JFrame frame = new JFrame("Taena mo");
    JTable tb = new JTable();
    JScrollPane sp = new JScrollPane(tb);
    JOptionPane dialog = new JOptionPane();
    JTextField id = new JTextField();
    JTextField username = new JTextField();
    JTextField password = new JTextField();
    JButton submit = new JButton("Submit");

    JRadioButton save = new JRadioButton("Save");
    JRadioButton edit = new JRadioButton("Edit");
    JRadioButton delete = new JRadioButton("Delete");
    ButtonGroup actionGroup = new ButtonGroup();

    actionGroup.add(save);
    actionGroup.add(edit);
    actionGroup.add(delete);

    id.setBounds(10, 10, 150, 30);
    username.setBounds(10, 50, 150, 30);
    password.setBounds(10, 90, 150, 30);

    model.addColumn("ID");
    model.addColumn("Username");
    model.addColumn("Password");

    tb.setModel(model);
    sp.setBounds(180, 10, 300, 250);

    save.setBounds(10, 130, 150, 30);
    edit.setBounds(10, 160, 150, 30);
    delete.setBounds(10, 190, 150, 30);
    submit.setBounds(10, 230, 150, 30);

    save.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        id.setEditable(false);
        id.setText(String.valueOf(lastElement));
      }
    });

    edit.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        id.setEditable(true);
      }
    });

    delete.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        id.setEditable(true);
      }
    });

    submit.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String uid = id.getText();
        String user = username.getText();
        String pass = password.getText();

        if (save.isSelected()) {
          if (user.isEmpty() || pass.isEmpty()) {
            dialog.showMessageDialog(dialog, "Please fill up all fields");
            return;
          }

          String[] data = { String.valueOf(lastElement), user, pass };
          model.addRow(data);
          lastElement++;
          id.setText(String.valueOf(lastElement));
          username.setText("");
          password.setText("");
        } else if (edit.isSelected()) {
          if (uid.isEmpty() || user.isEmpty() || pass.isEmpty()) {
            dialog.showMessageDialog(dialog, "Please fill up all fields");
            return;
          }

          int rowIndex = findId(uid);
          if (rowIndex == -1) {
            dialog.showMessageDialog(dialog, "ID not found!");
            return;
          }

          model.setValueAt(user, rowIndex, 1);
          model.setValueAt(pass, rowIndex, 2);

          id.setText("");
          username.setText("");
          password.setText("");
        } else if (delete.isSelected()) {
          if (uid.isEmpty()) {
            dialog.showMessageDialog(dialog, "Please enter an ID to delete");
            return;
          }

          int rowIndex = findId(uid);
          if (rowIndex == -1) {
            dialog.showMessageDialog(dialog, "ID not found!");
            return;
          }

          model.removeRow(rowIndex);
          id.setText("");
          username.setText("");
          password.setText("");
        } else {
          dialog.showMessageDialog(dialog, "Please select an action.");
        }
      }
    });

    frame.add(id);
    frame.add(username);
    frame.add(password);
    frame.add(save);
    frame.add(edit);
    frame.add(delete);
    frame.add(submit);
    frame.add(sp);

    frame.setSize(500, 300);
    frame.setResizable(false);
    frame.setLayout(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  private static int findId(String id) {
    for (int i = 0; i < model.getRowCount(); i++) {
      if (model.getValueAt(i, 0).toString().equals(id)) {
        return i;
      }
    }
    return -1;
  }
}
Editor is loading...
Leave a Comment