-1

My problem is that I want the table to be updated instantly as soon as I press the "MusteriEkle" button while transferring the data I received from the "MusteriOlusturmaEkranın" to the table, but when I press it, the previous data in "Musteri.txt" is added to the table, that is, it is repeated, but when we click the "GeriDon" button and come to the "AnaEkran", and again when we click on the "MusteriOlusturmaEkranın" It shows up correctly when we open " How can I fix this?

package GUI;

import Action.MusteriEkraniAction;
import Controller.MusteriController;
import DAO.MusteriDao;
import Entity.Musteri;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class MusteriOlusturmaEkrani extends JFrame {
    private JTable MusteriTablo;
    private JLabel AdSoyad;
    private JLabel TelefoNo;
    private JLabel Email;
    private JLabel SevkAdres;
    private JTextField AdSoyadF;
    private JTextField EmailF;
    private JTextField TelefoNoF;
    private JTextField SevkAdresF;
    private JScrollPane tabloPane;
    private JButton GeriDon;
    private JButton MusteriEkle;
    private DefaultTableModel Musterimodel;
    public Object[] musteriVeri;
    public MusteriController musteriController;

    public MusteriOlusturmaEkrani() throws IOException {
        TabloOlustur();
        Olustur();
    }

    private void TabloOlustur() {
        MusteriDao musteridao = new MusteriDao();
        musteriController = new MusteriController();
        Musterimodel = new DefaultTableModel();
        Object[] müsteriObje = new Object[4];
        müsteriObje[0] = "Ad Soyad";
        müsteriObje[1] = "Tel No";
        müsteriObje[2] = "E-mail";
        müsteriObje[3] = "Adres";
        Musterimodel.setColumnIdentifiers(müsteriObje);
        musteriVeri = new Object[4];
        List<Musteri> musteriler = musteriController.ListeyiAl();
        for (int i = 0; i < musteriler.size(); i++) {
            musteriVeri[0] = musteriler.get(i).getAdSoyad();
            musteriVeri[1] = musteriler.get(i).getEmail();
            musteriVeri[2] = musteriler.get(i).gettelefoNo();
            musteriVeri[3] = musteriler.get(i).getSevkAdresi();
            Musterimodel.addRow(musteriVeri);
        }
        PanelEkle();
    }

    public void TabloGuncelle() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Musterimodel.setRowCount(0); // Tablodaki tüm satırları sil
                List<Musteri> musteriler = musteriController.ListeyiAl();
                for (int i = 0; i < musteriler.size(); i++) {
                    Object[] musteriVeri = new Object[4]; // Yeni dizi oluştur
                    musteriVeri[0] = musteriler.get(i).getAdSoyad();
                    musteriVeri[1] = musteriler.get(i).getEmail();
                    musteriVeri[2] = musteriler.get(i).gettelefoNo();
                    musteriVeri[3] = musteriler.get(i).getSevkAdresi();
                    Musterimodel.addRow(musteriVeri);
                }
            }
        });
        thread.start();
    }

    private void Olustur() {
        add(PanelEkle());
        setTitle("Müşteri Oluşturma");
        setLocationRelativeTo(null);
        setBounds(300, 75, 1000, 650);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setResizable(false);
    }

    private JPanel PanelEkle() {
        JPanel MüsteriPanel = new JPanel();
        MüsteriPanel.setBackground(new Color(255, 240, 255));
        MüsteriPanel.setLayout(null);
        MüsteriPanel.add(getTabloPane());
        MüsteriPanel.add(getAdSoyad());
        MüsteriPanel.add(getAdSoyadF());
        MüsteriPanel.add(getTelefoNo());
        MüsteriPanel.add(getTelefoNoF());
        MüsteriPanel.add(getEmail());
        MüsteriPanel.add(getEmailF());
        MüsteriPanel.add(getGeriDon());
        MüsteriPanel.add(getSevkAdres());
        MüsteriPanel.add(getSevkAdresF());
        MüsteriPanel.add(getMusteriEkle());
        return MüsteriPanel;
    }

    public JTable getMusteriTablo() {
        if (MusteriTablo == null) {
            MusteriTablo = new JTable();
            MusteriTablo.setModel(Musterimodel);
        }
        return MusteriTablo;
    }

    public void setMusteriTablo(JTable MusteriTablo) {
        this.MusteriTablo = MusteriTablo;
    }

    public JLabel getAdSoyad() {
        if (AdSoyad == null) {
            AdSoyad = new JLabel("Ad Soyad :");
            AdSoyad.setBounds(50, 100, 200, 50);
            AdSoyad.setFont(new Font("Times New Roman", Font.PLAIN, 20));
        }
        return AdSoyad;
    }

    public void setAdSoyad(JLabel AdSoyad) {
        this.AdSoyad = AdSoyad;
    }

    public JLabel getTelefoNo() {
        if (TelefoNo == null) {
            TelefoNo = new JLabel("Telefon :");
            TelefoNo.setBounds(50, 260, 200, 50);
            TelefoNo.setFont(new Font("Times New Roman", Font.PLAIN, 20));
        }
        return TelefoNo;
    }

    public void setTelefoNo(JLabel TelefoNo) {
        this.TelefoNo = TelefoNo;
    }

    public JLabel getEmail() {
        if (Email == null) {
            Email = new JLabel("E-mail :");
            Email.setBounds(50, 180, 200, 50);
            Email.setFont(new Font("Times New Roman", Font.PLAIN, 20));
        }
        return Email;
    }

    public void setEmail(JLabel Email) {
        this.Email = Email;
    }

    public JTextField getAdSoyadF() {
        if (AdSoyadF == null) {
            AdSoyadF = new JTextField();
            AdSoyadF.setBounds(50, 150, 200, 30);
        }
        return AdSoyadF;
    }

    public void setAdSoyadF(JTextField AdSoyadF) {
        this.AdSoyadF = AdSoyadF;
    }

    public JTextField getEmailF() {
        if (EmailF == null) {
            EmailF = new JTextField();
            EmailF.setBounds(50, 310, 200, 30);
        }
        return EmailF;
    }

    public void setEmailF(JTextField EmailF) {
        this.EmailF = EmailF;
    }

    public JTextField getTelefoNoF() {
        if (TelefoNoF == null) {
            TelefoNoF = new JTextField();
            TelefoNoF.setBounds(50, 230, 200, 30);
        }
        return TelefoNoF;
    }

    public void setTelefoNoF(JTextField TelefoNoF) {
        this.TelefoNoF = TelefoNoF;
    }

    public JScrollPane getTabloPane() {
        if (tabloPane == null) {
            tabloPane = new JScrollPane();
            tabloPane.setViewportView(getMusteriTablo());
            tabloPane.setBounds(450, 50, 500, 450);
        }
        return tabloPane;
    }

    public void setTabloPane(JScrollPane tabloPane) {
        this.tabloPane = tabloPane;
    }

    public JButton getGeriDon() {
        if (GeriDon == null) {
            GeriDon = new JButton("Geri dön");
            GeriDon.setBounds(850, 550, 100, 30);
            GeriDon.setBackground(new Color(186, 153, 187));
            GeriDon.addActionListener(new MusteriEkraniAction(this));
        }
        return GeriDon;
    }

    public void setGeriDon(JButton GeriDon) {
        this.GeriDon = GeriDon;
    }

    public JLabel getSevkAdres() {
        if (SevkAdres == null) {
            SevkAdres = new JLabel("Sevk Adresi :");
            SevkAdres.setBounds(50, 360, 100, 30);
            SevkAdres.setFont(new Font("Times New Roman", Font.PLAIN, 20));
        }
        return SevkAdres;
    }

    public void setSevkAdres(JLabel SevkAdres) {
        this.SevkAdres = SevkAdres;
    }

    public JTextField getSevkAdresF() {
        if (SevkAdresF == null) {
            SevkAdresF = new JTextField();
            SevkAdresF.setBounds(50, 410, 200, 30);
        }
        return SevkAdresF;
    }

    public void setSevkAdresF(JTextField SevkAdresF) {
        this.SevkAdresF = SevkAdresF;
    }

    public JButton getMusteriEkle() {
        if (MusteriEkle == null) {
            MusteriEkle = new JButton("Müşteri Oluştur");
            MusteriEkle.setBounds(50, 500, 200, 30);
            MusteriEkle.setBackground(new Color(143, 194, 197));
            MusteriEkle.addActionListener(new MusteriEkraniAction(this));
        }
        return MusteriEkle;
    }

    public void setMusteriEkle(JButton MusteriEkle) {
        this.MusteriEkle = MusteriEkle;
    }
}

"Musterimodel.setRowCount(0);" in "TabloGüncelle" I tried adding the code it was repeating twice before this code it was repeating once after adding this code

Abra
  • 19,142
  • 7
  • 29
  • 41
  • Swing is not thread safe - you should be using a SwingWorker – MadProgrammer Apr 28 '23 at 08:57
  • i used it to prevent the app from freezing – Orhancan Yıldırım Apr 28 '23 at 09:06
  • 1
    And, as I stated, Swing is NOT thread safe - you should be using a `SwingWorker` which will allow you to execute a long running or blocking operation in the background, but provides you with the functionality to post updates back the Event Dispatching Thread – MadProgrammer Apr 28 '23 at 09:14
  • aside: stick to java naming conventions, please - without it's a terrible experience to read .. – kleopatra Apr 28 '23 at 10:34
  • 1
    another must-not (apart from must-not access components nor any of its properties off the EDT) is a null layout manager (along with hard-coding locations/sizes of components), instead choose a manager that suits your needs, the swing tag wiki has references for tutorials to learn how to use them. – kleopatra Apr 28 '23 at 10:39

1 Answers1

-1

Swing is not thread safe, this means that you should not be updating the UI, or any state the UI relies on, from outside the context of the Event Dispatching Thread.

See Concurrency in Swing for more details.

In this case, I would suggest using a SwingWorker, see Worker Threads and SwingWorker for more details.

I modified your code to generate some random data and your MusteriEkle button code to make use of a SwingWorker which will update the UI with new rows of data, after a small, deliberate, delay, which is used for effect.

protected List<Musteri> randomData() {
    List<Musteri> data = new ArrayList<>(256);
    for (int index = 0; index < 100 + (int) (Math.random() * 1000); index++) {
        data.add(new Musteri(index));
    }
    return data;
}

public JButton getMusteriEkle() {
    if (musteriEkle == null) {
        musteriEkle = new JButton("Müşteri Oluştur");
        musteriEkle.setBounds(50, 500, 200, 30);
        musteriEkle.setBackground(new Color(143, 194, 197));
        musteriEkle.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                musteriEkle.setEnabled(false);
                musteriModel.setRowCount(0); // Tablodaki tüm satırları sil
                SwingWorker<Void, Object[]> worker = new SwingWorker<>() {
                    @Override
                    protected Void doInBackground() throws Exception {
                        Thread.sleep(50);
                        List<Musteri> musteriler = randomData();
                        for (int i = 0; i < musteriler.size(); i++) {
                            Object[] musteriVeri = new Object[4]; // Yeni dizi oluştur
                            musteriVeri[0] = musteriler.get(i).getAdSoyad();
                            musteriVeri[1] = musteriler.get(i).getEmail();
                            musteriVeri[2] = musteriler.get(i).gettelefoNo();
                            musteriVeri[3] = musteriler.get(i).getSevkAdresi();
                            publish(musteriVeri);
                            Thread.sleep(50);
                        }
                        return null;
                    }

                    @Override
                    protected void process(List<Object[]> chunks) {
                        for (Object[] row : chunks) {
                            musteriModel.addRow(row);
                        }
                    }

                    @Override
                    protected void done() {
                        musteriEkle.setEnabled(true);
                    }
                };
                worker.execute();
            }
        });
        //musteriEkle.addActionListener(new MusteriEkraniAction(this));

    }
    return musteriEkle;
}

You might want to take some time and learn the Code Conventions for the Java Programming Language, it will make it easier for other people to read your code.

I would also recommend taking the time to learn, understand and use the layout management system available in Swing ... as you layout kind of broke on my system while I was trying to test your code.

but when I press it, the previous data in "Musteri.txt" is added to the table, that is, it is repeated

I was unable to reproduce your issue as you supplied a incomplete and uncompilable code example, but I would start my investigation in MusteriController and the ListeyiAl method.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    arrrgghhh .. you know better than to replicate such terrible naming violations ;) – kleopatra Apr 28 '23 at 10:36
  • @kleopatra Copy'n'Paste - does it help to "answer" the question? I'm kind of over trying to "fix every little" thing - I've highlight the issues and moved on :P – MadProgrammer Apr 28 '23 at 13:21