2013-10-25 47 views
0

基本上我想要做的是,我有一個JList,其中包含可用驅動器的列表,如果其中一個驅動器被用戶選中,那麼我將顯示位於選定驅動器中的所有html文件在JTable中,所以我把我的JList的事件監聽器,然後創建一個JTable並將所有數據放在那裏並顯示在容器中。代碼如下所示:爲什麼JTable無法刷新DefaultTableModel?

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(); 
       // } 
      }  

此代碼的工作只爲第一次,但沒有工作第二次等等,所以我在這裏錯過?任何幫助都會很棒。謝謝。

回答

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

不創建新表更改重置當前表的模型。由於您沒有創建任何新的GUI組件,因此該方法中的其餘代碼不是必需的。

另外,請不要調用fireXXX方法。這是TableModel的責任。

+0

謝謝..我已經嘗試過,但仍然沒有運氣。 – NomNomNom

+1

這是正確的解決方案。您還需要刪除'removeAll()'方法。沒有必要添加或替換任何組件。 – camickr

+0

現在它正在工作,我將部分代碼移至void main,正如您建議刪除不必要的部分。 – NomNomNom