目录
前言:
目标:
实现:
导入包:
组件文本准备:
总代码:
关于Swing进行一些创作和样品设计,不过笔者还是更加推荐去网上寻找一些视频教程,这里直接看代码的话会比较的蒙,建议初学者以视频讲解类的内容为主。
下图是使用以下代码实现的成品结构图。
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
创建相关的容器主键,这里文本中有了一定的注释,具体还有那里不懂可以私聊笔者进行询问。
JFrame jf = new JFrame("简单表格");
//创建一维数组.存储标题
Object[] titles = {"姓名","年龄","性别"};
//创建二维数组,存储数据
Object[][] data = {
{"李清照",29,"女"},
{"苏格拉底",56,"男"},
{"李白",35,"男"},
{"弄玉",18,"女"},
{"虎头",2,"男"}
};
private Vector titlesV = new Vector(); //存储标题
private Vector dataV = new Vector<>(); //存储数据
初始化界面:
初始化界面也就是对组件进行一定的封装,将小的组件封装到容器中并实现。
public void init(){
//组装视图
for (int i = 0; i < titles.length; i++) {
titlesV.add(titles[i]);
}
for (int i = 0; i < data.length; i++) {
Vector t = new Vector<>();
for (int j = 0; j < data[i].length; j++) {
t.add(data[i][j]);
}
dataV.add(t);
}
MyTableModel myTableModel = new MyTableModel();
//创建JTable对象
// JTable jTable = new JTable(dataV,titlesV);
JTable jTable = new JTable(myTableModel);
jf.add(new JScrollPane(jTable));
JButton btn = new JButton("获取选中行的数据");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取选中的列号
int selectedColumn = jTable.getSelectedColumn();
//获取选中的行号
int selectedRow = jTable.getSelectedRow();
System.out.println("当前选中行的索引:"+selectedRow);
System.out.println("当前选中列的索引:"+selectedColumn);
Object valueAt = myTableModel.getValueAt(selectedRow, selectedColumn);
System.out.println("当前选中行第一个单元格的内容为:"+valueAt);
}
});
jf.add(btn, BorderLayout.SOUTH);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
private class MyTableModel extends AbstractTableModel{
@Override
public int getRowCount() {
return dataV.size();
}
@Override
public int getColumnCount() {
return titlesV.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return dataV.get(rowIndex).get(columnIndex);
}
@Override
public String getColumnName(int column) {
return (String) titlesV.get(column);
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
}
接下来就是完整的代码实现了
package com.swing12;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
/**
* 自定义TableModel
*/
public class Demo122 {
JFrame jf = new JFrame("简单表格");
//创建一维数组.存储标题
Object[] titles = {"姓名","年龄","性别"};
//创建二维数组,存储数据
Object[][] data = {
{"李清照",29,"女"},
{"苏格拉底",56,"男"},
{"李白",35,"男"},
{"弄玉",18,"女"},
{"虎头",2,"男"}
};
private Vector titlesV = new Vector(); //存储标题
private Vector dataV = new Vector<>(); //存储数据
public void init(){
//组装视图
for (int i = 0; i < titles.length; i++) {
titlesV.add(titles[i]);
}
for (int i = 0; i < data.length; i++) {
Vector t = new Vector<>();
for (int j = 0; j < data[i].length; j++) {
t.add(data[i][j]);
}
dataV.add(t);
}
MyTableModel myTableModel = new MyTableModel();
//创建JTable对象
// JTable jTable = new JTable(dataV,titlesV);
JTable jTable = new JTable(myTableModel);
jf.add(new JScrollPane(jTable));
JButton btn = new JButton("获取选中行的数据");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取选中的列号
int selectedColumn = jTable.getSelectedColumn();
//获取选中的行号
int selectedRow = jTable.getSelectedRow();
System.out.println("当前选中行的索引:"+selectedRow);
System.out.println("当前选中列的索引:"+selectedColumn);
Object valueAt = myTableModel.getValueAt(selectedRow, selectedColumn);
System.out.println("当前选中行第一个单元格的内容为:"+valueAt);
}
});
jf.add(btn, BorderLayout.SOUTH);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
private class MyTableModel extends AbstractTableModel{
@Override
public int getRowCount() {
return dataV.size();
}
@Override
public int getColumnCount() {
return titlesV.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return dataV.get(rowIndex).get(columnIndex);
}
@Override
public String getColumnName(int column) {
return (String) titlesV.get(column);
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
}
public static void main(String[] args) {
new Demo122().init();
}
}