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

huangapple go评论81阅读模式
英文:

Best algorithm for copying selected rows of a Swing JTable

问题

我试图在Jython中复制选定的JTable Swing行。复制事件发生在单击时,因此起始点是所选行,最终目标是在它们下面复制它们。

我尝试过,但是我得到了一个非常繁琐的算法,它并不能完全满足我的要求(复制所选行,而不是复制到下面...!)

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:] # 个人拼接
      model.setRowCount(0)
      for nr in newDataVector:
         model.addRow(nr)

我也接受Java中的建议。

提前感谢!

英文:

I am trying to copy the selected lines of a JTable Swing, using Jython. Copy event happens on click, so the starting point are the selected lines, and the final goal is to copy them under them. <br>
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)] + [
         &#39;&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;])] + dataVector[r:] # personal concatenation
      model.setRowCount(0)
      for nr in newDataVector:
         model.addRow(nr)

I accept a suggestion also in Java. <br><br>
Thanks in advance!

答案1

得分: 0

Simplicity is the hardest thing 引用 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:奇怪的是我第一个回答……

英文:

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...

huangapple
  • 本文由 发表于 2020年10月6日 14:17:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64220295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定