简体   繁体   中英

Best algorithm for copying selected rows of a Swing JTable

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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