更新jtable live时出现问题

jtw3ybtb  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(343)

我对编码完全是个初学者,这是我学习的第一年。我们实际上在学习java,我有一个问题找不到。我在stackoverflow上找到了一些东西,但都没用,因为我想我不知道自己在做什么。
一切正常,插入、删除和全选都显示jtable。我不明白的是,当我插入时,表会被更新,但是当我删除时,没有任何东西会被实时更新。当我关上再打开的时候它就工作了。

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;
    }
}
58wvjzkj

58wvjzkj1#

我认为问题出在delete methode中的contactcontroller类中,您是从数据库中删除的,而不是从annuaire列表中删除的:(这就是为什么在重新打开它时,它会工作的原因)
因此,请更改删除方法:

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

这将使它工作

相关问题