英文:
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)] + [
'', '', '', '', '', ''])] + 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...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论