Untitled

 avatar
unknown
java
3 years ago
2.7 kB
7
Indexable
package Swing;

import javax.swing.*;
import java.awt.*;

public class EditTable extends JDialog {

    public static String id, name, note;

    public EditTable(String id, String name, String note) {
        this.setModal(true);

        EditTable.id = id;
        EditTable.name = name;
        EditTable.note = note;

        addControls();
        addEvents();
    }


    private JTextField txtId, txtName;
    private JTextArea txtNote;
    private JButton btnSave;

    private void addControls() {
        Container con = getContentPane();

        JPanel pnMain = new JPanel();
        pnMain.setLayout(new BoxLayout(pnMain, BoxLayout.Y_AXIS));

        JPanel pnTextField = new JPanel();
        pnTextField.setLayout(new BoxLayout(pnTextField, BoxLayout.Y_AXIS));

        JPanel pnId = new JPanel();
        JLabel lblId = new JLabel("MSSV*", SwingConstants.RIGHT);
        txtId = new JTextField(20);
        pnId.add(lblId);
        pnId.add(txtId);

        JPanel pnName = new JPanel();
        JLabel lblName = new JLabel("Họ tên*", SwingConstants.RIGHT);
        txtName = new JTextField(20);
        pnName.add(lblName);
        pnName.add(txtName);

        JPanel pnNote = new JPanel();
        JLabel lblNote = new JLabel("Ghi chú", SwingConstants.RIGHT);
        txtNote = new JTextArea(5, 20);
        pnNote.add(lblNote);
        pnNote.add(txtNote);

        pnTextField.add(pnId);
        pnTextField.add(pnName);
        pnTextField.add(pnNote);

        lblId.setPreferredSize(lblNote.getPreferredSize());
        lblName.setPreferredSize(lblNote.getPreferredSize());

        pnMain.add(pnTextField);

        JPanel pnButton = new JPanel();
        btnSave = new JButton("Save");
        pnButton.add(btnSave);
        pnMain.add(pnButton);

        txtId.setText(id);
        txtName.setText(name);
        txtNote.setText(note);

        con.add(pnMain);
    }

    private void addEvents() {
        btnSave.addActionListener(e -> {
            String id = txtId.getText();
            String name = txtName.getText();
            String note = txtNote.getText();

            if (id.trim().length() == 0 || name.trim().length() == 0) {
                JOptionPane.showMessageDialog(this, "Please enter id and name");
                return;
            }

            EditTable.id = id;
            EditTable.name = name;
            EditTable.note = note;

            JOptionPane.showMessageDialog(this, "Success");

            this.dispose();
        });
    }

    public void showWindow() {
        this.setTitle("Hế lô");
        this.setSize(400, 300);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }
}
Editor is loading...