Question

basically what i am trying to do is, i have a JList which contain a list of available drive, if one of that drive selected by user then i will show all html files which located in selected drive in a JTable, so i put an event listener for my JList and then i create a JTable and put all data there and show it in the container. the code look like this:

static class HtmlListing implements ListSelectionListener
        {
            public void valueChanged(ListSelectionEvent event) 
            {
              if (!event.getValueIsAdjusting())
              {   //trying to remove and re-add controls in container.
                  EastCont.removeAll(); 

                  globarr = new ArrayList<File>(); // global variable

                  FileListing fl = new FileListing();
                  fl.walk(fileList1.getSelectedValue() + "work\\airasia\\html", 500, 0);    

                  //if(globarr.size() > 0)
                  //{
                       Object[][] data = new Object[globarr.size()][globarr.size()];

                       for(int i = 0; i < globarr.size(); i++)
                       {
                           if(globarr.get(i).isFile())
                           {
                               String filename =  globarr.get(i).getName().toString();
                               String date = sdf.format(globarr.get(i).lastModified());

                               Object[] obj = new Object[] {filename,  filename.substring(filename.lastIndexOf(".") + 1), date, globarr.get(i).getAbsolutePath()};
                               data[i] = obj;
                           }
                       }

                      Object[] column = new Object[]{"name ", "type", "date modified", "path"}; 

                      DefaultTableModel model = new DefaultTableModel(data, column);
                      model.fireTableDataChanged();

                       table =  new JTable(model) 
                       {
                            private static final long serialVersionUID = 1L;

                            public boolean isCellEditable(int row, int column) 
                            {                
                                    return false;               
                            };  
                        };

                       table.addMouseListener(new MouseAdapter()
                       {
                             public void mouseClicked(MouseEvent e)
                             {
                                  if (e.getClickCount() == 2)
                                  {
                                      int rowIdx = table.getSelectedRow(); // path to your new file
                                      TableModel tm = table.getModel();
                                      String path =  tm.getValueAt(rowIdx, 3).toString(); 
                                      File htmlFile = new File(path);

                                      try // open the default web browser for the HTML page
                                      {
                                          Desktop.getDesktop().browse(htmlFile.toURI());
                                          //Desktop.getDesktop().open(htmlFile);
                                      } 
                                      catch (IOException e1) 
                                      {
                                            // TODO Auto-generated catch block
                                            e1.printStackTrace();
                                      }
                                  }
                             }
                        });

                       table.removeColumn(table.getColumnModel().getColumn(3)); //hide column path from display
                       table.setFillsViewportHeight(true);
                       table.setIntercellSpacing(new Dimension(0, 5));
                       table.setShowGrid(false);

                       scrollPane = new JScrollPane(table);

                       EastCont = new JPanel();
                       EastCont.setLayout(new BorderLayout());
                       EastCont.add(scrollPane);
                       EastCont.setPreferredSize(new Dimension(1050, 1000));

                       //EastCont.repaint();
                       //EastCont.revalidate();

                       gui.add(EastCont, BorderLayout.EAST);
                       gui.revalidate();
                       gui.repaint();  
                // }
                // else                
                // {
                //     EastCont.remove(table);
                //     gui.remove(EastCont);
                //     gui.revalidate();
                //     gui.repaint();
                // } 
            }    

this code work only for first time, but does not working for second time and so on, so what i miss here? any help would be great. thank you.

Was it helpful?

Solution

DefaultTableModel model = new DefaultTableModel(data, column);
//model.fireTableDataChanged();
//table =  new JTable(model) 
table.setModel( model );

Don't create a new table change reset the model of your current table. The rest of the code in that method is not necessary either since you are not creating any new GUI components.

Also, never invoke a fireXXX method. That is the responsibility of the TableModel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top