2012-10-01 54 views
2

這裏是我的代碼:JTable中沒有顯示的列名

public class DownloadMainView extends JFrame{ 
    private ArrayList<DownloadItem> downloadList = new ArrayList<DownloadItem>(); 
    private JMenuBar menubar = new JMenuBar(); 
    private JMenu m_task = new JMenu("Tasks"); 
    private JMenu m_tool = new JMenu("Tools"); 
    private JMenu m_help = new JMenu("Help"); 

    private JMenuItem mi_add = new JMenuItem("Add"); 
    private JMenuItem mi_exit = new JMenuItem("Exit"); 
    private JMenuItem mi_options = new JMenuItem("Options"); 
    private JMenuItem mi_help = new JMenuItem("Help"); 
    private JMenuItem mi_about = new JMenuItem("About"); 

    private JTree categoryTree = new JTree(); 
    private JTable contentTable = new JTable(new Object[][]{},new Object[]{"No.","Filename","URL","Status","Size","Added Date"}); 
    private JToolBar toolbar = new JToolBar(); 
    private JScrollPane scrollPane = new JScrollPane(); 

    private JButton btnAdd = new JButton("Add"); 
    private JButton btnCancel = new JButton("Cancel"); 
    private JButton btnDelete = new JButton("Delete"); 
    private JButton btnOption = new JButton("Option"); 

    public DownloadMainView() throws IOException{ 
     super("KPDownloader"); 
     setSize(800,400); 
     setLayout(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     //Build menubar 
     menubar.add(m_task); 
     menubar.add(m_tool); 
     menubar.add(m_help); 
     m_task.add(mi_add); 
     mi_add.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,ActionEvent.CTRL_MASK)); 
     m_task.add(new JSeparator()); 
     m_task.add(mi_exit); 
     mi_exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.ALT_MASK)); 
     m_tool.add(mi_options); 
     mi_options.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK)); 
     m_help.add(mi_help); 
     mi_help.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0)); 
     m_help.add(mi_about); 
     mi_about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,ActionEvent.CTRL_MASK)); 
     setJMenuBar(menubar); 
     //about buttons 
     toolbar.add(btnAdd);   
     toolbar.add(btnOption); 
     toolbar.add(btnCancel); 
     toolbar.add(btnDelete); 
     toolbar.setLocation(0, 0); 
     toolbar.setSize(800,42); 
     this.add(toolbar); 
     //add table to mainview 
     String columns[] = {"No.","Filename","URL","Status","Size","Added Date"}; 
     DefaultTableModel model = new DefaultTableModel(columns,1); 
     readDownloadList(); 
     if(downloadList != null){ 
      int length = downloadList.size(); 
      for(int i = 0; i < length; i++) 
       model.insertRow(i, new Object[]{i, 
         downloadList.get(i).getFilename(),downloadList.get(i).getSize(), 
         downloadList.get(i).getStatus(), 
         downloadList.get(i).getURL(),downloadList.get(i).getAddedDate()}); 
     } 

     contentTable.setModel(model); 
     contentTable.setSize(800, 350); 
     scrollPane.add(contentTable); 
     scrollPane.setSize(800, 350); 
     scrollPane.setLocation(0, 50); 
     this.add(scrollPane); 

    } 

但是當我運行我的代碼,表中沒有顯示的列名。它只顯示一個空行,因爲我將1設置爲此行:DefaultTableModel model = new DefaultTableModel(columns,1);

請告訴我我的代碼錯在哪裏? 謝謝!

編輯:正如有人問同樣的問題(在JTable中沒有頭銜),但答案是添加JTable中JScrollPane中,這不利於@@ 編輯:嗨,我已經添加了我的全部構造和這裏是readDownloadList()方法的代碼:

void readDownloadList(){ 
      File file = new File("downloadlist.dat"); 
      ObjectInputStream ois = null; 
      if(!file.exists()) 
       try { 
        file.createNewFile(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      try { 
       ois = new ObjectInputStream(new FileInputStream(file)); 
       downloadList = (ArrayList<DownloadItem>) ois.readObject(); 
       ois.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       downloadList = new ArrayList<DownloadItem>(); 
       e.printStackTrace(); 
      }catch(ClassNotFoundException e){ 
       downloadList = new ArrayList<DownloadItem>(); 
       e.printStackTrace(); 
      } 
     } 

謝謝!

+0

通常JScrollPane會提供幫助。請發佈一些我們可以運行的代碼,並嘗試諸如我們找出問題所在。 –

+0

嗨Dan,我添加了我的完整構造函數,請再次查看! – Songokute

+0

好的,看我的答案。 –

回答

5

您以錯誤的方式使用了JScrollPane。要使其正常工作,只需執行以下操作。

傳遞JTable實例的JScrollPane在構造函數中:

private JScrollPane scrollPane = new JScrollPane(contentTable); 

註釋掉,您使用的JTable添加到JScrollPane行:

// scrollPane.add(contentTable); 

當你把組件內部一個JScrollPane的構造函數,你會提到哪個視圖被應用到的滾動視圖。

另一方面,使用add方法,您只需將組件添加到容器,例如將其添加到JPanel。這樣,您不指定要添加滾動條的組件。

+0

謝謝,它的工作原理!但是這兩種方式有什麼區別? – Songokute

+0

當然。看到我更新的答案。 –

+0

明白了,非常感謝! :D – Songokute