简体   繁体   English

Java Swing - 实时更新 Jtable 时出现问题

[英]Java Swing - Problem updating Jtable live

I'm a total beginner in coding, and it's my first year studying.我是编码的完全初学者,这是我学习的第一年。 We are actually studying Java, and I have an issue I can't find.我们实际上正在学习 Java,但我有一个我找不到的问题。 Tried several stuff I found on stackoverflow, but nothing worked, because I think I don't know what I'm doing.尝试了我在 stackoverflow 上找到的一些东西,但没有任何效果,因为我想我不知道自己在做什么。

Every thing work, the insert, delete, and select all to show the Jtable.一切正常,插入、删除和全选以显示 Jtable。 And the thing that I don't understand, is when I do an insert, the Table gets Updated, but when I delete, nothing gets updated live.我不明白的是,当我进行插入时,表会更新,但是当我删除时,没有任何内容实时更新。 It works when I close and Reopen .当我关闭并重新打开时它起作用。


import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EcranContacts extends JFrame implements ActionListener {

    private JPanel contentPane;
    private JLabel lblNewLabel;
    private JComboBox comboBox;
    private JLabel lblNewLabel_1;
    private JTextField textField;
    private JTextField textField_1;
    private JLabel lblNewLabel_1_1;
    private JTextField textField_2;
    private JLabel lblNewLabel_1_2;
    private JTextField textField_3;
    private JLabel lblNewLabel_1_3;
    private JTable table;
    private JButton btnNewButton;
    private JButton btnNewButton1;
    private JButton btnDelete;
    private JPanel panelAnnuaire;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    EcranContacts frame = new EcranContacts();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public EcranContacts() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 501);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        contentPane.add(getLblNewLabel());
        contentPane.add(getComboBox());
        contentPane.add(getLblNewLabel_1());
        contentPane.add(getTextField());
        contentPane.add(getTextField_1());
        contentPane.add(getLblNewLabel_1_1());
        contentPane.add(getTextField_2());
        contentPane.add(getLblNewLabel_1_2());
        contentPane.add(getTextField_3());
        contentPane.add(getLblNewLabel_1_3());
        //  contentPane.add(getTable());
        contentPane.add(getBtnNewButton());
        contentPane.add(getBtnNewButton1());
        contentPane.add(getBtnDelete());
    }

    private JLabel getLblNewLabel() {
        if (lblNewLabel == null) {
            lblNewLabel = new JLabel("Choisissez une option :");
            lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
            lblNewLabel.setBounds(45, 49, 192, 19);
        }
        return lblNewLabel;
    }

    private JComboBox getComboBox() {
        String[] options = {"Ajouter un contact", "Supprimer un contact"};

        if (comboBox == null) {
            comboBox = new JComboBox(options);

            comboBox.setFont(new Font("Tahoma", Font.PLAIN, 14));


            comboBox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JComboBox cb = (JComboBox) e.getSource();
                    String optionChoice = (String) cb.getSelectedItem();
                    if (optionChoice == "Supprimer un contact") {
                        getBtnNewButton().setVisible(false);
                        getLblNewLabel_1().setVisible(false);
                        getTextField().setVisible(false);
                        getLblNewLabel_1_1().setVisible(false);
                        getTextField_1().setVisible(false);
                        getLblNewLabel_1_3().setVisible(false);
                        getTextField_3().setVisible(false);
                        getLblNewLabel_1_2().setBounds(140, 98, 48, 14);
                        getTextField_2().setBounds(186, 96, 166, 20);
                        getBtnNewButton().setVisible(true);
                        getBtnDelete().setVisible(true);
                        getBtnNewButton1().setVisible(false);


                    }
                    if (optionChoice == "Ajouter un contact") {
                        getBtnNewButton().setVisible(false);
                        getLblNewLabel_1().setVisible(true);
                        getTextField().setVisible(true);
                        getLblNewLabel_1_1().setVisible(true);
                        getTextField_1().setVisible(true);
                        getLblNewLabel_1_3().setVisible(true);
                        getTextField_3().setVisible(true);
                        getLblNewLabel_1_2().setVisible(true);
                        getTextField_2().setVisible(true);
                        getLblNewLabel_1_2().setBounds(140, 156, 48, 14);
                        getTextField_2().setBounds(186, 154, 166, 20);
                        getBtnNewButton1().setVisible(true);
                        getBtnNewButton().setVisible(true);
                        getBtnDelete().setVisible(false);

                    }
                }
            });

            comboBox.setBounds(198, 48, 216, 22);
        }
        return comboBox;
    }

    private JLabel getLblNewLabel_1() {
        if (lblNewLabel_1 == null) {
            lblNewLabel_1 = new JLabel("Nom :");
            lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
            lblNewLabel_1.setBounds(140, 98, 48, 14);
        }
        return lblNewLabel_1;
    }

    private JTextField getTextField() {
        if (textField == null) {
            textField = new JTextField();
            textField.setBounds(186, 96, 166, 20);
            textField.setColumns(10);
        }
        return textField;
    }

    private JTextField getTextField_1() {
        if (textField_1 == null) {
            textField_1 = new JTextField();
            textField_1.setColumns(10);
            textField_1.setBounds(186, 125, 166, 20);
        }
        return textField_1;
    }

    private JLabel getLblNewLabel_1_1() {
        if (lblNewLabel_1_1 == null) {
            lblNewLabel_1_1 = new JLabel("Prénom :");
            lblNewLabel_1_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
            lblNewLabel_1_1.setBounds(124, 127, 71, 14);
        }
        return lblNewLabel_1_1;
    }

    private JTextField getTextField_2() {
        if (textField_2 == null) {
            textField_2 = new JTextField();
            textField_2.setColumns(10);
            textField_2.setBounds(186, 154, 166, 20);
        }
        return textField_2;
    }

    private JLabel getLblNewLabel_1_2() {
        if (lblNewLabel_1_2 == null) {
            lblNewLabel_1_2 = new JLabel("Email :");
            lblNewLabel_1_2.setFont(new Font("Tahoma", Font.PLAIN, 13));
            lblNewLabel_1_2.setBounds(140, 156, 48, 14);
        }
        return lblNewLabel_1_2;
    }

    private JTextField getTextField_3() {
        if (textField_3 == null) {
            textField_3 = new JTextField();
            textField_3.setColumns(10);
            textField_3.setBounds(186, 183, 166, 20);
        }
        return textField_3;
    }

    private JLabel getLblNewLabel_1_3() {
        if (lblNewLabel_1_3 == null) {
            lblNewLabel_1_3 = new JLabel("Mot de passe :");
            lblNewLabel_1_3.setFont(new Font("Tahoma", Font.PLAIN, 13));
            lblNewLabel_1_3.setBounds(96, 185, 92, 14);
        }
        return lblNewLabel_1_3;
    }


    private JButton getBtnNewButton() {
        if (btnNewButton == null) {
            btnNewButton = new JButton("Voir Annuaire");
            btnNewButton.setBounds(80, 233, 120, 23);
            setFont(new Font("Tahoma", Font.PLAIN, 13));

            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    EcranAnnuaire en = new EcranAnnuaire();
                    en.setVisible(true);

                }
            });
        }
        return btnNewButton;
    }

    public JButton getBtnNewButton1() {
        if (btnNewButton1 == null) {
            btnNewButton1 = new JButton("Valider");
            btnNewButton1.setBounds(250, 233, 120, 23);
            setFont(new Font("Tahoma", Font.PLAIN, 13));

            btnNewButton1.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    Contact contact = new Contact();
                    contact.setName(getTextField().getText());
                    contact.setSurname(getTextField_1().getText());
                    contact.setEmail(getTextField_2().getText());
                    contact.setPassword(getTextField_3().getText());
                    ContactController.get().save(contact);

                    JOptionPane.showMessageDialog(null, "Ajout du contact réussie");

                }
            });
        }
        return btnNewButton1;
    }

    public JButton getBtnDelete() {
        if (btnDelete == null) {
            btnDelete = new JButton("Supprimer");
            btnDelete.setBounds(250, 233, 120, 23);
            setFont(new Font("Tahoma", Font.PLAIN, 13));

            btnDelete.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
//
                    ContactController.get().delete(getTextField_2().getText());
                    JOptionPane.showMessageDialog(null, "Suppression du contact réussie");

                    //ContactController.getCatalogue();
//                    DefaultTableModel model = (DefaultTableModel) table.getModel();
//                    model.fireTableDataChanged();

                }
            });
        }
        return btnDelete;
    }

}

import java.util.List;

public class ContactController {

    private static List<Contact> annuaire;
    private EcranContacts ecranContacts;
    private ContactManager cm;
   // private List<Contact> annuaire;
    private static ContactController instance;
    private int indexCatalogue;

    public static synchronized ContactController get() {
        if(instance == null) {
            instance = new ContactController();
        }
        return instance;
    }


    public ContactController() {
            cm = new ContactManager();
            //J'initialise le catalogue en mémoire
            annuaire = cm.ListContacts();
    }

    public static List<Contact> getCatalogue() {
        return annuaire;
    }

    public void save(Contact contact) {

        cm.insertContact(contact);
        annuaire.add(contact);
    }

    public void delete(String email) {

        cm.delete(email);
    }

}



import javax.swing.table.AbstractTableModel;
import java.util.List;

public class TableCatalogueModel extends AbstractTableModel {

    private List<Contact> annuaire;
    private String[] columnNames = {"Nom", "Prénom", "Email", "Mot de passe"};

    public TableCatalogueModel(List<Contact> annuaire) {
        this.annuaire = annuaire;
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    public Class<?> getColumnClass(int columnIndex) {
        return getValueAt(0, columnIndex).getClass();
    }

    public String getColumnName(int column) {
        return columnNames[column];
    }


    @Override
    public int getRowCount() {
        return annuaire.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Object val = null;

        switch (columnIndex) {
            case 0: val = annuaire.get(rowIndex).getName(); break;
            case 1: val = annuaire.get(rowIndex).getSurname(); break;
            case 2: val = annuaire.get(rowIndex).getEmail(); break;
            case 3: val = annuaire.get(rowIndex).getPassword(); break;
        }
        return val;
    }
}


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

public class TableCatalogue extends JTable {

    private List<Contact> annuaire;

    public TableCatalogue(List<Contact> annuaire) {

        super(new TableCatalogueModel(annuaire));
        this.getColumnModel().getColumn(0).setPreferredWidth(50);
        this.getColumnModel().getColumn(1).setPreferredWidth(100);
        this.getColumnModel().getColumn(2).setPreferredWidth(100);
        this.getColumnModel().getColumn(3).setPreferredWidth(200);


        setPreferredScrollableViewportSize(new Dimension(500,70));
        setFillsViewportHeight(true);

        this.setRowHeight(30);

    }


}


import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ContactManager {

    private static InterfaceDao daoContact;

    private Contactdao contactDao = new Contactdao();
    public static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);


    public ContactManager() {
        daoContact = DAOFactory.getContactDao();
    }


    /**
     * Insert Contact (USES ContactDao)
     *
     * @param contact
     */
    public void insertContact(Contact contact) {
        contactDao.insertContact(contact);
    }

    /**
     * Delete contact by Email (USES ContactDao)
     *
     * @param email
     */
    public void delete(String email) {
        contactDao.delete(email);
    }

    /**
     * List of all the contacts - select all (USES ContactDao)
     *
     * @return
     */
    public List<Contact> ListContacts() {
        return contactDao.ListContacts();
    }

    public static boolean validateEmail(String email) {
        Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(email);
        return matcher.find();
    }

    public static boolean validateName(String name) {
        boolean validName = true;
        if (name == "") {
            validName = false;
        }
        return validName;
    }
}

i think the problem is in your ContactController class in delete methode you delete from the database and not from annuaire list : (that's why this work when you reopen it)我认为问题出在您的ContactController类中的删除方法中,您从数据库中删除,而不是从annuaire列表中删除:(这就是为什么当您重新打开它时这项工作)
so change your delete methode :所以改变你的删除方法:

public void delete(String email) {
   contactDao.delete(email);
   annuairel.removeIf((value) -> {
        return value.email == email;
   });
}

and this will make it works这将使它起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM