包括2个java文件: TableTEST.java 和 MyTableModel.java.
// 1. TableTEST.java ///
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
public class TableTEST extends JFrame {
// 数据源 String tableHeads[] = { “A”, “B”, “C”, “D”, “E” };
String tableContent[][] = { { “abc”, “def”, “ghi”, “jkl” },
{ “pqr”, “stu”, “vwx”, “yza” },
{ “efg”, “hij”, “klm”, “nop” },
{ “tuv”, “wxy”, “zab”, “cde” },
{ “ijk”, “lmn”, “opq”, “rst” } };
JTextField txtboxRow = new JTextField(10);
JTextField txtboxCol = new JTextField(10);
JTextField txtboxContent = new JTextField(10);
JPanel pnlSouth = new JPanel(new GridLayout(1, 6));
MyTableModel tbModel = new MyTableModel();
// 生成模型类对象
public TableTEST() { // 初始化窗体 super(“DO A Test!”);
this.setBounds(50, 50, 500, 400);
// 获取contentPane
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
// 初始化表格数据及表头
tbModel.data = new Vector(1, 1);
for (int i = 0; i < 5; i++)
for (int j = 0; j < 4; j++)
{
tbModel.data.add(tableContent[i][j]);
}
tbModel.titles = new Vector(1, 1);
for (int i = 0; i < 5; i++)
{
tbModel.titles.add(tableHeads[i]);
} // 使用表模型对象生成表
JTable myTable = new JTable(tbModel);
// 窗体组件布局
myTable.setAutoResizeMode(5);
JScrollPane jspCenter = new JScrollPane(myTable);
contentPane.add(jspCenter, BorderLayout.CENTER);
pnlSouth.add(new JLabel(“Row:”), BorderLayout.SOUTH);
pnlSouth.add(txtboxRow, BorderLayout.SOUTH);
pnlSouth.add(new JLabel(“Col:”), BorderLayout.SOUTH);
pnlSouth.add(txtboxCol, BorderLayout.SOUTH);
pnlSouth.add(new JLabel(“Content:”), BorderLayout.SOUTH);
pnlSouth.add(txtboxContent, BorderLayout.SOUTH);
contentPane.add(pnlSouth, BorderLayout.SOUTH);
// 为表格添加监听器
myTable.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
// 实现双击
{
int row = ((JTable) e.getSource()).rowAtPoint(e.getPoint()); // 获得行位置
int col = ((JTable) e.getSource()).columnAtPoint(e
.getPoint()); // 获得列位置
String cellVal = (String) (tbModel.getValueAt(row, col)); // 获得点击单元格数据
txtboxRow.setText((row + 1) + “”);
txtboxCol.setText((col + 1) + “”);
txtboxContent.setText(cellVal);
} else
return;
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent me) {
System.exit(0);
}
});
this.setVisible(true);
}
public static void main(String Str[]) {
TableTEST frm = new TableTEST();
}
}
// 2. MyTableModel.java ///
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
class MyTableModel extends AbstractTableModel {
public Vector data;
public Vector titles;
public int getRowCount() {
return data.size()/getColumnCount();
}
public int getColumnCount() {
return titles.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data.get((rowIndex*getColumnCount())+columnIndex);
}
}
今天的文章java 表格模型 vector,利用AbstractTableModel创建自己的table | 学步园分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/31256.html