简体   繁体   English

复制 Swing JTable 选定行的最佳算法

[英]Best algorithm for copying selected rows of a Swing JTable

I am trying to copy the selected lines of a JTable Swing, using Jython.我正在尝试使用 Jython 复制 JTable Swing 的选定行。 Copy event happens on click, so the starting point are the selected lines, and the final goal is to copy them under them.复制事件发生在单击时,因此起点是选定的线,最终目标是将它们复制到它们之下。
I tried, but I came up with an "extremely" onerous algorithm that does not do exactly what I want (copy over the selected ones, not below...!)我试过了,但我想出了一个“非常”繁琐的算法,它不能完全按照我的要求做(复制所选的算法,而不是下面的......!)

def copySelectedLine(self, e):
   model = self.table.getModel()
   dataVector = model.getDataVector()
   rowsToCopy = self.table.getSelectedRows()
   for adder, r in enumerate(rowsToCopy):
      r = r+adder
      newDataVector = dataVector[:r] + [([model.getValueAt(r, c) for c in xrange(3)] + [
         '', '', '', '', '', ''])] + dataVector[r:] # personal concatenation
      model.setRowCount(0)
      for nr in newDataVector:
         model.addRow(nr)

I accept a suggestion also in Java.我也接受 Java 的建议。

Thanks in advance!提前致谢!

Simplicity is the hardest thing cit.简单是最难引用的东西 Massimiliano Allegri马西米利亚诺·阿莱格里

def copySelectedLine(self, e):
   model = self.table.getModel()
   rowsToCopy = self.table.getSelectedRows()
   for adder, r in enumerate(rowsToCopy):
      i = r+adder+1
      model.insertRow(i, [model.getValueAt(r, c) for c in xrange(3)])

PS: Strange that I was the first to answer... PS:奇怪我是第一个回答的。。。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM