Skip to content

Instantly share code, notes, and snippets.

@rubenquadros
Last active March 27, 2024 17:17
Show Gist options
  • Save rubenquadros/8259f9a02be51cd136fe57a67b272bc8 to your computer and use it in GitHub Desktop.
Save rubenquadros/8259f9a02be51cd136fe57a67b272bc8 to your computer and use it in GitHub Desktop.
Package table model to display in a JTable.
class PackageTableModel(
private val packages: MutableList<String>
) : AbstractTableModel() {
override fun getRowCount(): Int {
return packages.size
}
override fun getColumnCount(): Int {
return 1 //only 1 column
}
override fun getValueAt(rowIndex: Int, columnIndex: Int): Any {
return packages.elementAtOrElse(rowIndex) { "" }
}
override fun getColumnName(column: Int): String {
return "Packages"
}
override fun isCellEditable(rowIndex: Int, columnIndex: Int): Boolean {
return true
}
override fun setValueAt(aValue: Any?, rowIndex: Int, columnIndex: Int) {
packages[rowIndex] = aValue.toString()
}
fun addRow(value: String, rowIndex: Int) {
packages.add(rowIndex, value)
setValueAt(value, rowIndex, 1)
}
fun removeRow(firstRow: Int, lastRow: Int, rowIndex: Int) {
packages.removeAt(rowIndex)
fireTableRowsDeleted(firstRow, lastRow)
}
fun updateRow(rowIndex: Int, value: String) {
setValueAt(value, rowIndex, 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment