Hi Peter Van den hombergh,I added model.clear in : 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); }
and the above code is working fine now but I also added model.clear in the following code but it is not working: 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); } } } Somebody please guide me. Zulfi. On Thursday, January 13, 2022, 08:34:42 AM CST, Pieter van den Hombergh <pieter.van.den.hombe...@gmail.com> wrote: 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.6but when I am storing data in the ListBox, I am getting repeated data. I storethe data on the listBox by clicking on the “Display Info Saving or CurrentButton” and by selecting “Current Account” radio Button as shown in theattached 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 = newAccount(200.0, 100, "SSUET1", 's'); Account acc2 = newAccount(300.0, 101, "SSUET2", 's'); Account acc3 = newAccount(400.0, 102, "SSUET3", 's'); Account acc4 = newAccount(500.0, 103, "SSUET4", 'c'); Account acc5 = new Account(600.0,104, "SSUET5", 'c'); ButtonGroup group =new ButtonGroup(); /** * Creates new formRButtArrListJFrame */ publicRButtArrListJFrame() { initComponents(); group.add(currAccRadBtn); group.add(savAccRadBtn); } voidstoreAllAccountInfoinArrayList(){ //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 iscalled from within the constructor to initialize the form. * WARNING: Do NOTmodify this code. The content of this method is always * regenerated bythe Form Editor. */ @SuppressWarnings("unchecked") // <editor-folddefaultstate="collapsed" desc="Generated Code"> private voidinitComponents() { btnDispSavCurr= new javax.swing.JButton(); btnDispAll =new javax.swing.JButton(); jScrollPane1 =new javax.swing.JScrollPane(); jList1 = newjavax.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 voidactionPerformed(java.awt.event.ActionEvent evt) { btnDispSavCurrActionPerformed(evt); } }); btnDispAll.setText("Display All Accounts"); btnDispAll.addActionListener(new java.awt.event.ActionListener() { public voidactionPerformed(java.awt.event.ActionEvent evt) { btnDispAllActionPerformed(evt); } }); jScrollPane1.setViewportView(jList1); savAccRadBtn.setText("Saving Account"); currAccRadBtn.setText("CurrentAccount"); javax.swing.GroupLayout layout = newjavax.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 voidbtnDispAllActionPerformed(java.awt.event.ActionEvent evt) { // TODO addyour 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.ActionEventevt) { // TODO addyour handling code here: storeAllAccountInfoinArrayList(); booleaniscurrAccRBSelected = currAccRadBtn.isSelected(); booleanissavAccRBSelected = savAccRadBtn.isSelected(); model.clear(); if(iscurrAccRBSelected){//Why in case of current Acc it is storing acc4and 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 } elseif(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 thecommand line arguments */ public static voidmain(String args[]) { /* Set theNimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Lookand feel setting code (optional) "> /* If Nimbus(introduced in Java SE 6) is not available, stay with the default look andfeel. * For detailssee 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 anddisplay the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { newRButtArrListJFrame().setVisible(true); } }); } // Variablesdeclaration - do not modify privatejavax.swing.JButton btnDispAll; private javax.swing.JButtonbtnDispSavCurr; privatejavax.swing.JRadioButton currAccRadBtn; privatejavax.swing.JList<String> jList1; privatejavax.swing.JScrollPane jScrollPane1; privatejavax.swing.JRadioButton savAccRadBtn; // End of variablesdeclaration } My Account.java class is given below: package com.mycompany.inher2rbuttarrlist2; /** * * @author zulfi */ public class Account { private doublebalance; private int number; String name; char accType; Account(){ balance = 0.0; number = 0; final Stringname = "0"; accType ='\0'; } Account(doubledBal, int iNum, String strName, char chAccType){ balance = dBal; number = iNum; name = strName; accType =chAccType; } Account (AccountaccObj){ balance =accObj.balance; number =accObj.number; name =accObj.name; accType =accObj.accType; } doublegetBalance(){ return balance; } int getNumber(){ return number; } char getAccType(){ return accType; } voidsetBalance(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.