Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: JlList not getting updated properly

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JlList not getting updated properly

    Ok, so this program is to scan a directory for video files and display them in a list. The user selects the directory from the JFileChooser option given in the File menu. So far, program does exactly what it is supposed to do. The only problem is the list is not getting updated properly when a user selects a directory for the second time. It seems like old list is overlapping the new list. I have tried to clear the list first and then add new elements to it but it just doesnt seem to work. Here is a screenshot of how it looks.
    bug.jpg
    Code
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package moviemanager;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.io.FilenameFilter;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    /**
     *
     * @author Vishal
     */
    public class MovieManager {
     
        private static class ListListener implements ListSelectionListener {
     
            public ListListener() {
            }
     
            @Override
            public void valueChanged(ListSelectionEvent e) {
                fileinfo.setText("Information of "+list.getSelectedValue().toString());
     
            }
        }
        private JFrame menu;
        private JPanel left,right,top,center;
        private JMenuBar mainmenu;
     
        public MovieManager(JFrame menu, JPanel left, JPanel right, JPanel top, JPanel center, JMenuBar mainmenu, JMenu file, JMenuItem seldir, String directoryname, JFileChooser jfc, JList list, String[] filenames, File directory, FilenameFilter filter, DefaultListModel listmodel, JTextArea fileinfo) {
            this.menu = menu;
            this.left = left;
            this.right = right;
            this.top = top;
            this.center = center;
            this.mainmenu = mainmenu;
            this.file = file;
            this.seldir = seldir;
            this.directoryname = directoryname;
            this.jfc = jfc;
            this.list = list;
            this.filenames = filenames;
            this.directory = directory;
            this.filter = filter;
            this.listmodel = listmodel;
            this.fileinfo = fileinfo;
        }
        private JMenu file;
        private JMenuItem seldir;
        private String directoryname;
        private JFileChooser jfc;
        private static JList list;
        private String filenames[];
        private File directory;
        private FilenameFilter filter;
        private DefaultListModel listmodel;
        private static JTextArea fileinfo;
        MovieManager(){
            JFrame main = new JFrame("Movie Manager");
            listmodel = new DefaultListModel();
            list = new JList(listmodel);
            mainmenu = new JMenuBar();
            file = new JMenu("File");
            seldir = new JMenuItem("Select Directory to scan");
            mainmenu.add(file);
            file.add(seldir);
            seldir.addActionListener(new MenuHandler());
            directoryname="F:/TV/How i met your mother/season 1/";
            fileinfo = new JTextArea("No File Selected yet",3,1);
            fileinfo.setSize(300, 250);
     
     
            //<<----------------MAIN WINDOW ------------>>
            main.setVisible(true);
            main.setBounds(340,150,700,500);
            main.setResizable(false);
            main.setLayout(new BorderLayout());
            main.setJMenuBar(mainmenu);
            //<----------End Window----------->>
            left= new JPanel();
            right = new JPanel();
            top = new JPanel();
            main.add(left,BorderLayout.WEST);
            main.add(right, BorderLayout.EAST);
            left.setLayout(new BorderLayout());
            main.add(top,BorderLayout.PAGE_START);
            top.setLayout(new GridLayout(1,0));
            left.add(new JLabel("Files Scanned:"),BorderLayout.NORTH);
            right.add(fileinfo, BorderLayout.EAST);
            fileinfo.setSize(250, 250);
            fileinfo.setLineWrap(true);
           // directory = new File(directoryname);
           // filenames = directory.list();
            //list = new JList(filenames);
            //left.add(new JScrollPane(list));
     
        }
        public static void main(String[] args) {
            MovieManager movieManager = new MovieManager();
        }
        private class MenuHandler implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent ae){
                menu = new JFrame("Select a folder to scan");
                menu.setVisible(true);
                menu.setBounds(340,150,500,500);
                menu.setResizable(false);
                jfc = new JFileChooser();
                jfc.setMultiSelectionEnabled(false);
                menu.add(jfc);
                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );
                jfc.addActionListener(new ScannerClass());
     
     
            }   //Emd actionPerformed()
        }   //End MenuHandler
     
        private class ScannerClass implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent ae){
                    String command = ae.getActionCommand();
                    if(command.equals("ApproveSelection")){
     
                       // directoryname = jfc.getSelectedFile().getAbsolutePath();
                        directory = new File(jfc.getSelectedFile().getAbsolutePath());
                        filter = new FilenameFilter(){
                        public boolean accept(File dir, String name) {
                            if(name.endsWith(".avi")|| name.endsWith(".mp4")|| name.endsWith(".mkv")){
                                return true;
                            }
                            return false;
                        }
                        };
                        String[] filename1 = directory.list(filter);
                        //listmodel.clear();
                        for(int i = 0; i< filename1.length;i++){
                            listmodel.add(i,filename1[i]);
                        }//END of For 
                        left.add(list, BorderLayout.CENTER);
                        list.updateUI();
                        left.add(new JScrollPane(list));
                        list.updateUI();
                        menu.dispose();
                        list.addListSelectionListener(new ListListener());
                    System.out.println(jfc.getSelectedFile().getAbsolutePath());
                    }else{
                        menu.dispose();
                        listmodel.clear();
     
                }   //End if
     
            }
        }
    }

    The program is written in NetBeans IDE 7.0


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: JlList not getting updated properly

    Is there a reason why you are adding your list (and list in a JScrollPane) in the actionPerformed method? Why not add it to the user interface to begin with, and just update the model from the listener. If you add it dynamically to the GUI such as this, you must validate the component

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JlList not getting updated properly

    I tried what you said, but again, the list doesn't update correctly when i choose a directory for the second time.

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: JlList not getting updated properly

    Quote Originally Posted by frozenmonkey View Post
    I tried what you said, but again, the list doesn't update correctly when i choose a directory for the second time.
    What did you try? Post the updated code

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JlList not getting updated properly

    Ok, i got it to work. JScrollPane at ActionListener was causing all the problems. Now, there is another problem. When i choose a directory to scan for the second time it throws null pointer exception (i guess at list.clear()). When i remove list.clear() it works but then old elements are retained in the List.

    Exceptions:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at moviemanager.MovieManager$ListListener.valueChange d(MovieManager.java:26)
    at javax.swing.JList.fireSelectionValueChanged(JList. java:1765)
    at javax.swing.JList$ListSelectionHandler.valueChange d(JList.java:1779)
    at javax.swing.DefaultListSelectionModel.fireValueCha nged(DefaultListSelectionModel.java:167)
    at javax.swing.DefaultListSelectionModel.fireValueCha nged(DefaultListSelectionModel.java:147)
    at javax.swing.DefaultListSelectionModel.fireValueCha nged(DefaultListSelectionModel.java:194)
    at javax.swing.DefaultListSelectionModel.removeIndexI nterval(DefaultListSelectionModel.java:660)
    at javax.swing.plaf.basic.BasicListUI$Handler.interva lRemoved(BasicListUI.java:2589)
    at javax.swing.AbstractListModel.fireIntervalRemoved( AbstractListModel.java:161)
    at javax.swing.DefaultListModel.clear(DefaultListMode l.java:490)
    at moviemanager.MovieManager$ScannerClass.actionPerfo rmed(MovieManager.java:131)
    at javax.swing.JFileChooser.fireActionPerformed(JFile Chooser.java:1718)
    at javax.swing.JFileChooser.approveSelection(JFileCho oser.java:1628)
    at javax.swing.plaf.basic.BasicFileChooserUI$ApproveS electionAction.actionPerformed(BasicFileChooserUI. java:914)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEven tMulticaster.java:272)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

    Code:
    /
    *
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package moviemanager;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.io.FilenameFilter;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    /**
     *
     * @author Vishal
     */
    public class MovieManager {
     
        private static class ListListener implements ListSelectionListener {
     
            public ListListener() {
            }
     
            @Override
            public void valueChanged(ListSelectionEvent e) {
                fileinfo.setText("Information of "+list.getSelectedValue().toString());
     
            }
        }
        private JFrame menu;
        private JPanel left,right,top,center;
        private JMenuBar mainmenu;
     
        public MovieManager(JFrame menu, JPanel left, JPanel right, JPanel top, JPanel center, JMenuBar mainmenu, JMenu file, JMenuItem seldir, String directoryname, JFileChooser jfc, JList list, String[] filenames, File directory, FilenameFilter filter, DefaultListModel listmodel, JTextArea fileinfo) {
            this.menu = menu;
            this.left = left;
            this.right = right;
            this.top = top;
            this.center = center;
            this.mainmenu = mainmenu;
            this.file = file;
            this.seldir = seldir;
            this.directoryname = directoryname;
            this.jfc = jfc;
            this.list = list;
            this.filenames = filenames;
            this.directory = directory;
            this.filter = filter;
            this.listmodel = listmodel;
            this.fileinfo = fileinfo;
        }
        private JMenu file;
        private JMenuItem seldir;
        private String directoryname;
        private JFileChooser jfc;
        private static JList list;
        private String filenames[];
        private File directory;
        private FilenameFilter filter;
        private DefaultListModel listmodel;
        private static JTextArea fileinfo;
        MovieManager(){
            JFrame main = new JFrame("Movie Manager");
            listmodel = new DefaultListModel();
            list = new JList(listmodel);
            mainmenu = new JMenuBar();
            file = new JMenu("File");
            seldir = new JMenuItem("Select Directory to scan");
            mainmenu.add(file);
            file.add(seldir);
            seldir.addActionListener(new MenuHandler());
            directoryname="F:/TV/How i met your mother/season 1/";
            fileinfo = new JTextArea("No File Selected yet",3,1);
            fileinfo.setSize(300, 250);
     
     
            //<<----------------MAIN WINDOW ------------>>
            main.setVisible(true);
            main.setBounds(340,150,700,500);
            main.setResizable(false);
            main.setLayout(new BorderLayout());
            main.setJMenuBar(mainmenu);
            //<----------End Window----------->>
            left= new JPanel();
            right = new JPanel();
            top = new JPanel();
            main.add(left,BorderLayout.WEST);
            main.add(right, BorderLayout.EAST);
            left.setLayout(new BorderLayout());
            main.add(top,BorderLayout.PAGE_START);
            top.setLayout(new GridLayout(1,0));
            left.add(new JLabel("Files Scanned:"),BorderLayout.NORTH);
            right.add(fileinfo, BorderLayout.EAST);
            fileinfo.setSize(250, 250);
            fileinfo.setLineWrap(true);
            left.add(list, BorderLayout.CENTER);
           // directory = new File(directoryname);
           // filenames = directory.list();
            //list = new JList(filenames);
            left.add(new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
     
        }
        public static void main(String[] args) {
            MovieManager movieManager = new MovieManager();
        }
        private class MenuHandler implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent ae){
                menu = new JFrame("Select a folder to scan");
                menu.setVisible(true);
                menu.setBounds(340,150,500,500);
                menu.setResizable(false);
                jfc = new JFileChooser();
                jfc.setMultiSelectionEnabled(false);
                menu.add(jfc);
                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );
                jfc.addActionListener(new ScannerClass());
     
     
            }   //Emd actionPerformed()
        }   //End MenuHandler
     
        private class ScannerClass implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent ae){
                    String command = ae.getActionCommand();
                    if(command.equals("ApproveSelection")){
     
                       // directoryname = jfc.getSelectedFile().getAbsolutePath();
                        directory = new File(jfc.getSelectedFile().getAbsolutePath());
                        //listmodel.clear();
                        filter = new FilenameFilter(){
                        public boolean accept(File dir, String name) {
                            if(name.endsWith(".avi")|| name.endsWith(".mp4")|| name.endsWith(".mkv")){
                                return true;
                            }
                            return false;
                        }
                        };
                        String[] filename1 = directory.list(filter);
     
                        for(int i = 0; i< filename1.length;i++){
                            listmodel.add(i,filename1[i]);
                        }//END of For 
                        menu.dispose();
                        list.addListSelectionListener(new ListListener());
                    System.out.println(jfc.getSelectedFile().getAbsolutePath());
                    }else{
                        menu.dispose();
                        listmodel.clear();
     
                }   //End if
     
            }
        }
    }
    Last edited by frozenmonkey; July 6th, 2011 at 01:14 PM.

  6. #6
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: JlList not getting updated properly

    On the line where the exception is thrown, add some println's to print the objects you try to access to see which one is null (from there you can backtrack to figure out the appropriate location to instantiate the object)

  7. The Following User Says Thank You to copeg For This Useful Post:

    frozenmonkey (July 7th, 2011)

  8. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JlList not getting updated properly

    Exception is thrown at listmodel.clear(). Now what should i actually print?
    Beginner Java, C++ Programming at www.mycoding.net

  9. #8
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: JlList not getting updated properly

    The exception isn't thrown at listmodel.clear(), it's thrown downstream at line 26, i.e. list.getSelectedValue().toString() in the list listener - the exception message tells you that:

    java.lang.NullPointerException
    at moviemanager.MovieManager$ListListener.valueChange d(MovieManager.java:26)


    Your problem happens because you're not checking the value returned by list.getSelectedValue() before trying to use it, and it is null when the list has been cleared. Just a little careless...

    Incidentally, if you're going to create a frame for this app, it helps if you set the default close operation to DISPOSE_ON_CLOSE, so it doesn't hang around in memory when you close it.

  10. The Following User Says Thank You to dlorde For This Useful Post:

    frozenmonkey (July 7th, 2011)

  11. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JlList not getting updated properly

    Thanks for the heads up pros, my program works flawlessly now. Cheers.
    Beginner Java, C++ Programming at www.mycoding.net

  12. #10
    Junior Member
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JlList not getting updated properly

    Ok, after adding some more functionality to my program, it is again causing small problems.
    I added this code to my program due to which JList is not getting updated. (I have to minimize/maximize for JList to get updated.)
    This code was added to ScannerClass
    String elim[] = {".","_","avi","dvdrip","xvid","hdtv","vtv","lol"};
                        for(int j=0;j<filename1.length;j++){
                            for (int k=0;k<elim.length;k++){
                                filename1[j] = filename1[j].replace(elim[k]," ");
                            }   //End nested loop
                            filename1[j]=filename1[j].trim();
                        }//End J for loop

    New Class to connect to a DB and Insert data into it.

        public static class Database implements ActionListener{
            Connection con;
            Statement stmt;
            Database(){
                try{
                    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MovieInfo","root","");
                    stmt = con.createStatement();
                }catch(SQLException se){
                    System.out.println("Could not connect to DB");
                    se.printStackTrace();
                }   //End catch
            }//End Database()


    Current program:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package moviemanager;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.io.FilenameFilter;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import java.sql.*;
    /**
     *
     * @author Vishal
     */
    public class MovieManager {
        private static JButton addDB;
        private JFrame menu;
        private JPanel left,right,top,center;
        private JMenuBar mainmenu;
        private static String fileinfodetails = null;
     
        public MovieManager(JFrame menu, JPanel left, JPanel right, JPanel top, JPanel center, JMenuBar mainmenu, JMenu file, JMenuItem seldir, String directoryname, JFileChooser jfc, JList list, String[] filenames, File directory, FilenameFilter filter, DefaultListModel listmodel, JTextArea fileinfo) {
            this.menu = menu;
            this.left = left;
            this.right = right;
            this.top = top;
            this.center = center;
            this.mainmenu = mainmenu;
            this.file = file;
            this.seldir = seldir;
            this.directoryname = directoryname;
            this.jfc = jfc;
            this.list = list;
            this.filenames = filenames;
            this.directory = directory;
            this.filter = filter;
            this.listmodel = listmodel;
            this.fileinfo = fileinfo;
        }
        private JMenu file;
        private JMenuItem seldir;
        private String directoryname;
        private JFileChooser jfc;
        private static JList list;
        private String filenames[];
        private File directory;
        private FilenameFilter filter;
        private DefaultListModel listmodel;
        private static JTextArea fileinfo;
        private JScrollPane listjcp;
     
        MovieManager(){
            JFrame main = new JFrame("Movie Manager");
            main.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            listmodel = new DefaultListModel();
            list = new JList(listmodel);
            mainmenu = new JMenuBar();
            file = new JMenu("File");
            seldir = new JMenuItem("Select Directory to scan");
            mainmenu.add(file);
            file.add(seldir);
            seldir.addActionListener(new MenuHandler());
            directoryname="F:/TV/How i met your mother/season 1/";
            fileinfo = new JTextArea("No File Selected yet",3,1);
            fileinfo.setSize(300, 250);
     
     
            //<<----------------MAIN WINDOW ------------>>
            main.setVisible(true);
            main.setBounds(340,150,700,500);
            main.setResizable(false);
            main.setLayout(new BorderLayout());
            main.setJMenuBar(mainmenu);
            //<----------End Window----------->>
            left= new JPanel();
            right = new JPanel();
            top = new JPanel();
            center = new JPanel();
            addDB = new JButton("Add To DB");
            main.add(center,BorderLayout.CENTER);
            center.setLayout(new FlowLayout());
            center.add(addDB);
            addDB.setEnabled(false);
            addDB.addActionListener(new Database());
            main.add(left,BorderLayout.WEST);
            main.add(right, BorderLayout.EAST);
            left.setLayout(new BorderLayout());
            main.add(top,BorderLayout.PAGE_START);
            top.setLayout(new GridLayout(1,0));
            left.add(new JLabel("Files Scanned:"),BorderLayout.NORTH);
            right.add(fileinfo, BorderLayout.EAST);
            fileinfo.setSize(250, 250);
            fileinfo.setLineWrap(true);
            left.add(list, BorderLayout.CENTER);
           // directory = new File(directoryname);
           // filenames = directory.list();
            //list = new JList(filenames);
            left.add(listjcp = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
            listjcp.setPreferredSize(new Dimension(250,500));
            list.addListSelectionListener(new ListListener());
     
        }
        public static void main(String[] args) {
            MovieManager movieManager = new MovieManager();
            Database database = new Database();
        }
        private class MenuHandler implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent ae){
                menu = new JFrame("Select a folder to scan");
                menu.setVisible(true);
                menu.setBounds(340,150,500,500);
                menu.setResizable(false);
                jfc = new JFileChooser();
                jfc.setMultiSelectionEnabled(false);
                menu.add(jfc);
                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );
                jfc.addActionListener(new ScannerClass());
     
     
            }   //Emd actionPerformed()
        }   //End MenuHandler
     
        private class ScannerClass implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent ae){
                    String command = ae.getActionCommand();
                    if(command.equals("ApproveSelection")){
     
                           // directoryname = jfc.getSelectedFile().getAbsolutePath();
                        directory = new File(jfc.getSelectedFile().getAbsolutePath());
                        filter = new FilenameFilter(){
                        public boolean accept(File dir, String name) {
                            if(name.endsWith(".avi")|| name.endsWith(".mp4")|| name.endsWith(".mkv")){
                                return true;
                            }
                            return false;
                        }
                        };
                        String[] filename1 = directory.list(filter);
                        listmodel.clear();
                        String elim[] = {".","_","avi","dvdrip","xvid","hdtv","vtv","lol"};
                        for(int j=0;j<filename1.length;j++){
                            for (int k=0;k<elim.length;k++){
                                filename1[j] = filename1[j].replace(elim[k]," ");
                            }   //End nested loop
                            filename1[j]=filename1[j].trim();
                        }//End J for loop
                        for(int i = 0; i< filename1.length;i++){
                            listmodel.add(i,filename1[i]);
                        }//END of For 
                        menu.dispose();
                        list.updateUI();
     
                    System.out.println(jfc.getSelectedFile().getAbsolutePath());
                    }else{
                        menu.dispose();
                        listmodel.clear();
     
                }   //End if
     
            }
        }
        public static class Database implements ActionListener{
            Connection con;
            Statement stmt;
            Database(){
                try{
                    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MovieInfo","root","");
                    stmt = con.createStatement();
                }catch(SQLException se){
                    System.out.println("Could not connect to DB");
                    se.printStackTrace();
                }   //End catch
            }//End Database()
            //@Override
            public void actionPerformed(ActionEvent ae){
                if(!list.isSelectionEmpty()){
                    try {
                        boolean rs = stmt.execute("INSERT INTO movie(mov_name) VALUES ('"+list.getSelectedValue().toString()+"')") ;
                        } catch (SQLException ex) {
                        System.out.println("SQL Error");
     
                    }
                }
            }
     
     }   //End of Database
     
        private static class ListListener implements ListSelectionListener {
     
            public ListListener() {
            }
     
            @Override
            public void valueChanged(ListSelectionEvent e){
     
                if(!list.isSelectionEmpty()){
                    addDB.setEnabled(true);
                fileinfodetails = list.getSelectedValue().toString();
                }
                else{
                    addDB.setEnabled(false);
                }
                fileinfo.setText(fileinfodetails);
     
            }
        }
     
    }

    No exceptions are thrown. So i have no clue where am i going wrong.
    Beginner Java, C++ Programming at www.mycoding.net

  13. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JlList not getting updated properly

    Any thoughts? :S
    Beginner Java, C++ Programming at www.mycoding.net

Similar Threads

  1. jdk is not installed properly error..
    By bczm8703 in forum Java SE APIs
    Replies: 6
    Last Post: June 1st, 2011, 06:33 AM
  2. Output not coming through properly
    By Camiot in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 16th, 2011, 09:25 PM
  3. Code doesn't execute properly
    By fride360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 11:36 AM
  4. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM