Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By raffs03

Thread: user input doesn't save in DefaultListModel(JFrame Form)

  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Unhappy user input doesn't save in DefaultListModel(JFrame Form)

    Java Code:
    package gamecomp;
    
    import javax.swing.*;
    import java.util.*;
    
    /**
     *
     * @author knickozy
     */
    public class GameComp extends javax.swing.JFrame {
    final DefaultListModel model = new DefaultListModel();
    List<String> list = new ArrayList<>();
        /**
         * Creates new form GameComp
         */
        public GameComp() {
            initComponents();
           
        }
    
        /**
         * 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() {
    
            jButton4 = new javax.swing.JButton();
            btnAddData = new javax.swing.JButton();
            btnExit = new javax.swing.JButton();
            genreCmbx = new javax.swing.JComboBox();
            jLabel2 = new javax.swing.JLabel();
            compCmbx = new javax.swing.JComboBox();
            jScrollPane2 = new javax.swing.JScrollPane();
            listComp = new javax.swing.JList(model);
            btnView = new javax.swing.JButton();
    
            jButton4.setText("jButton4");
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Backup Database(CD)");
            setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
            setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
            setName("gameCompFrame"); // NOI18N
    
            btnAddData.setText("Add New Data");
            btnAddData.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnAddDataActionPerformed(evt);
                }
            });
    
            btnExit.setText("Exit");
            btnExit.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnExitActionPerformed(evt);
                }
            });
    
            genreCmbx.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-Select Genre-", "PC(Games)", "PC(Apps)", "PSX", "PS2", "PS3(PSN/Apps)", "Movies", "Others" }));
            genreCmbx.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    genreCmbxActionPerformed(evt);
                }
            });
    
            jLabel2.setText("Reminders: Use HJSplit to join .001,.002 files...");
    
            compCmbx.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-Select Backup Type-", "CD", "HDD(PS3 Games)", "HDD(Backups)" }));
            compCmbx.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    compCmbxActionPerformed(evt);
                }
            });
    
            listComp.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
            String [] sel ={ "1","2","3","4","5"};
            for(int j=0;j<sel.length;j++){
                list.add(sel[j]);
            }
            for(int i =0;i<list.size();i++){
                model.addElement(list.get(i));
            }
            listComp.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
            jScrollPane2.setViewportView(listComp);
    
            btnView.setText("View");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(2, 2, 2)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(btnView, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnAddData, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                                .addComponent(btnExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(compCmbx, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(genreCmbx, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(44, 44, 44))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(compCmbx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnAddData))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(genreCmbx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnView))
                            .addGap(18, 18, 18)
                            .addComponent(btnExit)))
                    .addContainerGap(183, Short.MAX_VALUE))
            );
    
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-749)/2, (screenSize.height-382)/2, 749, 382);
        }// </editor-fold>                        
    
        private void compCmbxActionPerformed(java.awt.event.ActionEvent evt) {                                         
          if(compCmbx.getSelectedIndex() == 2){
              new HDDbackup().setVisible(true);   
              setVisible(false);
          }
          else if(compCmbx.getSelectedIndex() == 3){
              new PS3backups().setVisible(true);
              setVisible(false);
          }
            
        }                                        
    
        private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {                                        
           System.exit(1);
        }                                       
    
        private void btnAddDataActionPerformed(java.awt.event.ActionEvent evt) {                                           
          
            String addDat = JOptionPane.showInputDialog("Enter new Value"); 
           model.addElement(addDat);
           
        }                                          
    
        private void genreCmbxActionPerformed(java.awt.event.ActionEvent evt) {                                          
            
        }                                         
    
        /**
         * @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(GameComp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(GameComp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(GameComp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(GameComp.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 GameComp().setVisible(true);
                    
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton btnAddData;
        private javax.swing.JButton btnExit;
        private javax.swing.JButton btnView;
        private javax.swing.JComboBox compCmbx;
        private javax.swing.JComboBox genreCmbx;
        private javax.swing.JButton jButton4;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JList listComp;
        // End of variables declaration                   
    }
    Everytime i switch to another Form, the input value that i put on String addDat and use the addElement code to put in DefaultListModel is displaying but when i switch to another Frame the value is gone... I try to add the input value at the arraylist but it doesn't display in JList Box.
    any help would be appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: user input doesn't save in DefaultListModel(JFrame Form)

    You might want to show the code that swaps windows. Note that for my money, I prefer not to use (or code) applications where windows are flung in and out of view as it's not a great user interface experience. You may be better off swapping views using a CardLayout.

  3. #3
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: user input doesn't save in DefaultListModel(JFrame Form)

    Thanks for your reply i will try to study the Java CardLayout :D I will comment again if its works.

  4. #4
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: user input doesn't save in DefaultListModel(JFrame Form)

    I think i got it working :D and my deeign got better tnx man!!!
    Fubarable likes this.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: user input doesn't save in DefaultListModel(JFrame Form)

    Glad you've got things working, and you're welcome!

Similar Threads

  1. How to make a Java Console Program save user made changes?
    By Chad.Curtiss in forum New To Java
    Replies: 3
    Last Post: 07-14-2012, 08:58 PM
  2. Security alert doesn't accept user input?
    By DLpres in forum New To Java
    Replies: 4
    Last Post: 02-03-2012, 07:57 AM
  3. Save logged in user
    By Skipito in forum New To Java
    Replies: 4
    Last Post: 12-06-2011, 12:38 PM
  4. Save Input of User in Strings!
    By Alerhau in forum New To Java
    Replies: 5
    Last Post: 06-12-2011, 11:20 AM
  5. save user input
    By ardel in forum New To Java
    Replies: 9
    Last Post: 02-15-2011, 04:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •