如何在更新多个jtables的数据时解决这个逻辑错误?

时间:2015-10-09 21:32:54

标签: java swing tablemodel

背景

我正在尝试同时更新多个jtables,如下图所示。每个jtable都有一个模型,它附加到TableModelListener

每当我在特定jtable的单元格中设置值时,我需要移除TableModelListener,设置值,然后重新添加TableModelListener,否则我得到一个如果我不按照这个顺序错误。此序列似乎在一个jtable

上成功运行

但是,当我根据此序列更新多个jtables中的数据时,我将面临逻辑错误。

逻辑错误:

当我根据上传的图片添加第一行jtable中的3行时,第一行将添加到第一行jtable中,但后两行将添加到最后一行jtables `的JTable。

问题:

如何更新多个TableModelListeners?中的所有行,并且这些表包含模型并附加到fireTableDataChanged();请不要告诉我使用TableModelListener方法,因为它只会触发updateJTables 1}}并触发updateJTables方法。

enter image description here

注释:

  

当我评论(擦除)方法TableModelListener并运行以下示例时,   在激活的选项卡中添加行将是正确的,换句话说,   问题将消失。因此,我假设的问题正在发生   当我删除并重新添加updateJTables方法时   import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JViewport; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.DefaultTableModel; public class JT { private static javax.swing.JTabbedPane tabPane; private javax.swing.JButton addRow; private javax.swing.JButton addTable; private javax.swing.JPanel topPanel; private static DefaultTableModel modelItemTable; private static javax.swing.JTable itemTable; private static TableModelListener itemTableListener; public JT(){ BorderLayout bl=new BorderLayout(); JFrame f=new JFrame("example of the problem"); f.setVisible(true); f.setSize(new Dimension(800,600)); f.setLayout(bl); topPanel=new javax.swing.JPanel(); tabPane=new javax.swing.JTabbedPane(); addRow=new javax.swing.JButton("Add Row"); addTable=new javax.swing.JButton("Add Table"); topPanel.setLayout(new FlowLayout()); topPanel.add(addRow); topPanel.add(addTable); addRow.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { if(modelItemTable!=null){ defineItemModel(); for(int i=1; i<4; i++){ modelItemTable.addRow(new Object[]{ "Hello World"+i, //name (Math.random()*10) //Cost } ); } } } }); addTable.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { addTab(); } }); itemTableListener= new TableModelListener(){ @Override public void tableChanged(TableModelEvent e) { if (e.getType() == TableModelEvent.UPDATE || e.getType() == TableModelEvent.INSERT) { updateJTables(); } } }; f.add(topPanel,BorderLayout.NORTH); f.add(tabPane,BorderLayout.CENTER); }//end of constructor public static javax.swing.JTable buildItemTable(){ itemTable= new javax.swing.JTable(); modelItemTable=new DefaultTableModel( new Object [][] {}, new String [] { "name", "Cost" } ) { Class[] types = new Class [] { java.lang.Object.class, //name java.lang.Double.class //Cost }; boolean[] canEdit = new boolean [] { true, //name false //Cost }; public Class getColumnClass(int columnIndex) { return types [columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit [columnIndex]; } }; modelItemTable.addTableModelListener(itemTableListener); itemTable.setModel(modelItemTable); return itemTable; }//end of buildItemTable public static List<javax.swing.JTable> defineMultipleItemJTables(){ Component[] components= tabPane.getComponents(); List<javax.swing.JTable> jTableList=new ArrayList<javax.swing.JTable>(); JScrollPane scrollPane; JViewport viewport; for(int i=0; i<components.length; i++){ if(components[i] instanceof JScrollPane){ scrollPane=(JScrollPane) components[i]; viewport = scrollPane.getViewport(); jTableList.add((javax.swing.JTable)viewport.getView()); } } return jTableList; }//end of defineMultipleItemModel public static void defineItemModel(){ JScrollPane scrollPane=(JScrollPane) tabPane.getSelectedComponent(); if(scrollPane!=null){ JViewport viewport = scrollPane.getViewport(); javax.swing.JTable selectedJTable = (javax.swing.JTable)viewport.getView(); modelItemTable=(DefaultTableModel)selectedJTable.getModel() ; itemTable=selectedJTable; } }//end of defineItemModel public void updateJTables(){ List<javax.swing.JTable> jTables=defineMultipleItemJTables(); if(jTables.size()>0){ for(int i=0; i<jTables.size(); i++){ modelItemTable=(DefaultTableModel) jTables.get(i).getModel(); itemTable=jTables.get(i); if(itemTable.getRowCount()>0){ modelItemTable.removeTableModelListener(itemTableListener); for(int c=0; c<jTables.get(i).getRowCount(); c++){ // calculate(c); } modelItemTable.addTableModelListener(itemTableListener); } } } }//end of updateJTables public static void addTab(){ tabPane.addTab("New_Tab",new javax.swing.JScrollPane(buildItemTable())); ChangeListener tabPaneListener=new ChangeListener(){ @Override public void stateChanged(ChangeEvent e) { modelItemTable.fireTableDataChanged(); } }; tabPane.addChangeListener(tabPaneListener); }//end of addTab public static void main(String[] args){ JT j=new JT(); }//end of main }//end of class JT 。这个问题的类型是什么?我真的很想   了解正在发生的事情。

示例代码(简短示例,已修改):

var serials = [
    { number: "DFG09008", qty: 1 }, 
    { number: "DFG09009", qty: 1 },
    { number: "DFG09016", qty: 1 },
    { number: "DFG09010", qty: 1 },
    { number: "DFG09011", qty: 1 },
    { number: "DFG09005", qty: 1 }, 
    { number: "DFG09014", qty: 1 },
    { number: "DFG09015", qty: 1 },
    { number: "DFG09020", qty: 1 },
    { number: "ASD-0001", qty: 1 },
    { number: "ASD-0002", qty: 1 },
    { number: "HJ-DRT-06", qty: 1 },
    { number: "HJ-DRT-07", qty: 1 },
    { number: "POU055054", qty: 1 }
];

var splitSerials = _.map(serials, function(serial){
  var parts = serial.number.match(/([^\d]+)(\d+)/);
  return {key: parts[1], number: parts[2]};
});

var grouped = _.groupBy(splitSerials, 'key');

function chunkConsec(array){
  return _.transform(array, function(result, n){
    if(_.last(_.last(result)) === n - 1){
      _.last(result).push(n);
    } else {
      result.push([n]);
    }
    return true;
  });
}

function unchunk(array){
  if(array.length > 1){
    return [_.first(array), _.last(array)];
  } else {
    return array;
  }
}

var groupedArray = _.mapValues(grouped,
  _.flow(
    _.partial(_.map, _, 'number'),
    _.partial(_.map, _, _.parseInt),
    _.sortBy,
    chunkConsec,
    _.partial(_.map, _, unchunk)
  ));

function buildRange(value, key){
  var result = {beginSerial: (key + value[0])};
  result.endSerial = (value.length > 1 ? key + value[1] : '');
  return result;
}
function buildRanges(values, key){
  return _.map(values, function(value){
    return buildRange(value, key);
  });
}

var finalResult = _.flatten(_.values(_.mapValues(groupedArray, buildRanges)));
console.log(finalResult);
alert(JSON.stringify(finalResult));

如果上面的代码仍然很大,请告诉我。虽然在这个修改之后,很难使上面的例子更短,因为我将无法显示或定义问题。如果您有任何意见或问题,请询问。感谢

0 个答案:

没有答案