1 import java.util.Vector;
2 import javax.swing.*;
3 import javax.swing.table.*;
4 public class abyssModel extends JScrollPane{
5
6
7 /**
8 *
9 */
10 private static final long serialVersionUID = 1L;
11 private DefaultTableModel tableModel;// 定义表格模型对象
12 private JTable table;// 定义表格对象
13
14 public abyssModel(Vector<String> columnNames, Vector<Vector<String>> tableValues) {
15 // 创建指定表格列名和表格数据的表格模型
16 tableModel = new DefaultTableModel(tableValues, columnNames);
17 table = new JTable(tableModel);// 创建指定表格模型的表格
18 table.setRowSorter(new TableRowSorter<>(tableModel));// 设置表格的排序器
19 // 设置表格的选择模式为单选
20 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
21 //关闭自动调整功能,从而使用水平滚动条
22 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
23 setViewportView(table);
24 }
25 //添加一行数据
26 public void addRow(Vector rowValues){
27 tableModel.addRow(rowValues);
28 }
29 //删除指定行的数据
30 public void removeRow(int selectedRow){
31 tableModel.removeRow(selectedRow);
32 }
33 //获得被选中行
34 public int getSelectedRow(){
35 return table.getSelectedRow();
36 }
37 }