Zulfi,

That is because you add the items every time when the display all accounts
buttons is pressed.
A list has now problems containing duplicate accounts, so expect no help
from the list.
You might want to update the model only when needed, not on each display.

On Thu, Jan 13, 2022 at 3:33 AM Zulfi Khan <zulfi6...@yahoo.com.invalid>
wrote:

> Hi,
>
> I have created a java swing application using NetBeans 12.6 but when I am
> storing data in the ListBox, I am getting repeated data. I store the data
> on the listBox by clicking on the “Display Info Saving or Current Button”
> and by selecting “Current Account” radio Button as shown in the attached
> image. My Jframe class code is given below:
>
> 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.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
>
>
>
>         }
>
>         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
>
> }
>
>
>
> My Account.java class is given below:
>
>
>
> package com.mycompany.inher2rbuttarrlist2;
>
>
>
> /**
>
>  *
>
>  * @author zulfi
>
>  */
>
> public class Account {
>
>       private double balance;
>
>     private int number;
>
>     String name;
>
>     char accType;
>
>     Account(){
>
>         balance = 0.0;
>
>         number = 0;
>
>         final String name = "0";
>
>         accType ='\0';
>
>     }
>
>     Account(double dBal, int iNum, String strName, char chAccType){
>
>         balance = dBal;
>
>         number = iNum;
>
>         name = strName;
>
>         accType = chAccType;
>
>     }
>
>     Account (Account accObj){
>
>         balance = accObj.balance;
>
>         number = accObj.number;
>
>         name = accObj.name;
>
>         accType = accObj.accType;
>
>     }
>
>     double getBalance(){
>
>         return balance;
>
>     }
>
>     int getNumber(){
>
>         return number;
>
>     }
>
>     char getAccType(){
>
>         return accType;
>
>     }
>
>     void setBalance(double b){
>
>         balance =b;
>
>
>
>     }
>
>
>
> }
>
> Somebody please help me.
>
> Zulfi.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@netbeans.apache.org
> For additional commands, e-mail: users-h...@netbeans.apache.org
>
> For further information about the NetBeans mailing lists, visit:
> https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists



-- 
Pieter Van den Hombergh.
No software documentation is complete with out it's source code.

Reply via email to