2013-06-30 66 views
1

我正在創建一個地址簿,並具有很多功能。但是實際的表格不能被看到。數據在那裏。我可以保存並加載它,但我實際上看不到任何數據。我已經嘗試了很多東西,setContentPane()是一個。我也花時間在Oracle網站上,但無濟於事。我有示例表,顯示錶,並試圖複製一些代碼,但再次無濟於事。Java JTable不可見

public AddressBookGUI() 
{ 
    /* 
    * Initialise 
    */ 

    addresses = new ArrayList<Person>(); 
    Person a = new Person("Kathy", "Smith", "1 mith Sqaoiudh", "Lonodn", "London", "BN76 8QQ", "01980472", "[email protected]"); 
    addresses.add(a); 
    addressBook = new AddressBook(addresses, columnNames); 

// JTable addressTable = new JTable(new TableModel()); 
    //addressTable.setModel(new addressTable.TableModel()); 

    createAddAddressFrame(); 
    createEditAddressFrame(); 
    createDeleteAddressFrame(); 
    createAddressBookFrame(); 
} 

//Create the address book frame 
private void createAddressBookFrame() { 

    //Create frame 
    addressBookFrame = new JFrame("My Address Book"); 

    //Create menu bar 
    mb = new JMenuBar(); 

    //Create file menu 
    fileMenu = new JMenu("File"); 
     anew = new JMenuItem("New"); 
     open = new JMenuItem("Open"); 
     save = new JMenuItem("Save"); 
     saveAs = new JMenuItem("Save As"); 
     print = new JMenuItem("Print"); 
     quit = new JMenuItem("Quit"); 

    //Create edit menu 
    editMenu = new JMenu("Edit"); 
     sort = new JMenu("Sort"); 
      byName = new JMenuItem("By Name"); 
      byPC = new JMenuItem("By Post Code"); 
     editSelectedPerson = new JMenuItem("Edit Selected Person"); 
     find = new JMenuItem("Find"); 
     findAgain = new JMenuItem("Find Again"); 
    //Add menu items to menu bar and menu 
     mb.add(fileMenu); 
     fileMenu.add(anew); 
     fileMenu.add(open); 
     fileMenu.add(save); 
     fileMenu.add(saveAs); 
     fileMenu.addSeparator(); 
     fileMenu.add(print); 
     fileMenu.add(quit); 
    mb.add(editMenu); 
     editMenu.add(editSelectedPerson); 
     editMenu.add(sort); 
      sort.add(byName); 
      sort.add(byPC); 
     editMenu.addSeparator(); 
     editMenu.add(find); 
     editMenu.add(findAgain); 

    //Set menu bar 
    addressBookFrame.setJMenuBar(mb); 

    //Add buttons to bottom of frame 
    buttonPanel = new JPanel(); 
    buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 

    //Add buttons 
    addButton = new JButton("Add"); 
    editButton = new JButton("Edit..."); 
    deleteButton = new JButton("Delete"); 
    buttonPanel.add(addButton); 
    buttonPanel.add(editButton); 
    buttonPanel.add(deleteButton); 

    //Add button panel 
    addressBookFrame.add(buttonPanel, BorderLayout.SOUTH); 

    //Add action Listeners 
    //BUTTONS 
    //Add 
    class addListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      //Set text of JTextFields 
      addAddressFrame.setVisible(true); 
     } 
    } 
    ActionListener addList = new addListener(); 
    addButton.addActionListener(addList); 

    //Edit 
    class editListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      row = myTable.getSelectedRow(); 
      if (row != -1) { 
       //jtf1.setText((String)addressBook.getValueAt(row, 0)); 
       //repeat 
      } 
     } 
    } 
    ActionListener editList = new editListener(); 
    editButton.addActionListener(editList); 

    //Delete 
    class delListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      row = myTable.getSelectedRow(); 
      if (row != -1) { 
       deleteAddressFrame.setVisible(true); 
      } 
     } 
    } 
    ActionListener delList = new delListener(); 
    deleteButton.addActionListener(delList); 


    //MENU ITEMS 
    //New 
    class newListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      addresses.clear(); 
      refreshAddressBook(); 
     } 
    } 
    ActionListener newList = new newListener(); 
    anew.addActionListener(newList); 

    //open 
    class openListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      addresses.clear(); 
      loadFile(); 
     } 
    } 
    ActionListener openList = new openListener(); 
    open.addActionListener(openList); 

    //save 
    class saveListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 

     } 
    } 
    ActionListener saveList = new saveListener(); 
    save.addActionListener(saveList); 

    //save as 
    class saveAsListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      saveToFile(); 
     } 
    } 
    ActionListener saveAsList = new saveAsListener(); 
    saveAs.addActionListener(saveAsList); 

    //print 
    class printListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      printAddresses(); 
     } 
    } 
    ActionListener printList = new printListener(); 
    print.addActionListener(printList); 

    //quit 
    class quitListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      System.exit(0); 
     } 
    } 
    ActionListener quitList = new quitListener(); 
    quit.addActionListener(quitList); 

    //edit selected person 
    class espListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 

     } 
    } 
    ActionListener espList = new espListener(); 
    editSelectedPerson.addActionListener(espList); 

    //sort by name 
    class sbnListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      sortByName(); 
     } 
    } 
    ActionListener sbnList = new sbnListener(); 
    byName.addActionListener(sbnList); 

    //sort by post code 
    class sbpcListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      } 
    } 
    ActionListener sbpcList = new sbpcListener(); 
    byPC.addActionListener(sbpcList); 

    //find 
    class findListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      } 
    } 
    ActionListener findList = new findListener(); 
    find.addActionListener(findList); 

    //find again 
    class findAgainListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      } 
    } 
    ActionListener findAgainList = new findAgainListener(); 
    findAgain.addActionListener(findAgainList); 

    myTable = new JTable(); 
    myTable.setPreferredScrollableViewportSize(new Dimension(500,800)); 
    myTable.setFillsViewportHeight(true); 
    //myTable.setModel(addressBook); 

    JScrollPane scrollPane = new JScrollPane(myTable); 
    scrollPane.setOpaque(true); 
    addressBookFrame.add(scrollPane, BorderLayout.CENTER); 
    //addressBookFrame.add(myTable, BorderLayout.CENTER); 

    //FInally show the address book 
    addressBookFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    addressBookFrame.setTitle("My Address Book"); 
    addressBookFrame.setSize(800, 400); 
    addressBookFrame.setVisible(true); 

} 
+1

可能與您的表模型有關。只是猜測,因爲代碼不起作用。 –

+0

爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

+1

您在代碼中註釋了兩行代碼://myTable.setModel(addressBook);和//addressBookFrame.add(myTable,BorderLayout.CENTER);'爲什麼? –

回答

2

評論,在此聲明,這樣一個TableModel設置爲JTablemyTable

//myTable.setModel(addressBook); 

確保AddressBook確實是一個正常運作的定製TableModel

+0

謝謝你的回覆。我現在取消了評論。我認爲你是對的,這是我的tableModel。我已經嘗試了幾個不同的選項,包括使用defaultTableModel。這個問題的主要部分是數據類型。我有一個名爲Person的類,並將地址存儲在List中。但要在TableModel中使用'length'函數,它必須是一個Object [] []。你有什麼想法來解決這個問題嗎?再次感謝 – user2536409

+0

@ user2536409:作爲替代方案,在你的'TableModel'中存儲'List ';一個相關的例子見[這裏](http://stackoverflow.com/a/9134371/230513)。 – trashgod

2

要顯示在表中的數據應該設置表模型

例如

Object[][] d = {{1,2},{2,1}}; 
Object[] s = {"one", "two"}; 
tableA.setModel(new DefaultTableModel(d, s));