1

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

我面临的问题是:

当我将底部表中的所有行添加到顶部表中时,如果我对顶部表中的第 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();}});

删除行

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;

}
4

0 回答 0