添加和删​​除一个表到另一个表

时间:2013-05-04 15:11:34

标签: java swing jtable

我面临的问题是从一个表视图中删除行的功能从另一个表视图到另一个表视图当前我有两个按钮一个添加行形成底部表到上面和下一个添加从上面的钮。从下到上添加的按钮称为添加,从顶部到底部添加的按钮一旦被复制到其中,就会从原始表中删除。

我面临的问题是:

当我将底部表格中的所有行添加到顶部表格时 如果我在顶部表中排序第0列,然后尝试从顶部表到底部表中删除行 尽管将行添加到底部表格中,但该行不会从顶部表中删除。

你能帮助我解决这个问题或者以更好的方式给我建议吗?

这是我目前的代码:

删除按钮:

btnRemove.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
    spTable.removeAll();
    x = resultTable.getSelectedRows();
    String[] cName= {"Column 0","Column 1","Column 2","Column 3","Column 4","Column 5","Column 6","Column 7","Column 8"};
    if(rTable.getRowCount()==0){                
       model=addToNew(resultTable,x,cName,model);
    }else{ model =addToExisting(resultTable,rTable, x, model);
                 }  
    deletRows(x,resultTable);
    rTable.setModel(model);
    JScrollPane spS = new JScrollPane(rTable);
    rTable.getColumnModel().getColumn(1).setPreferredWidth(290);
    spS.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    spS.setPreferredSize(new Dimension(800,200));
    rTable.setFillsViewportHeight(true);
    spTable.add(spS);
            validate();
    }});

添加按钮:

 btnAdd.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
    panel.removeAll();
    x = rTable.getSelectedRows();
    String[] cName= {"Column 0","Column 1","Column 2","Column 3","Column 4","Column 5","Column 6","Column 7","Column 8"};
    if(resultTable.getRowCount()==0){
                model=addToNew(rTable,x,cName,model);
    }else{ model =addToExisting(rTable,resultTable, x, model);
        }   
    deletRows(x,rTable);
    resultTable.setModel(model);
    JScrollPane spS = new JScrollPane(resultTable);
    resultTable.getColumnModel().getColumn(1).setPreferredWidth(290);
    spS.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    spS.setPreferredSize(new Dimension(800,170));
    panel.add(spS);
    resultTable.setFillsViewportHeight(true);
    validate();}});

deletRow

public void deletRows(int[] selected, JTable t){
 for(int i = selected.length-1;i>=0;i--){
   int y = selected[i];
    ((DefaultTableModel)t.getModel()).removeRow(y);
    validate();}}

/ **  *此方法允许向新表添加行  * /

       public DefaultTableModel addToNew(JTable t1,int[] selected,String[] ColName, DefaultTableModel m){
String[] name = ColName;
int col = name.length;  
m =new DefaultTableModel();
for(int i= 0;i<col;i++){
 m.addColumn(name[i]);
        }
Object[] data= new Object[col];
    for(int i =0; i<selected.length;i++){
        int y= selected[i];

        for (int z =0; z<col;z++){  

                        if( t1.getValueAt(y, z)!= null){
                            String value = t1.getValueAt(y, z).toString();
                            data[z]= value;
      }else{
                data[z]=null;
            }
        }
        m.addRow(data);
        }

    return m;

}

/ * 此方法允许将行添加到现有表* /

public DefaultTableModel addToExisting(JTable t1,JTable t2, int[] selected, DefaultTableModel m){


     m =(DefaultTableModel)t2.getModel();
    Object[] data= new Object[m.getColumnCount()];

    for(int i =0; i<selected.length;i++){
    int y= selected[i];
    for (int z =0; z<m.getColumnCount();z++){  
                    if( t1.getValueAt(y, z)!= null){
                        String value = t1.getValueAt(y, z).toString();
                        data[z]= value;                         
       }else{
        data[z]=null;
       }
    }

    m.addRow(data);
}

    return m;

}

0 个答案:

没有答案