【Java-GUI】10 Swing06 JTable
2021-06-08 15:05
标签:edit obj lazy bst seo 实现 jta edr remove 静态数据表格的样子: 程序效果: 简单的表格提供了一些基础功能: 1、可以选中某一条记录 2、可以对显示的记录的某一个字段进行修改 3、可以对列进行移动 使用TableModel来代替二维数组渲染数据: 可以自定义实现抽象表格模型类,监听获取选中的单元格: Java也提供默认的TableModel可使用,DefaultTableModel 加行,加列,删除行,选中就都有了 【Java-GUI】10 Swing06 JTable 标签:edit obj lazy bst seo 实现 jta edr remove 原文地址:https://www.cnblogs.com/mindzone/p/14514131.htmlpackage cn.dzz;
import javax.swing.*;
import java.awt.*;
public class JTable {
JFrame jFrame = new JFrame("简单表格");
Object[] titleList = {"姓名", "年龄", "性别"};
Object[][] dataList = {
{"李清照", 29, "女性"},
{"苏格拉底", 56, "男性"},
{"李白", 35, "男性"},
{"弄玉", 18, "女性"},
{"虎头", 2, "男性"},
};
public void init() {
javax.swing.JTable jTable = new javax.swing.JTable(dataList, titleList);
jFrame.add(new JScrollPane(jTable));
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setVisible(true);
}
public static void main(String[] args) {
new JTable().init();
}
}
然后是对Table的一些设置API:
package cn.dzz;
import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;
public class JTable {
JFrame jFrame = new JFrame("简单表格");
Object[] titleList = {"姓名", "年龄", "性别"};
Object[][] dataList = {
{"李清照", 29, "女性"},
{"苏格拉底", 56, "男性"},
{"李白", 35, "男性"},
{"弄玉", 18, "女性"},
{"虎头", 2, "男性"},
};
public void init() {
javax.swing.JTable jTable = new javax.swing.JTable(dataList, titleList);
// 默认的选择模式
jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// 设置列宽
TableColumn jTableColumn1 = jTable.getColumn(titleList[0]);
jTableColumn1.setMinWidth(40);
TableColumn jTableColumn3 = jTable.getColumn(titleList[2]);
jTableColumn3.setMaxWidth(50);
jFrame.add(new JScrollPane(jTable));
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setVisible(true);
}
public static void main(String[] args) {
new JTable().init();
}
}
package cn.dzz;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class TableModelDemo {
JFrame jFrame = new JFrame("tableModel");
Object[] titleList = {"姓名", "年龄", "性别"};
Object[][] dataList = {
{"李清照", 29, "女性"},
{"苏格拉底", 56, "男性"},
{"李白", 35, "男性"},
{"弄玉", 18, "女性"},
{"虎头", 2, "男性"},
};
private Vector titleVector = new Vector();
private Vector
package cn.dzz;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class TableModelDemo {
JFrame jFrame = new JFrame("tableModel");
Object[] titleList = {"姓名", "年龄", "性别"};
Object[][] dataList = {
{"李清照", 29, "女性"},
{"苏格拉底", 56, "男性"},
{"李白", 35, "男性"},
{"弄玉", 18, "女性"},
{"虎头", 2, "男性"},
};
private Vector titleVector = new Vector();
private Vector
上一篇:Java数组:初识数组
文章标题:【Java-GUI】10 Swing06 JTable
文章链接:http://soscw.com/index.php/essay/92268.html