2013-02-09 68 views

回答

5

您將需要合併兩個JTable的TableModel中保存的數據,並將它們添加到由第三個JTable持有的第三個TableModel中。如果表結構完全相同,則可以遍歷模型的行,將每行添加到新的TableModel。

您可以在Swing JTable Tutorial上閱讀JTables和TableModels。

5

您可以創建一個代理模型,允許您爲其添加一個或多個表模型。

然後,您將覆蓋所需的方法,通過代理模型將請求從表中傳遞到其他表。

主要的問題是在模型之間映射行索引。

已更新,例如

enter image description here

這是一個非常簡單的例子...

public class ProxyTableModelTest { 

    public static void main(String[] args) { 
     new ProxyTableModelTest(); 
    } 

    public ProxyTableModelTest() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       final PersonTableModel modelA = new PersonTableModel(); 
       modelA.add(new Person("Guy", "Eichler", getDateOfBirth())); 
       modelA.add(new Person("Jamie", "Weishaar", getDateOfBirth())); 
       modelA.add(new Person("Jamie", "Sinkler", getDateOfBirth())); 
       modelA.add(new Person("Lorrie", "Collelo", getDateOfBirth())); 
       modelA.add(new Person("Hugh", "Rolls", getDateOfBirth())); 
       modelA.add(new Person("Tyrone", "Bogen", getDateOfBirth())); 
       modelA.add(new Person("Jamie", "Vandine", getDateOfBirth())); 
       modelA.add(new Person("Max", "Flippin", getDateOfBirth())); 
       modelA.add(new Person("Christian", "Folson", getDateOfBirth())); 
       modelA.add(new Person("Neil", "Ralphs", getDateOfBirth())); 

       final PersonTableModel modelB = new PersonTableModel(); 
       modelB.add(new Person("Jamie", "Santillanes", getDateOfBirth())); 
       modelB.add(new Person("Carmella", "Leverich", getDateOfBirth())); 
       modelB.add(new Person("Mathew", "Valade", getDateOfBirth())); 
       modelB.add(new Person("Louisa", "Smead", getDateOfBirth())); 
       modelB.add(new Person("Nelson", "Vails", getDateOfBirth())); 
       modelB.add(new Person("Harriett", "Tengan", getDateOfBirth())); 
       modelB.add(new Person("Liza", "Uhler", getDateOfBirth())); 
       modelB.add(new Person("Ashlee", "Forbush", getDateOfBirth())); 
       modelB.add(new Person("Guy", "Kluender", getDateOfBirth())); 
       modelB.add(new Person("Eve", "Cooter", getDateOfBirth())); 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new GridBagLayout()); 

       JPanel controlsA = new JPanel(); 
       JButton addA = new JButton("Add"); 
       addA.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         modelA.add(new Person("A", "A", getDateOfBirth())); 
        } 
       }); 
       controlsA.add(addA); 

       JPanel controlsB = new JPanel(); 
       JButton addB = new JButton("Add"); 
       addB.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         modelB.add(new Person("B", "B", getDateOfBirth())); 
        } 
       }); 
       controlsB.add(addB); 

       JPanel groupA = new JPanel(new BorderLayout()); 
       groupA.add(new JScrollPane(new JTable(modelA))); 
       groupA.add(controlsA, BorderLayout.SOUTH); 

       JPanel groupB = new JPanel(new BorderLayout()); 
       groupB.add(new JScrollPane(new JTable(modelB))); 
       groupB.add(controlsB, BorderLayout.SOUTH); 

       GridBagConstraints gbc = new GridBagConstraints(); 
       gbc.fill = GridBagConstraints.BOTH; 
       gbc.weightx = 1; 
       gbc.weighty = 1; 
       gbc.gridy = 0; 
       gbc.gridx = 0; 
       frame.add(groupA, gbc); 
       gbc.gridx++; 
       frame.add(groupB, gbc); 

       gbc.gridx = 0; 
       gbc.gridy++; 
       gbc.gridwidth = GridBagConstraints.REMAINDER; 
       frame.add(new JScrollPane(new JTable(new ProxyTableModel(modelA, modelB))), gbc); 
       frame.setSize(400, 400); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 

     }); 
    } 

    public class ProxyTableModel extends AbstractTableModel implements TableModelListener { 

     private List<TableModel> models; 
     private int rowCount; 

     public ProxyTableModel() { 
      models = new ArrayList<>(3); 
     } 

     public ProxyTableModel(TableModel... models) { 
      this(); 
      for (TableModel model : models) { 
       add(model); 
      } 
      System.out.println("rowCount = " + rowCount); 
     } 

     protected int getRowOffset(TableModel model) { 
      int rowOffset = 0; 
      for (TableModel proxy : models) { 
       if (proxy.equals(model)) { 
        break; 
       } else { 
        rowOffset += proxy.getRowCount(); 
       } 
      } 
      return rowOffset; 
     } 

     protected TableModel getModelForRow(int row) {    
      TableModel model = null; 
      int rowOffset = 0; 
      for (TableModel proxy : models) { 
       if (row >= rowOffset && row < (rowOffset + proxy.getRowCount())) { 
        model = proxy; 
        break; 
       } 
       rowOffset += proxy.getRowCount(); 
      } 
      return model; 
     } 

     protected void updateRowCount() { 
      rowCount = 0; 
      for (TableModel proxy : models) { 
       rowCount += proxy.getRowCount(); 
      } 
     } 

     public void add(TableModel model) { 
      int firstRow = getRowCount(); 
      int lastRow = firstRow + model.getRowCount() - 1; 

      models.add(model); 
      model.addTableModelListener(this); 
      updateRowCount(); 
      fireTableRowsInserted(firstRow, lastRow); 
     } 

     public void remove(TableModel model) { 
      if (models.contains(model)) { 
       int firstRow = getRowOffset(model); 
       int lastRow = firstRow + model.getRowCount() - 1; 

       model.removeTableModelListener(this); 
       models.remove(model); 
       updateRowCount(); 
       fireTableRowsDeleted(firstRow, lastRow); 
      } 
     } 

     @Override 
     public int getRowCount() { 
      return rowCount; 
     } 

     @Override 
     public int getColumnCount() { 
      int columnCount = 0; 
      if (models.size() > 0) { 
       columnCount = models.get(0).getColumnCount(); 
      } 
      return columnCount; 
     } 

     @Override 
     public Object getValueAt(int rowIndex, int columnIndex) { 
      TableModel model = getModelForRow(rowIndex); 
      if (model == null) { 
       System.out.println("rowIndex = " + rowIndex); 
       System.out.println("rowCount = " + rowCount); 
       TableModel test = getModelForRow(rowIndex); 
      } 
      int rowOffset = getRowOffset(model); 
      rowIndex -= rowOffset; 
      return model.getValueAt(rowIndex, columnIndex); 
     } 

     @Override 
     public Class<?> getColumnClass(int columnIndex) { 
      Class clazz = String.class; 
      if (models.size() > 0) { 
       clazz = models.get(0).getColumnClass(columnIndex); 
      } 
      return clazz; 
     } 

     @Override 
     public String getColumnName(int column) { 
      String name = null; 
      if (models.size() > 0) { 
       name = models.get(0).getColumnName(column); 
      } 
      return name; 
     } 

     @Override 
     public void tableChanged(TableModelEvent e) { 
      int type = e.getType(); 
      if (type == TableModelEvent.INSERT || type == TableModelEvent.DELETE || type == TableModelEvent.UPDATE) {     
       int firstRow = e.getFirstRow(); 
       int lastRow = e.getLastRow(); 

       TableModel model = getModelForRow(firstRow); 
       int rowOffset = getRowOffset(model); 

       firstRow += rowOffset; 
       lastRow += rowOffset; 

       updateRowCount(); 

       TableModelEvent proxy = new TableModelEvent(this, firstRow, lastRow, e.getColumn(), type);   
       fireTableChanged(e); 
      } else { 
       updateRowCount(); 

       TableModelEvent proxy = new TableModelEvent(this, e.getFirstRow(), e.getLastRow(), e.getColumn(), type);     
       fireTableChanged(e); 
      } 
     } 

    } 

    protected Date getDateOfBirth() { 
     Calendar cal = Calendar.getInstance(); 
     cal.set(Calendar.MONTH, random(0, 11)); 
     cal.set(Calendar.DATE, random(1, 31)); 
     cal.set(Calendar.YEAR, random(1900, 2012)); 
     return cal.getTime(); 
    } 

    protected int random(int min, int max) { 

     return min + ((int) Math.round(Math.random() * (max - min))); 

    } 

    public class PersonTableModel extends AbstractTableModel { 

     private List<Person> people; 

     public PersonTableModel() { 
      people = new ArrayList<>(25); 
     } 

     public void add(Person person) { 
      people.add(person); 
      int index = people.size() - 1; 
      fireTableRowsInserted(index, index); 
     } 

     public void remove(Person person) { 
      int index = people.indexOf(person); 
      if (index > -1) { 
       people.remove(index); 
       fireTableRowsDeleted(index, index); 
      } 
     } 

     @Override 
     public int getRowCount() { 
      return people.size(); 
     } 

     @Override 
     public int getColumnCount() { 
      return 3; 
     } 

     @Override 
     public Object getValueAt(int rowIndex, int columnIndex) { 
      Object value = null; 
      Person person = people.get(rowIndex); 
      switch (columnIndex) { 
       case 0: 
        value = person.getFirstName(); 
        break; 
       case 1: 
        value = person.getLastName(); 
        break; 
       case 2: 
        value = person.getDateOfBirth(); 
      } 
      return value; 
     } 

     @Override 
     public String getColumnName(int column) { 
      String value = null; 
      switch (column) { 
       case 0: 
        value = "First name"; 
        break; 
       case 1: 
        value = "Last name"; 
        break; 
       case 2: 
        value = "Date of birth"; 
      } 
      return value; 
     } 

     @Override 
     public Class<?> getColumnClass(int columnIndex) { 
      Class value = null; 
      switch (columnIndex) { 
       case 0: 
       case 1: 
        value = String.class; 
        break; 
       case 2: 
        value = Date.class; 
        break; 
      } 
      return value; 
     } 

    } 

    public class Person { 

     private String firstName; 
     private String lastName; 
     private Date dateOfBirth; 

     public Person(String firstName, String lastName, Date dateOfBirth) { 
      this.firstName = firstName; 
      this.lastName = lastName; 
      this.dateOfBirth = dateOfBirth; 
     } 

     public Date getDateOfBirth() { 
      return dateOfBirth; 
     } 

     public String getFirstName() { 
      return firstName; 
     } 

     public String getLastName() { 
      return lastName; 
     } 

    } 

} 
+0

我不知道這一點非常和善告訴我任何教程 – user2042166 2013-02-09 05:59:46

+0

這是一個聰明的解決方案。 1+ – 2013-02-09 06:04:25

+0

@ user2042166沒有任何教程(我知道)爲這樣的事情,你必須弄清楚yoursef。如果我有時間,我會嘗試做一個例子 – MadProgrammer 2013-02-09 06:07:54

相關問題