You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@netbeans.apache.org by Zulfi Khan <zu...@yahoo.com.INVALID> on 2022/01/17 23:58:13 UTC

Why model.clear not working?

Hi,I am doing model .clear but still, listbox holds duplicate entries. I have problem in the function: 

 private void btnDispSavCurrActionPerformed(java.awt.event.ActionEvent evt)

Following is my JFrame derived class, Somebody please guide me.
Zulfi.



package com.mycompany.inher2rbuttarrlist2;

import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;

/**
 *
 * @author zulfi
 */
public class RButtArrListJFrame extends javax.swing.JFrame {
    ArrayList<Account> al_allAcc = new ArrayList<>();
    //ArrayList<String> al_sav = new ArrayList<String>();
    DefaultListModel <String> model = new DefaultListModel<String>();
    Account acc1 = new Account(200.0, 100, "SSUET1", 's');
    Account acc2 = new Account(300.0, 101, "SSUET2", 's');
    Account acc3 = new Account(400.0, 102, "SSUET3", 's');
    Account acc4 = new Account(500.0, 103, "SSUET4", 'c');
    Account acc5 = new Account(600.0, 104, "SSUET5", 'c'); 
    ButtonGroup group = new ButtonGroup();
    /**
     * Creates new form RButtArrListJFrame
     */
    public RButtArrListJFrame() {
        initComponents();
        group.add(currAccRadBtn);
        group.add(savAccRadBtn);
    }
    
    void storeAllAccountInfoinArrayList(){
        //https://www.delftstack.com/howto/java/adding-objects-to-arraylist/
        al_allAcc.add(acc1);
        al_allAcc.add(acc2);
        al_allAcc.add(acc3);
        al_allAcc.add(acc4);
        al_allAcc.add(acc5);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        btnDispSavCurr = new javax.swing.JButton();
        btnDispAll = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList<>();
        savAccRadBtn = new javax.swing.JRadioButton();
        currAccRadBtn = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnDispSavCurr.setText("Display Info Saving or Current");
        btnDispSavCurr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDispSavCurrActionPerformed(evt);
            }
        });

        btnDispAll.setText("Display All Accounts");
        btnDispAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDispAllActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jList1);

        savAccRadBtn.setText("Saving Account");

        currAccRadBtn.setText("Current Account");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(btnDispSavCurr)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 143, Short.MAX_VALUE)
                        .addComponent(btnDispAll, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(savAccRadBtn)
                            .addComponent(currAccRadBtn))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(49, 49, 49))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDispSavCurr)
                    .addComponent(btnDispAll))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(savAccRadBtn)
                        .addGap(51, 51, 51)
                        .addComponent(currAccRadBtn)))
                .addContainerGap(210, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void btnDispAllActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        model.clear();
        model.addElement(" " + acc1.getBalance() + " " + acc1.getNumber() + acc1.name+ acc1.accType);
        model.addElement(" " + acc2.getBalance() + " " + acc2.getNumber() + acc2.name+ acc2.accType);
        model.addElement(" " + acc3.getBalance() + " " + acc3.getNumber() + acc3.name+ acc3.accType);
        model.addElement(" " + acc4.getBalance() + " " + acc4.getNumber() + acc4.name+ acc4.accType);
        model.addElement(" " + acc5.getBalance() + " " + acc5.getNumber() + acc5.name+ acc5.accType);
        jList1.setModel(model);
    }                                          

    private void btnDispSavCurrActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        storeAllAccountInfoinArrayList();
        boolean iscurrAccRBSelected = currAccRadBtn.isSelected();
        boolean issavAccRBSelected = savAccRadBtn.isSelected();
        model.clear();
        if(iscurrAccRBSelected){//Why in case of current Acc it is storing acc4 and acc5 twice?
            for (Account acc: al_allAcc) {
               //System.out.println("AccountTitle: "+acc.name+", balance "+acc.getBalance()+ "and mnumber is "+acc.getNumber());
               if (acc.accType == 'c' )
                  model.addElement(" " + acc.getBalance() + " " + acc.getNumber() + acc.name+ acc.accType);  
                  jList1.setModel(model);
            }
            //code it
            //ask why we creat rbgroup if we are not using the grp var
            
        }
        else if(issavAccRBSelected){
            for (Account acc: al_allAcc) {
               //System.out.println("AccountTitle: "+acc.name+", balance "+acc.getBalance()+ "and mnumber is "+acc.getNumber());
               if (acc.accType == 's' )
                  model.addElement(" " + acc.getBalance() + " " + acc.getNumber() + acc.name+ acc.accType);    
                  jList1.setModel(model);
            }
        }
    }                                              

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RButtArrListJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RButtArrListJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RButtArrListJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RButtArrListJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RButtArrListJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnDispAll;
    private javax.swing.JButton btnDispSavCurr;
    private javax.swing.JRadioButton currAccRadBtn;
    private javax.swing.JList<String> jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton savAccRadBtn;
    // End of variables declaration                   
}



Re: Why model.clear not working?

Posted by "Brad K." <ze...@gmail.com>.
Not sure if this question is appropriate for this mailing list, but...

At the beginning of btnDispSavCurrActionPerformed, you call 
"storeAllAccountInfoinArrayList" ... this happens EVERY TIME you click 
this button. So the underlying list grows every time you click on the 
button.

Since this appears to be an initializer, its probably better
to put this someplace where it only executes once.

BK

On 1/17/2022 5:58 PM, Zulfi Khan wrote:
> Subject:
> Why model.clear not working?
> From:
> Zulfi Khan <zu...@yahoo.com.INVALID>
> Date:
> 1/17/2022, 5:58 PM
> 
> To:
> NetBeans Mailing List <us...@netbeans.apache.org>
> 
> Delivered-To:
> zelmak@gmail.com
> Received:
> by 2002:a05:7000:7948:0:0:0:0 with SMTP id r8csp3039488maz; Mon, 17 Jan 
> 2022 15:59:30 -0800 (PST)
> X-Google-Smtp-Source:
> ABdhPJz9cl6hpTPLbGlBENtrVobyOwc7tZa7eJoKzBGOgQhCujaPaictLNtsRLdZo1306Hcx0Zge
> X-Received:
> by 2002:a05:620a:2942:: with SMTP id n2mr16340301qkp.340.1642463970609; 
> Mon, 17 Jan 2022 15:59:30 -0800 (PST)
> ARC-Seal:
> i=1; a=rsa-sha256; t=1642463970; cv=none; d=google.com; s=arc-20160816; 
> b=WWGRRWAPLjRbIa+rwCN4uqDuVlKjpOSdUcF0k2LaBYF/MQFdv0t9GrqyWL2vD+Qys9 
> gKAxn8LwY3TeaH0NwHXQhqy5Clm87UVAYNtEdtPG4F5WRDbQ0kHki9J+RRRz0JHwTVFH 
> eJIOslrZUGYFsQ0uKXLaFSUPYA5fo1y6ob4DyMDxsMLxcaK71OdM0hoonSpqRmLZUuPW 
> /PasFgMNMVxuv+9wAqIYSAH1P1FEiYZMtlLnoKPrThI/C5PxcFkg+m5hvmgRe3G6WbPE 
> FO08dBfxBhEX66VVWyy7ipEs7o/icaSFSrUnrWxL5bapL1rJU+e2WZ0i5D2b7mzZLSzp dRGQ==
> ARC-Message-Signature:
> i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; 
> h=references:mime-version:subject:message-id:to:from:date 
> :delivered-to:list-id:list-post:list-unsubscribe:list-help 
> :precedence:mailing-list; 
> bh=rYKmbLQK3wkpZ/mE6HfBx1qpSGjS8F1+/TYhJp38t94=; 
> b=WFz23UEmtb4c1y9Y/OcLOyL5UayzbNHHWa1Ok7UYL8rnum91XbhgM9e5FVlO3dO0m+ 
> vEXqT9kCSCC+UGjsfWNZ6c1+siNAkB9pHPPPPqRAevgpsedB1Q+aJHJ/zcZ3UzXxlvH8 
> MZkNWaFzt3sCMHnyg/8veEtkJRUNpU7eEWFAk82s3YvRSTpCjsUm5prGGz73EQnw7tR3 
> tLIfX0Q77surcp20mpAQZboZBy270TRbgsa9RYjOJkCmebgWhSkRGXEFBrG2lKC1NsWv 
> 7C01e5jsJgTi/MeDzWP1Lbw/MvG632dZtE8MMUlt16TSWH/fKE/1YBUOvSF6s4dKX8xX 7cPg==
> ARC-Authentication-Results:
> i=1; mx.google.com; spf=pass (google.com: domain of 
> users-return-8464-zelmak=gmail.com@netbeans.apache.org designates 
> 3.227.148.255 as permitted sender) 
> smtp.mailfrom="users-return-8464-zelmak=gmail.com@netbeans.apache.org"
> Return-Path:
> <us...@netbeans.apache.org>
> Received:
> from mxout1-ec2-va.apache.org (mxout1-ec2-va.apache.org. 
> [3.227.148.255]) by mx.google.com with ESMTPS id 
> c9si4700991qtd.56.2022.01.17.15.59.30 for <ze...@gmail.com> 
> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jan 
> 2022 15:59:30 -0800 (PST)
> Received-SPF:
> pass (google.com: domain of 
> users-return-8464-zelmak=gmail.com@netbeans.apache.org designates 
> 3.227.148.255 as permitted sender) client-ip=3.227.148.255;
> Authentication-Results:
> mx.google.com; spf=pass (google.com: domain of 
> users-return-8464-zelmak=gmail.com@netbeans.apache.org designates 
> 3.227.148.255 as permitted sender) 
> smtp.mailfrom="users-return-8464-zelmak=gmail.com@netbeans.apache.org"
> Received:
> from mail.apache.org (mailroute1-lw-us.apache.org [207.244.88.153]) by 
> mxout1-ec2-va.apache.org (ASF Mail Server at mxout1-ec2-va.apache.org) 
> with SMTP id 959B8404F3 for <ze...@gmail.com>; Mon, 17 Jan 2022 
> 23:59:27 +0000 (UTC)
> Received:
> (qmail 66331 invoked by uid 500); 17 Jan 2022 23:59:26 -0000
> Mailing-List:
> contact users-help@netbeans.apache.org; run by ezmlm
> Precedence:
> bulk
> List-Help:
> <ma...@netbeans.apache.org>
> List-Unsubscribe:
> <ma...@netbeans.apache.org>
> List-Post:
> <ma...@netbeans.apache.org>
> List-Id:
> <users.netbeans.apache.org>
> Delivered-To:
> mailing list users@netbeans.apache.org
> Received:
> (qmail 66321 invoked by uid 99); 17 Jan 2022 23:59:26 -0000
> Received:
> from spamproc1-he-fi.apache.org (HELO spamproc1-he-fi.apache.org) 
> (95.217.134.168) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 17 Jan 
> 2022 23:59:26 +0000
> Received:
> from localhost (localhost [127.0.0.1]) by spamproc1-he-fi.apache.org 
> (ASF Mail Server at spamproc1-he-fi.apache.org) with ESMTP id 90288C0556 
> for <us...@netbeans.apache.org>; Mon, 17 Jan 2022 23:59:25 +0000 (UTC)
> X-Virus-Scanned:
> Debian amavisd-new at spamproc1-he-fi.apache.org
> X-Spam-Flag:
> NO
> X-Spam-Score:
> 0.249
> X-Spam-Status:
> No, score=0.249 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, 
> DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, 
> FREEMAIL_ENVFROM_END_DIGIT=0.25, HTML_MESSAGE=0.2, 
> RCVD_IN_MSPIKE_H2=-0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] 
> autolearn=disabled
> Authentication-Results:
> spamproc1-he-fi.apache.org (amavisd-new); dkim=pass (2048-bit key) 
> header.d=yahoo.com
> Received:
> from mx1-he-de.apache.org ([116.203.227.195]) by localhost 
> (spamproc1-he-fi.apache.org [95.217.134.168]) (amavisd-new, port 10024) 
> with ESMTP id w5L4ppckx7oO for <us...@netbeans.apache.org>; Mon, 17 Jan 
> 2022 23:59:24 +0000 (UTC)
> Received-SPF:
> Pass (mailfrom) identity=mailfrom; client-ip=66.163.189.147; 
> helo=sonic314-21.consmr.mail.ne1.yahoo.com; 
> envelope-from=zulfi6000@yahoo.com; receiver=<UNKNOWN>
> Received:
> from sonic314-21.consmr.mail.ne1.yahoo.com 
> (sonic314-21.consmr.mail.ne1.yahoo.com [66.163.189.147]) by 
> mx1-he-de.apache.org (ASF Mail Server at mx1-he-de.apache.org) with 
> ESMTPS id E7A387EDC3 for <us...@netbeans.apache.org>; Mon, 17 Jan 2022 
> 23:59:23 +0000 (UTC)
> X-ASF-DKIM-Sig:
> v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; 
> t=1642463955; bh=rYKmbLQK3wkpZ/mE6HfBx1qpSGjS8F1+/TYhJp38t94=; 
> h=Date:From:To:Subject:References:From:Subject:Reply-To; 
> b=oC178eaHKeFT1x/9sGs4o761QYD6qlfUwYsQE/FjijAQZ8Q6SnCpXBh+MNPlR82XOTGdfptQ/bR7Ym5srxoDSs0PwX5CuvAQX2dkJrcRyAlVrXMA/2/8pltOzkLCJipelmoI+BekEfTuS3mU9UrUtR6g/asDvFhEI40O0ivYSiLvND23Vf7AIb+otM95vV3AzMWb9yY76c4v0v3XIu7wIlDWaXE6EFugDumO18lenHBWF/lZ7MDZtuWvn1d9yOsKb2620TyRwBNo0Tw6x+D4SEU96PmLZczeYJS8gQkF59s/eo1wmAUMxsVAQiE1QGfFwwMxYnncevPTY4J7jkSdew==
> X-SONIC-DKIM-SIGN:
> v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; 
> t=1642463955; bh=99D5eoRy7FbMK+Raj0KcYUCVsaPCtZsQvc9I6yO6pcr=; 
> h=X-Sonic-MF:Date:From:To:Subject:From:Subject; 
> b=Y9JNsfk33u3xFtLb/sgLtOEPt82ebM270zOfj3dYXg4kWc814esJg7dkM8iLi17jfma06PUEeIiSqLU5cTxmGn6thccqLSuVm3ygN0M8k6BsjCZk0xrmzxDiTqd1yIsqJVwS/3lI+wIGi/J6nGedvuiyQuKtrhVbrNy3skrxcyJAaBxFVwDYm1/0SJPrS7Y1fWEa8xwBsGjxzXqb51d2Tpbnv2YK0mEyEmUm/apiXEYMiShOCGzGG6bJUkRRCto3U0XCftupz/nrf1v/+cmPUf7lO3P62lC1CHz74s13hy5CAbhAjhPUkdKIohjlTRwpjZ/4aB+ayRSC/Gy9ugtsdA==
> X-YMail-OSG:
> KBr1aIMVM1mZ94OIl_yph4RbII0sFAbDzJdIVEy9mO08kQDRBAPjnRbxPw1tE8p 
> t.HYjZpUcG04zkoMobdHkvvikG3fQAn7OM8cog4iQoHrQzjynboFEDBdNgmqAa11phlxUUx0pRWr 
> B7F81E6iKmqC9.AcDJ9E9pgZxuqIoHd8P1K7H3wsd8QK54gr43rInosCDPShwaTmOw52kGIJsEOM 
> cTHUBgbnroqB4J18.JjGQTrRo.T4QaYjW8dS2a8P2840tnjzdi4eCdmmYFwhWRfscPa6Cp5MX88P 
> uTWFem_zPb7TmDC1GiDpzoQzoInBprRylTmcOaPddVZWLeaxtlIFD2wYxnFm_bg7XDLsu6h2iSrI 
> 0gbNRJXuvguSTWAmoaz.VXZESJG_K.SOsDkfzcTSpa.6IPPsY10XEEqfCTynP23ew9bE21JwAlr9 
> 3JAuQk14Kf5Ib4h6AI7d78KQHYyNU5e7pKTWzSBmk_6dkDcGYCjW5h12GS40MfGUrn4PXgQS1Unb 
> 2zjt1Sxy7sTMCVC8diKWO7Dmhknqggb1nsWeywu42_MRzy9ZYoobEyle7iZEb_p8athq649pZBMp 
> 1pwndFt04DL2zSJ_GQ_BHy9.tGhRwjd9MvzD8NAgjPACS3x9pH8kHe0rbSk1DIc_Bi6T6Zp9aNyA 
> JYbMU.s.ZfD7969CHNJx_ZEH0aegDlgkclgiZ64DSCLwJFNS8ITBr7fhu5Uh8bfuNk9uBsfBCsOl 
> oXroklE9NSajiDb.XbR5QAQHNMexDUQje4UbqiVFIQXdmuC_N2lSmcVNg3YxwcCJO8q7Yl4zFB6o 
> Xu7dIKCTMD9wHebN060I6_UFr_fXl61MhUsWvD8ify7x1_WayNbUvrt84geaOKe91wXDuU.baUCn 
> d57j_Pn4E5kOMP4GqXWjPNU1BmqOyLwmMiS6TRPXDkUk6KHF1Eq2lvaWch6dI9lOPz1H4xLXwSvl 
> cq5QEyhrF0.LBhgf8kgzjBPjh7Bju1gI6ziLp8Xl4k4B_hAvzK.pFoAnPR7P6CrIMJ1xIo35fPfn 
> TbOGhETr_ZAXI2bbZl2k.UbH7FXUES518KVK5s_63LMqoBxQbcV5ZDulQ4R7mWXNbK9w6HCmTA0P 
> IX2.HODAD0h20O4OddXz3jYeIRz3oBARqxgPG4D_pHeCA4eKcZ5wDbO6stWi33a8Sm2IlGlbGqef 
> qoQgg_jDgpUh.zuRZFi8o1RODhz238uui9H3ADu8mPDA09Ish2rVzapoSF2TDNBXR9_OGXn37p_s 
> GXVuELwYnPmcY3jKM3yMBMAruaXWihH61pn4EKM5pUk3.ujLyDihjMToZVqT7lbQy8s8n.uUbXyq 
> k1EowxHTLkb9RA1leL13OKRvQhx02OFpi_jR7N1DmhELW2gHmvSG.q_AglII5w2Kte0UVdsQ1w2i 
> ag9hBeJIkSM09WjXoSxBvh6vv1QSJ8IRX9_ZOQdCztMwE50OFmQPiqm5cO3YCyxbLjbaVeqakOv7 
> WqBg5SO68nvgrk3WyMaknoByUXIFYZC835Y7_pVoePzI6HZHrweqZNQmWu3VYMYh8jYJRQEdBw2T 
> yTcbtrX3jfqzWrXL7y4db1oNqOukjVCGdowtF8JWBqAmGutIv6KYIyQsZB3aC5y7VgFNLQPsODWO 
> fVZ7G3OeZiw0UAUo79ZdlMuqJ3feczW45JxlwyAgVmctKe8jVdokp7WPAaw118lL2g5S28JNhI70 
> Wjlw3XyRWSncAM2OSlnHLZHyj5qYVgNsGYHVRJPfqm32zoEP.RI9H6w3WQffN7hPtzP1IVsT9sry 
> WPQlN0UViekV.KC3QZP5ZXxKAr4I1ka33HhyBcFE8i0ILQ9ZcDDNMqShkGRWLbjfD_PW6DrIi2S9 
> .ipXc489W0RV7v9oLeza3E07O8ae._TDZaXGKXGBLx7kMfRaGjlue9lveyDq9K6Mdf_oXS_rUNdO 
> RsuFSlCv9gb1TTojAbCPeQkIsiypnePbrn4cApQe19NbXpV9Ot56368sR5wXf.oIJsWGUeyxkWO5 
> JNgqljSM_dipzlTuS68yLm2_rLBy4ePDmnXPsrotRRAtJ4s15v5CLkj.mo5.DQNqklSoxjwlT6ep 
> yr7uvh6Kc4Hrwcc54xs8Jd_scnpnaRLAeqPOeIFDhrXPJ_QB6kxtePKO1fVFks99RkPzL9qRcmKL 
> NzQp4gfUoexVtRBOuK8p0z_OMJ69EayGteNL_gFlAB1cXBVYqswBlBvc2ykhbOdHYdHYvReTuGkK 
> zI8_zOgm3.ReD_EBi4bmPyN_Q6PHpKZs-
> X-Sonic-MF:
> <zu...@yahoo.com>
> Received:
> from sonic.gate.mail.ne1.yahoo.com by sonic314.consmr.mail.ne1.yahoo.com 
> with HTTP; Mon, 17 Jan 2022 23:59:15 +0000
> Message-ID:
> <49...@mail.yahoo.com>
> MIME-Version:
> 1.0
> Content-Type:
> multipart/alternative; 
> boundary="----=_Part_1647577_1201937267.1642463893424"
> References:
> <49...@mail.yahoo.com>
> X-Mailer:
> WebService/1.1.19594 YMailNorrin
> 
> 
> Hi,
> I am doing model .clear but still, listbox holds duplicate entries. I 
> have problem in the function:
> 
> private void btnDispSavCurrActionPerformed(java.awt.event.ActionEvent evt)
> 
> 
> Following is my JFrame derived class, Somebody please guide me.
> 
> Zulfi.
> 
> 
> 
> package com.mycompany.inher2rbuttarrlist2;
> 
> import java.util.ArrayList;
> import javax.swing.ButtonGroup;
> import javax.swing.DefaultListModel;
> 
> /**
>   *
>   * @author zulfi
>   */
> public class RButtArrListJFrame extends javax.swing.JFrame {
>      ArrayList<Account> al_allAcc = new ArrayList<>();
>      //ArrayList<String> al_sav = new ArrayList<String>();
>      DefaultListModel <String> model = new DefaultListModel<String>();
>      Account acc1 = new Account(200.0, 100, "SSUET1", 's');
>      Account acc2 = new Account(300.0, 101, "SSUET2", 's');
>      Account acc3 = new Account(400.0, 102, "SSUET3", 's');
>      Account acc4 = new Account(500.0, 103, "SSUET4", 'c');
>      Account acc5 = new Account(600.0, 104, "SSUET5", 'c');
>      ButtonGroup group = new ButtonGroup();
>      /**
>       * Creates new form RButtArrListJFrame
>       */
>      public RButtArrListJFrame() {
>          initComponents();
>          group.add(currAccRadBtn);
>          group.add(savAccRadBtn);
>      }
> 
>      void storeAllAccountInfoinArrayList(){
>          
> //https://www.delftstack.com/howto/java/adding-objects-to-arraylist/
>          al_allAcc.add(acc1);
>          al_allAcc.add(acc2);
>          al_allAcc.add(acc3);
>          al_allAcc.add(acc4);
>          al_allAcc.add(acc5);
>      }
>      /**
>       * This method is called from within the constructor to initialize 
> the form.
>       * WARNING: Do NOT modify this code. The content of this method is 
> always
>       * regenerated by the Form Editor.
>       */
>      @SuppressWarnings("unchecked")
>      // <editor-fold defaultstate="collapsed" desc="Generated Code">
>      private void initComponents() {
> 
>          btnDispSavCurr = new javax.swing.JButton();
>          btnDispAll = new javax.swing.JButton();
>          jScrollPane1 = new javax.swing.JScrollPane();
>          jList1 = new javax.swing.JList<>();
>          savAccRadBtn = new javax.swing.JRadioButton();
>          currAccRadBtn = new javax.swing.JRadioButton();
> 
>          
> setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
> 
>          btnDispSavCurr.setText("Display Info Saving or Current");
>          btnDispSavCurr.addActionListener(new 
> java.awt.event.ActionListener() {
>              public void actionPerformed(java.awt.event.ActionEvent evt) {
>                  btnDispSavCurrActionPerformed(evt);
>              }
>          });
> 
>          btnDispAll.setText("Display All Accounts");
>          btnDispAll.addActionListener(new java.awt.event.ActionListener() {
>              public void actionPerformed(java.awt.event.ActionEvent evt) {
>                  btnDispAllActionPerformed(evt);
>              }
>          });
> 
>          jScrollPane1.setViewportView(jList1);
> 
>          savAccRadBtn.setText("Saving Account");
> 
>          currAccRadBtn.setText("Current Account");
> 
>          javax.swing.GroupLayout layout = new 
> javax.swing.GroupLayout(getContentPane());
>          getContentPane().setLayout(layout);
>          layout.setHorizontalGroup(
>              
> layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
>              .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, 
> layout.createSequentialGroup()
>                  
> .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
>                      
> .addGroup(javax.swing.GroupLayout.Alignment.LEADING, 
> layout.createSequentialGroup()
>                          .addGap(81, 81, 81)
>                          .addComponent(btnDispSavCurr)
>                          
> .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 
> 143, Short.MAX_VALUE)
> .addComponent(btnDispAll, javax.swing.GroupLayout.PREFERRED_SIZE, 144, 
> javax.swing.GroupLayout.PREFERRED_SIZE))
>                      .addGroup(layout.createSequentialGroup()
>                          .addGap(64, 64, 64)
>                          
> .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
>                              .addComponent(savAccRadBtn)
>                              .addComponent(currAccRadBtn))
> .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 
> javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
> .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, 
> javax.swing.GroupLayout.PREFERRED_SIZE)))
>                  .addGap(49, 49, 49))
>          );
>          layout.setVerticalGroup(
>              
> layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
>              .addGroup(layout.createSequentialGroup()
>                  .addGap(34, 34, 34)
>                  
> .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
>                      .addComponent(btnDispSavCurr)
>                      .addComponent(btnDispAll))
>                  
> .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
>                      .addGroup(layout.createSequentialGroup()
>                          .addGap(28, 28, 28)
> .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 
> javax.swing.GroupLayout.DEFAULT_SIZE, 
> javax.swing.GroupLayout.PREFERRED_SIZE))
>                      .addGroup(layout.createSequentialGroup()
>                          .addGap(55, 55, 55)
>                          .addComponent(savAccRadBtn)
>                          .addGap(51, 51, 51)
>                          .addComponent(currAccRadBtn)))
>                  .addContainerGap(210, Short.MAX_VALUE))
>          );
> 
>          pack();
>      }// </editor-fold>
> 
>      private void btnDispAllActionPerformed(java.awt.event.ActionEvent 
> evt) {
>          // TODO add your handling code here:
>          model.clear();
>          model.addElement(" " + acc1.getBalance() + " " + 
> acc1.getNumber() + acc1.name+ acc1.accType);
>          model.addElement(" " + acc2.getBalance() + " " + 
> acc2.getNumber() + acc2.name+ acc2.accType);
>          model.addElement(" " + acc3.getBalance() + " " + 
> acc3.getNumber() + acc3.name+ acc3.accType);
>          model.addElement(" " + acc4.getBalance() + " " + 
> acc4.getNumber() + acc4.name+ acc4.accType);
>          model.addElement(" " + acc5.getBalance() + " " + 
> acc5.getNumber() + acc5.name+ acc5.accType);
>          jList1.setModel(model);
>      }
> 
>      private void 
> btnDispSavCurrActionPerformed(java.awt.event.ActionEvent evt) {
>          // TODO add your handling code here:
>          storeAllAccountInfoinArrayList();
>          boolean iscurrAccRBSelected = currAccRadBtn.isSelected();
>          boolean issavAccRBSelected = savAccRadBtn.isSelected();
>          model.clear();
>          if(iscurrAccRBSelected){//Why in case of current Acc it is 
> storing acc4 and acc5 twice?
>              for (Account acc: al_allAcc) {
>                 //System.out.println("AccountTitle: "+acc.name+", 
> balance "+acc.getBalance()+ "and mnumber is "+acc.getNumber());
>                 if (acc.accType == 'c' )
>                    model.addElement(" " + acc.getBalance() + " " + 
> acc.getNumber() + acc.name+ acc.accType);
>                    jList1.setModel(model);
>              }
>              //code it
>              //ask why we creat rbgroup if we are not using the grp var
> 
>          }
>          else if(issavAccRBSelected){
>              for (Account acc: al_allAcc) {
>                 //System.out.println("AccountTitle: "+acc.name+", 
> balance "+acc.getBalance()+ "and mnumber is "+acc.getNumber());
>                 if (acc.accType == 's' )
>                    model.addElement(" " + acc.getBalance() + " " + 
> acc.getNumber() + acc.name+ acc.accType);
>                    jList1.setModel(model);
>              }
>          }
>      }
> 
>      /**
>       * @param args the command line arguments
>       */
>      public static void main(String args[]) {
>          /* Set the Nimbus look and feel */
>          //<editor-fold defaultstate="collapsed" desc=" Look and feel 
> setting code (optional) ">
>          /* If Nimbus (introduced in Java SE 6) is not available, stay 
> with the default look and feel.
>           * For details see 
> http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
>           */
>          try {
>              for (javax.swing.UIManager.LookAndFeelInfo info : 
> javax.swing.UIManager.getInstalledLookAndFeels()) {
>                  if ("Nimbus".equals(info.getName())) {
>                      
> javax.swing.UIManager.setLookAndFeel(info.getClassName());
>                      break;
>                  }
>              }
>          } catch (ClassNotFoundException ex) {
> java.util.logging.Logger.getLogger(RButtArrListJFrame.class.getName()).log(java.util.logging.Level.SEVERE, 
> null, ex);
>          } catch (InstantiationException ex) {
> java.util.logging.Logger.getLogger(RButtArrListJFrame.class.getName()).log(java.util.logging.Level.SEVERE, 
> null, ex);
>          } catch (IllegalAccessException ex) {
> java.util.logging.Logger.getLogger(RButtArrListJFrame.class.getName()).log(java.util.logging.Level.SEVERE, 
> null, ex);
>          } catch (javax.swing.UnsupportedLookAndFeelException ex) {
> java.util.logging.Logger.getLogger(RButtArrListJFrame.class.getName()).log(java.util.logging.Level.SEVERE, 
> null, ex);
>          }
>          //</editor-fold>
> 
>          /* Create and display the form */
>          java.awt.EventQueue.invokeLater(new Runnable() {
>              public void run() {
>                  new RButtArrListJFrame().setVisible(true);
>              }
>          });
>      }
> 
>      // Variables declaration - do not modify
>      private javax.swing.JButton btnDispAll;
>      private javax.swing.JButton btnDispSavCurr;
>      private javax.swing.JRadioButton currAccRadBtn;
>      private javax.swing.JList<String> jList1;
>      private javax.swing.JScrollPane jScrollPane1;
>      private javax.swing.JRadioButton savAccRadBtn;
>      // End of variables declaration
> }
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@netbeans.apache.org
For additional commands, e-mail: users-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists