UpdateTable

UpdateTable
 avatar
user_8008155
java
2 years ago
3.3 kB
5
Indexable
Never
import javax.swing.*;
import java.awt.*;

public class EditTable extends JDialog {
    private String ID, NAME, NOTE;
    private JTable jTable;
    private JButton save;
    private JTextField textName, textId;
    private  JTextArea textNote;
    
    public String getID() {
        return ID;
    }

    public String getNAME() {
        return NAME;
    }

    public String getNOTE() {
        return NOTE;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public void setNAME(String NAME) {
        this.NAME = NAME;
    }

    public void setNOTE(String NOTE) {
        this.NOTE = NOTE;
    }

    public EditTable(){
        addControl();
        addEvent();
    }

    public void addControl(){
        Container con = getContentPane();

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

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

        JPanel jpID = new JPanel();
        JLabel jlID = new JLabel("ID",SwingConstants.RIGHT);
        textId = new JTextField(20);
        jpID.add(jlID);
        jpID.add(textId);

        JPanel jpName = new JPanel();
        JLabel jlName = new JLabel("Name", SwingConstants.RIGHT);
        textName = new JTextField(20);
        jpName.add(jlName);
        jpName.add(textName);

        JPanel jpNote = new JPanel();
        JLabel jlNote = new JLabel("Note",SwingConstants.RIGHT);
        textNote = new JTextArea(5,20);
        jpNote.add(jlNote);
        jpNote.add(textNote);

        jpText.add(jpID);
        jpText.add(jpName);
        jpText.add(jpNote);

        jlID.setPreferredSize(jlName.getPreferredSize());
        jlNote.setPreferredSize(jlName.getPreferredSize());

        Jpa.add(jpText);

        JPanel JpButton = new JPanel();
        save = new JButton("Save");
        JpButton.add(save);
        Jpa.add(JpButton);

        textId.setText(ID);
        textName.setText(NAME);;
        textNote.setText(NOTE);

        con.add(Jpa);
    }

    private void addEvent(){
        save.addActionListener(e -> {
            String ID = textId.getText();
            String NAME = textName.getText();
            String NOTE = textNote.getText();

            if ((ID.trim().length() == 0)&& (NAME.trim().length() == 0)){
                JOptionPane.showMessageDialog(null, "Opp, please check ID And Name.",
                        "Inane error", JOptionPane.ERROR_MESSAGE);
                return;
            }else if ((ID.trim().length() == 0) ) {//trim khoảng cách 2 đầu
                JOptionPane.showMessageDialog(null, "Opp, please check ID.",
                        "Inane error", JOptionPane.ERROR_MESSAGE);
                return;
            } else if (NAME.trim().length() == 0) {
                JOptionPane.showMessageDialog(null, "Opp, please check Name.");
                return;
            }



            JOptionPane.showMessageDialog(this, "Success");
            this.dispose();
        });
    }

    public void Show(){
        this.setTitle("Table");
        this.setSize(300,300);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }
}