Results 1 to 4 of 4
  1. #1
    kmccmk9 is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Trying to update a jList

    Hello, so basically below are all of my class code. What I'm trying to do is have a main window with a jList that contains a list of games tied to a user's accounts. I have the user login, and upon successful login, the list should be updated. However, no matter what code I try the list of games is passed and the listModel is updated and changed. But, the jList never changes from just displaying test. I'm really lost on this one, thanks for any help.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package frglauncher;
    
    /**
     *
     * @author KYLE-LAPTOP
     */
    public class FRGLauncher {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            final Main mainframe = new Main();
        mainframe.setTitle("Future Retro Gaming Launcher");
        mainframe.setVisible(true);
        }
    }
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package frglauncher;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.DefaultListModel;
    import javax.swing.JEditorPane;
    
    /**
     *
     * @author KYLE-LAPTOP
     */
    public class Main extends javax.swing.JFrame {
    
        /**
         * Creates new form Main
         */
         private javax.swing.JEditorPane editorWeb = new JEditorPane();
         public DefaultListModel listModel = new DefaultListModel();
        public Main() {
            initComponents();
            scrollWeb.setViewportView( editorWeb );
            editorWeb.setEditable(false);
            editorWeb.setSize(scrollWeb.getWidth(), scrollWeb.getHeight());
            try {
                editorWeb.setPage("http://www.futureretrogaming.tk/news.html");
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            listModel.addElement("test");
     
            
        }
        
        public void updateList(List<String> gamelist) {
            System.out.println("Entered updateList");
            listModel.clear();
            listGames.enable(false);
            listModel.addElement("test2");
            DefaultListModel listModel2 = new DefaultListModel();
            listModel2.addElement("test string");
            listGames.setModel(listModel2);
            
            for (int i=0;i<gamelist.size();i++) {
                System.out.println(gamelist.get(i).toString());
                System.out.println("Adding " + gamelist.get(i).toString() + " to the list");
                listModel.addElement(gamelist.get(i).toString());
                System.out.println(listModel.get(0).toString());
            }
            listGames.setModel(listModel);
        }
    
        /**
         * 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() {
    
            jPanel1 = new javax.swing.JPanel();
            jScrollPane1 = new javax.swing.JScrollPane();
            listGames = new javax.swing.JList(listModel);
            scrollWeb = new javax.swing.JScrollPane();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            menuLogin = new javax.swing.JMenuItem();
            menuClose = new javax.swing.JMenuItem();
            jMenu2 = new javax.swing.JMenu();
            jMenuItem4 = new javax.swing.JMenuItem();
            jMenuItem3 = new javax.swing.JMenuItem();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 51, Short.MAX_VALUE)
            );
    
            listGames.setModel(listModel);
            jScrollPane1.setViewportView(listGames);
    
            jMenu1.setText("File");
    
            menuLogin.setText("Login");
            menuLogin.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    menuLoginActionPerformed(evt);
                }
            });
            jMenu1.add(menuLogin);
    
            menuClose.setText("Close");
            menuClose.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    menuCloseActionPerformed(evt);
                }
            });
            jMenu1.add(menuClose);
    
            jMenuBar1.add(jMenu1);
    
            jMenu2.setText("Help");
    
            jMenuItem4.setText("Online Help");
            jMenu2.add(jMenuItem4);
    
            jMenuItem3.setText("Submit Bug");
            jMenu2.add(jMenuItem3);
    
            jMenuBar1.add(jMenu2);
    
            setJMenuBar(jMenuBar1);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(scrollWeb, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
                        .addComponent(scrollWeb))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void menuLoginActionPerformed(java.awt.event.ActionEvent evt) {                                          
            // TODO add your handling code here:
            final LoginWindow login = new LoginWindow();
            login.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            listModel.clear();
            listModel.addElement("sdex");
        login.setTitle("Login:");
        login.setVisible(true);
        }                                         
    
        private void menuCloseActionPerformed(java.awt.event.ActionEvent evt) {                                          
            System.exit(0);
        }                                         
    
        /**
         * @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(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Main.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 Main().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenu jMenu2;
        private javax.swing.JMenuBar jMenuBar1;
        private javax.swing.JMenuItem jMenuItem3;
        private javax.swing.JMenuItem jMenuItem4;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JList listGames;
        private javax.swing.JMenuItem menuClose;
        private javax.swing.JMenuItem menuLogin;
        private javax.swing.JScrollPane scrollWeb;
        // End of variables declaration                   
    }
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package frglauncher;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Scanner;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author KYLE-LAPTOP
     */
    public class LoginWindow extends javax.swing.JFrame {
    
        /**
         * Creates new form LoginWindow
         */
        List<String> games = new ArrayList<String>();
        Main program = new Main();
        public LoginWindow() {
            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() {
    
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            textUsername = new javax.swing.JTextField();
            textPassword = new javax.swing.JPasswordField();
            buttonLogin = new javax.swing.JButton();
    
            jLabel1.setText("Please provide your username and password:");
            jLabel1.setToolTipText("");
    
            jLabel2.setText("Username:");
    
            jLabel3.setText("Password:");
    
            buttonLogin.setText("Submit");
            buttonLogin.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonLoginActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textUsername))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(buttonLogin))
                    .addContainerGap(84, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(textUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(34, 34, 34)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                    .addComponent(buttonLogin)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>                        
    
        private void buttonLoginActionPerformed(java.awt.event.ActionEvent evt) {                                            
            try {
                // TODO add your handling code here:
                String loginurl = "http://futureretrogaming.tk/scripts/checklogin.php?username="+textUsername.getText()+"&password="+textPassword.getText();
                System.out.println(loginurl);
                URL checklogin = new URL(loginurl);
                URLConnection yc = checklogin.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
                URL file2 = new URL(loginurl);
                Scanner sc2 = new Scanner(file2.openStream());
                String inputLine = "";
                int i = 0;
                while (/*(inputLine = in.readLine()) != null*/sc2.hasNext()) {
                    inputLine = inputLine.trim();
                    String input = sc2.next();
                    input = input.trim();
                    if (inputLine.equalsIgnoreCase("false") || input.equalsIgnoreCase("false")) {
                        JOptionPane.showMessageDialog(this,"Username or Password is incorrect. Please try again.", "Error", JOptionPane.PLAIN_MESSAGE);
                        break;
                    }
                    else if (inputLine.equalsIgnoreCase("empty") || input.equalsIgnoreCase("empty")) {
                        JOptionPane.showMessageDialog(this,"You must type in a username and a password.", "Error",JOptionPane.PLAIN_MESSAGE);
                        break;
                    }
                    else {
                        System.out.println(input);
                        if (games == null)
                            System.out.print("games is null");
                        games.add(input);
                    }
                    
                }
                in.close();
                if (games!=null) {
                games.remove(0);
                Collections.sort(games);
                program.listModel.addElement("Element from other class");
                program.updateList(games);
                }
            } catch (MalformedURLException ex) {
                Logger.getLogger(LoginWindow.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(LoginWindow.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                           
    
        /**
         * @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(LoginWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(LoginWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(LoginWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(LoginWindow.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 LoginWindow().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton buttonLogin;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JPasswordField textPassword;
        private javax.swing.JTextField textUsername;
        // End of variables declaration                   
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,916
    Rep Power
    5

    Default Re: Trying to update a jList

    First, a couple of suggestions if you want help.

    1. Don't post large amounts of code unless someone specifically asks for it.
    2. Use javadoc (/**) comments and not regular block (/*) comments. It's not your fault but
    it screws up the formatter.
    3. Provide an SSCCE that demonstrates the problem. Don't provide unnecessary irrelevant code.
    (Example. Putting stuff in a JList that is extracted from a DB does not require all the code showing
    how you are accessing the DB).
    4. And as a general guideline, stay away from visual designers that layout the components for you. You will be
    better served in the long run to learn about layout managers.
    5. For more useful info, please read the FAQ.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Trying to update a jList

    6. Remove inane IDE-generated comments like
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    kmccmk9 is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: Trying to update a jList

    Quote Originally Posted by jim829 View Post
    First, a couple of suggestions if you want help.

    1. Don't post large amounts of code unless someone specifically asks for it.
    2. Use javadoc (/**) comments and not regular block (/*) comments. It's not your fault but
    it screws up the formatter.
    3. Provide an SSCCE that demonstrates the problem. Don't provide unnecessary irrelevant code.
    (Example. Putting stuff in a JList that is extracted from a DB does not require all the code showing
    how you are accessing the DB).
    4. And as a general guideline, stay away from visual designers that layout the components for you. You will be
    better served in the long run to learn about layout managers.
    5. For more useful info, please read the FAQ.

    Regards,
    Jim
    Quote Originally Posted by DarrylBurke View Post
    6. Remove inane IDE-generated comments like
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    db
    Thanks for the suggestion. Ya I shouldn't have posted all my code. However, I did just figure it out, basically what worked was to create just a jpanel form class and design that the way I wanted. Then I would call that in a jOptionsDialog.

Similar Threads

  1. Update JList problem
    By ostkanten in forum New To Java
    Replies: 5
    Last Post: 09-25-2012, 05:53 AM
  2. Update Jlist in JFrame
    By dragon_baby in forum AWT / Swing
    Replies: 4
    Last Post: 05-26-2012, 03:07 PM
  3. Replies: 4
    Last Post: 01-10-2012, 02:05 PM
  4. How update Jlist in java Chat
    By pappyj77 in forum Threads and Synchronization
    Replies: 59
    Last Post: 09-13-2011, 02:26 AM
  5. Cannot get my JList to update!
    By rangvald in forum AWT / Swing
    Replies: 1
    Last Post: 11-17-2009, 02:57 PM

Posting Permissions

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