8 Replies Latest reply: Jun 3, 2007 1:49 AM by 807606 RSS

    Cannot get the size of the list

    807606
      I have made a JList and a browse button, when the user click the button and select the files, then it will show the files in the JList. Here is the code for update the jlist
          private void LoadFileList(String path){     
              addFileList.setModel(addFileListModel);
              addFileListModel.addElement(path); 
              addFileList = new JList(addFileListModel);        
          }
      However, when i check which values are selected, it always show -1 and the size of the list is always 0.

      Here is my code
              int[] selectedList = addFileList.getSelectedIndices();
              int sizeSelected = selectedList.length; //0
              Dimension size = addFileList.getSize(); //0
      I would like to know why the values are always 0 even the list get some elements.

      Thank you.
        • 1. Re: Cannot get the size of the list
          807606
          You already know that this is not the correct forum for Swing questions. You even once used the Swing forum. Please in future post your Swing questions into the Swing forum.

          You seem to be confused about what selected means. Selected means the items of the Jlist that are selected (highlighted, mouse clicky etc by the user) not all the items as you seem to think.

          Why are you not using a FileChooser?
          • 2. Re: Cannot get the size of the list
            807606
            How about try this, (but I really don't get what you are doing):
            private void LoadFileList(String path) {
                addFileList = new JList(new Vector().add(path));
            }
            • 3. Re: Cannot get the size of the list
              807606
              Sorry for that, i will use the swing forum next time.

              I know the selectedvalues... and maybe i should describe it in details...

              My question is even i selected 2 or 3 of the elements in the list, it still return 0.. i have checked the size of element in the jlist and it also returns 0. However, the jlist is containing elements.. which really confuses me..

              in the browse button, i am using filechooser to choose file.. and add the file name to the list by defaultListModel.

              Thank you
              • 4. Re: Cannot get the size of the list
                807606
                Here is the full version of my code
                public class PhotoGallery extends javax.swing.JApplet {
                    DropTarget dt = new DropTarget();
                
                    DefaultListModel addFileListModel = new DefaultListModel();
                    DefaultListModel addServerFileModel = new DefaultListModel();;
                    /** Initializes the applet PhotoGallery */
                    public void init() {
                        try {
                            java.awt.EventQueue.invokeAndWait(new Runnable() {
                                public void run() {
                                    initComponents();
                           
                                }
                            });
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                    
                    /** This method is called from within the init() method to
                     * initialize the form.
                     * WARNING: Do NOT modify this code. The content of this method is
                     * always regenerated by the Form Editor.
                     */
                    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
                    private void initComponents() {
                        jPanel1 = new javax.swing.JPanel();
                        filePath = new javax.swing.JTextField();
                        btnBrowse = new javax.swing.JButton();
                        btnAdd = new javax.swing.JButton();
                        brnRemove = new javax.swing.JButton();
                        jScrollPane1 = new javax.swing.JScrollPane();
                        addFileList = new javax.swing.JList();
                        jScrollPane2 = new javax.swing.JScrollPane();
                        serverFileList = new javax.swing.JList();
                        status = new javax.swing.JLabel();
                
                        filePath.setEditable(false);
                
                        btnBrowse.setText("Browse");
                        btnBrowse.addActionListener(new java.awt.event.ActionListener() {
                            public void actionPerformed(java.awt.event.ActionEvent evt) {
                                btnBrowseActionPerformed(evt);
                            }
                        });
                
                        btnAdd.setText("Add");
                        btnAdd.addActionListener(new java.awt.event.ActionListener() {
                            public void actionPerformed(java.awt.event.ActionEvent evt) {
                                btnAddActionPerformed(evt);
                            }
                        });
                
                        brnRemove.setText("Remove");
                        brnRemove.addActionListener(new java.awt.event.ActionListener() {
                            public void actionPerformed(java.awt.event.ActionEvent evt) {
                                brnRemoveActionPerformed(evt);
                            }
                        });
                
                        addFileList.setDragEnabled(true);
                        jScrollPane1.setViewportView(addFileList);
                
                        jScrollPane2.setViewportView(serverFileList);
                
                        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
                        jPanel1.setLayout(jPanel1Layout);
                        jPanel1Layout.setHorizontalGroup(
                            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(45, 45, 45)
                                        .addComponent(filePath, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(btnBrowse))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addContainerGap()
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(brnRemove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGap(21, 21, 21)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addContainerGap()
                                        .addComponent(status)))
                                .addContainerGap())
                        );
                        jPanel1Layout.setVerticalGroup(
                            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(58, 58, 58)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(filePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnBrowse))
                                .addGap(24, 24, 24)
                                .addComponent(status)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(13, 13, 13)
                                        .addComponent(btnAdd)
                                        .addGap(38, 38, 38)
                                        .addComponent(brnRemove))
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE))
                                .addGap(36, 36, 36))
                        );
                
                        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)
                        );
                        layout.setVerticalGroup(
                            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(25, Short.MAX_VALUE))
                        );
                    }// </editor-fold>                        
                
                    private void brnRemoveActionPerformed(java.awt.event.ActionEvent evt) {                                          
                // TODO add your handling code here:
                        status.setText("Removing");
                        int[] selectedList = addFileList.getSelectedIndices();
                        int sizeSelected = selectedList.length;
                        for (int i=0; i<sizeSelected; i++){
                            try{
                                AppletContext a = getAppletContext();
                                URL url = new URL("http://www.abc.com");
                                a.showDocument(url, "_blank");
                            }catch (MalformedURLException e){
                                 System.out.println(e.getMessage());
                            }
                        }
                    }                                         
                
                    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
                // TODO add your handling code here:
                        status.setText("Adding");
                        int[] selectedList = addFileList.getSelectedIndices();
                        int sizeSelected = selectedList.length;
                        Dimension size = addFileList.getSize();
                        status.setText(size.toString());
                      //  status.setText(Integer.toString(sizeSelected));
                
                        for (int i=0; i<sizeSelected; i++){
                            try{       
                                AppletContext a = getAppletContext();
                                URL url = new URL("http://www.abc.com");
                                a.showDocument(url, "_blank");
                            }catch (MalformedURLException e){
                                 System.out.println(e.getMessage());
                            }
                        }
                   
                    }                                      
                
                    private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {                                          
                // TODO add your handling code here:
                        JFileChooser fc = new JFileChooser();
                        fc.setMultiSelectionEnabled(true);
                        
                        fc.setDragEnabled(true);
                        int retval = fc.showOpenDialog(PhotoGallery.this);
                        
                        if (retval == JFileChooser.APPROVE_OPTION) {
                            //... The user selected a file, get it, use it.
                            File[] files = fc.getSelectedFiles();
                            String[] fileNames = new String[files.length];
                            filePath.setText(files[0].getName().toString());
                            for (int i = 0; i < files.length; i++) {
                                fileNames[i] = files.getPath();
                //System.out.println(fileNames[i]);
                LoadFileList(fileNames[i]);
                }


                }
                }

                • 5. Re: Cannot get the size of the list
                  807606
                  Thank you.

                  What i am trying t do is that.

                  I want to choose the files (maybe more than one) and the selected files will be displayed in the list. The user can choose the files in the list and click the "add" button to upload the files to the server.

                  Really thanks for your help.
                  • 6. Re: Cannot get the size of the list
                    807606
                    i tried this before
                           addFileListVector.add(path);
                            addFileList = new JList(addFileListVector);
                    but it doesnt work
                    • 7. Re: Cannot get the size of the list
                      807606
                      Are you aware of the restrictions of Applets when it comes to using files?
                      • 8. Re: Cannot get the size of the list
                        807606
                        i know i need to sign the applet before it can access the file system of the client... i think the problem may not come from here since it has already added into the list and the list should be able to tell if there are anything in the list.

                        actually i am wondering if it's because i have set the model of the list as DefaultListModel so that causing the error...

                        I have no ideas about this...

                        Hope someone can help.

                        Thank you.

                        Message was edited by:
                        MIS