Best algorithm for copying selected rows of a Swing JTable

113 views Asked by At

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!

1

There are 1 answers

0
Memmo On BEST ANSWER

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