2015-06-13 41 views
0

我的JTable有一个问题:它看起来像emty,没有从Vector传递给它的对象中获取任何数据。我检查了相关的主题,但遗憾的是我没有找到任何理由解决这个问题,几乎相同的代码实际上为我工作过一次。 为了进一步测试,我在按钮上单击创建对象,将它们添加到我的Vector中,然后尝试刷新表格。 Printlns显示实际上将对象添加到Vector中,但表格仍为空。我认为,问题必须在表格代码中的某处。 我发布我的测试班和相关的,希望你能帮助我。JTable没有从Vector获取数据

测试类:

public class TEST_Table extends JFrame { 

     private PPControl ppc; 
     private EntryFilePM epm; 

     private JTable entryTable; 
     private JPanel entryTablePanel; 

     private JButton addButton; 
     private JButton saveButton; 
     private JButton loadButton; 
     private JPanel buttonsPanel; 

     private JLabel kursl; 
     private JTextField kurst; 

     private JLabel cnumberl; 
     private JTextField cnumbert; 

     private JLabel namel; 
     private JTextField namet; 

     private JLabel datel; 
     private JTextField datet; 

     private JLabel addressl; 
     private JTextField addresst; 

     private JLabel pdatestl; 
     private JTextField pdatestt; 

     private JLabel pdateendl; 
     private JTextField pdateendt; 

     private JLabel roadl; 
     private JTextField roadt; 

     private JLabel studyfl; 
     private JTextField studyft; 

     private JLabel naprl; 
     private JTextField naprt; 

     private JLabel ratingl; 
     private JTextField ratingt; 

     private JLabel averagel; 
     private JTextField averaget; 

     private JLabel profl; 
     private JTextField proft; 

     private JLabel pbasel; 
     private JTextField pbaset; 

     private JLabel homel; 
     private JTextField homet; 

     private JPanel horriblePanel; 

     private JButton saveEntryButton; 
     private JButton cancelEntryButton; 

     private JPanel entryButtonsPanel; 

     private JPanel entryEditPanel; 

     int idCounter = 1; 


     public static void main(String[] args) throws EntryExistsException { 
      try { 
       new TEST_Table(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     public int getIdCounterNext(){ 
      return idCounter++; 
     } 

     public TEST_Table() throws IOException, EntryExistsException { 
      super("TEST"); 
      ppc = new PPControl(); 
      initialize(); 
     } 

     private void initialize() { 
      setSize(1200,800); 
      setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
      setLayout(new BorderLayout()); 
      createEntryTable(); 
      createButtonsPanel(); 
      createEntryTab(); 
      add(entryTablePanel,BorderLayout.CENTER); 
      add(entryEditPanel, BorderLayout.WEST); 
      add(buttonsPanel, BorderLayout.SOUTH); 
      this.setVisible(true); 
      this.pack(); 

     } 

     private void createEntryTable() { 
      final Vector<String> columns = new Vector<String>(); 
      columns.add("Std"); 
      columns.add("Contr #"); 
      columns.add("Name"); 

      columns.add("Birth dt"); 
      columns.add("Address"); 
      columns.add("Pr date start"); 

      columns.add("Pr date end"); 
      columns.add("Road"); 
      columns.add("Std form"); 

      columns.add("№ way"); 
      columns.add("Rating"); 
      columns.add("Avrg"); 

      columns.add("Prof"); 
      columns.add("Pr base"); 
      columns.add("Home"); 

      final EntryTableModel entryModel = new EntryTableModel(ppc.giveAllEntry(), columns); 
      entryTable = new JTable(); 
      entryTable.setModel(entryModel); 
      entryTable.setShowGrid(true); 
    //  entryTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    //  entryTable.getSelectionModel().addListSelectionListener(new TableSelectionListener()); 
      JScrollPane entryScrollPane = new JScrollPane(entryTable); 
      entryTablePanel = new JPanel(); 
      entryTablePanel.setLayout(new GridLayout()); 
      entryTablePanel.add(entryScrollPane, BorderLayout.CENTER); 
      entryTablePanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10)); 
     } 

     private void createButtonsPanel() { 
      addButton = new JButton("Add"); 
      saveButton = new JButton("Save"); 
      saveButton.addActionListener(new ButtonsListener()); 
      loadButton = new JButton("Load"); 
      loadButton.addActionListener(new ButtonsListener()); 

      buttonsPanel = new JPanel(); 
      buttonsPanel.setLayout(new GridLayout(1, 3)); 
      buttonsPanel.add(addButton); 
      buttonsPanel.add(saveButton); 
      buttonsPanel.add(loadButton); 

     } 

     private void createEntryTab(){ 
      JLabel kursl = new JLabel ("kurs"); 
      kurst = new JTextField(); 

//otherlabels/tfields 

      JLabel ratingl = new JLabel("rating.double"); 
      ratingt = new JTextField(); 

      JLabel averagel = new JLabel("average.double"); 
      averaget = new JTextField(); 

      JLabel profl = new JLabel("profession"); 
      proft = new JTextField(); 

      JLabel pbasel = new JLabel("prak.base"); 
      pbaset = new JTextField(); 

      JLabel homel = new JLabel("home"); 
      homet = new JTextField(); 

      horriblePanel = new JPanel(); 
      JScrollPane horribleScrollPane = new JScrollPane(horriblePanel); 
      horriblePanel.setLayout(new GridLayout(15,2)); 
    //  horriblePanel.add(horribleScrollPane, BorderLayout.CENTER); 
      horriblePanel.add(kursl); 
      horriblePanel.add(kurst); 

//other labels/tfields 
      horriblePanel.add(homel); 
      horriblePanel.add(homet); 

      saveEntryButton = new JButton("Save"); 
      saveEntryButton.addActionListener(new ButtonsListener()); 
      cancelEntryButton = new JButton("Cancel"); 
      entryButtonsPanel = new JPanel(); 
      entryButtonsPanel.setLayout(new GridLayout()); 
      entryButtonsPanel.add(saveEntryButton); 
      entryButtonsPanel.add(cancelEntryButton); 

      entryEditPanel = new JPanel(); 
      entryEditPanel.setLayout(new GridLayout()); 
      entryEditPanel.add(horriblePanel); 
      entryEditPanel.add(entryButtonsPanel); 

     } 

     class TableSelectionListener implements ListSelectionListener { 

      @Override 
      public void valueChanged(ListSelectionEvent e) { 
       int rowIndex = -1; 
       if (e.getSource().equals(entryTable.getSelectionModel()) 
         && entryTable.getSelectedRow() != -1) { 
        rowIndex = entryTable.convertRowIndexToModel(entryTable 
          .getSelectedRow()); 
        EntryTableModel em = (EntryTableModel) entryTable.getModel(); 
        Entry entry = em.giveEntryInRow(rowIndex); 
       } 
      } 
     } 

     class ButtonsListener implements ActionListener{ 
      public void actionPerformed(final ActionEvent ae) { 
       if(ae.getSource().equals(addButton)){ 

       } 
       if (ae.getSource().equals(saveButton)){ 
        try { 
         epm.exportTable(entryTable); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
       if(ae.getSource().equals(loadButton)){ 
        try { 
         epm.importTable("C:/test.xls"); 
        } catch (BiffException | IOException | ParseException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 

       if (ae.getSource().equals(saveEntryButton)) { 
        Calendar c = new GregorianCalendar(); 
        int studyYear = 23;// Integer.parseInt(kurst.getText()); 
        int contractNumber = getIdCounterNext(); 
        contractNumber++;// Integer.parseInt(cnumbert.getText()); 
        String stname = "Uano";// namet.getText(); 
        Date birthDate = c.getTime(); 
        // birthDate = epm.convertToDate(datet.getText()); 

        String address = "abc asd fg 123";// addresst.getText(); 
        Date practiceDateStart = c.getTime(); 
        // practiceDateStart = epm.convertToDate(pdatestt.getText()); 

        Date practiceDateEnd = c.getTime(); 
        // practiceDateEnd = epm.convertToDate(pdateendt.getText()); 
        String railway = "bad road";// roadt.getText(); 
        String studyForm = "bad form";// studyft.getText(); 
        int directionNumber = 1232; // = 
               // Integer.parseInt(naprt.getText()); 
        double rating = 3.4;// Double.parseDouble(ratingt.getText()); 
        double average = 4.5;// Double.parseDouble(averaget.getText()); 
        String profession = "fcki";// proft.getText(); 
        String practiceBase = "fckh";// pbaset.getText(); 
        String inhabitation = "fckf2";// homet.getText(); 
        try { 
         ppc.addEntry(studyYear, contractNumber, stname, birthDate, 
           address, practiceDateStart, practiceDateEnd, railway, 
           studyForm, directionNumber, rating, average, 
           profession, practiceBase, inhabitation); 

         for (Entry e : ppc.giveAllEntry()) { 
         System.out.println(e.getContractNumber()); 
         System.out.println(e.getName()); 
         System.out.println(e.getBirthDate()); 
        } 
        final EntryTableModel em = (EntryTableModel) entryTable.getModel(); 
        System.out.println("a"); 
        em.setDataVector(ppc.giveAllEntry()); 
        entryTable.updateUI(); 
         System.out.println("b"); 

        } catch (EntryExistsException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 

         } 
       } 

      } 
     } 

为TableModel类:

​​

,让我的对象的功能载体

public class EntryControl { 
    private Vector <Entry> entryList = new Vector <Entry>(); 
    public void addEntry(Entry e) throws EntryExistsException{ 
     if (entryList.contains(e)) 
      throw new EntryExistsException(e, ""); 
     else 
      entryList.add(e); 
    } 
    public Vector<Entry> listAllEntry(){ 
     return entryList; 
    } 
//reference in main control class 
    public Vector<Entry> giveAllEntry(){ 
     return entryControl.listAllEntry(); 
    } 

输出: http://i57.tinypic.com/125geac.jpg

回答

1

在您的setDataVector方法中,您没有向rows添加任何内容。你可能想加入这一行:

rows.add(entryAsVector); 

你可以自己已经发现这个问题,如果你已经检查了rows,当你把它传递给this.setDataVector值,通过添加一个print语句或通过使用调试器。

+0

一如往常,一个愚蠢的错误。谢谢! – reick

1

您的TableModel类有点精神分裂,因为它扩展了DefaultTableModel,一个已经拥有自己的数据核的类,尽管如此,它仍然拥有自己的数据核entryList,忽略了DefaultTableModel父类的数据核。您需要执行以下两项操作之一:使用entryList字段并扩展AbstractTableModel,或者删除它,并确保将数据传递给父级DefaultTableModel的超级构造函数调用。