扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
jTable一般和DefaultTableModel或者 TableModel配置合使用,
我们提供的服务有:成都网站建设、网站建设、微信公众号开发、网站优化、网站认证、信州ssl等。为成百上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的信州网站制作公司
DefaultTableModel里面就有
(JDK文档)
void addRow(Object[] rowData)添加一行到模型的结尾。
void addRow(Vector rowData)添加一行到模型的结尾。
void insertRow(int row, Object[] rowData)在模型中的 row 位置插入一行。
void insertRow(int row, Vector rowData)在模型中的 row 位置插入一行。
你应该多看jdk的文档,其实很多的功能都己经在里面了
java连接数据库时,往数据库一次添加多条数据,可以在DAO层,使用executeBatch()批量插入数据,如下代码:
conn = DBToolkit.getConnection();
Statement stmt = conn.createStatement();
//连续添加多条SQL
stmt.addBatch("insert into testdb.book (kind, name) values ('java', 'java in aciton')");
stmt.addBatch("insert into testdb.book (kind, name) values ('c', 'c in aciton')");
//执行批量执行
stmt.executeBatch();
jxl 删除功能不怎么好用。之前我做过一个比较大点的excel吧,列能有60+,然后我删除某一列,结果jxl删除的不是我指定的列,而且最多好像只删除了不到10列。然后没办法,只能做隐藏了。增加行,没明白你的意思。
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.util.*;
import java.util.List;
public class StudentManage extends JFrame{
public final static String[] CH_NUM = {"一","二","三","四","五","六","七","八","九","十"};
public final static String ROOT = "班级管理";
public final static int GRADE_NUM = 3;
public final static int CLASS_NUM = 3;
private MapString, ListStudent classes;
public StudentManage() {
setTitle("学生管理系统");
setSize(300, 300);
setLayout(new BorderLayout());
if(classes == null) classes = new HashMap();
DefaultMutableTreeNode top = new DefaultMutableTreeNode(ROOT);
DefaultMutableTreeNode[] gradeNodes = new DefaultMutableTreeNode[GRADE_NUM];
for (int i = 0; i GRADE_NUM; i++) {
gradeNodes[i] = new DefaultMutableTreeNode(CH_NUM[i] + "年级");
for (int j = 0; j CLASS_NUM; j++) {
gradeNodes[i].add(new DefaultMutableTreeNode(CH_NUM[j] + "班"));
}
top.add(gradeNodes[i]);
}
final JTree tree = new JTree(top);
String title[] = {"ID","姓名","性别","年龄","地址"};
final DefaultTableModel model = new DefaultTableModel(title, 0);
JTable table=new JTable(model);
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree .getLastSelectedPathComponent();
if (node == null) return;
Object object = node.getUserObject();
if (node.isLeaf()) {
ListStudent stdList = classes.get(getFullPath(node));
Object[][] data = new Object[stdList.size()][5];
for (int i = 0; i stdList.size(); i++) {
data[i][0] = stdList.get(i).getId();
data[i][1] = stdList.get(i).getName();
data[i][2] = stdList.get(i).getSex();
data[i][3] = stdList.get(i).getAge();
data[i][4] = stdList.get(i).getAddress();
}
while(model.getRowCount()0){
model.removeRow(model.getRowCount()-1);
}
for (int i = 0; i stdList.size(); i++) {
model.addRow(data[i]);
}
model.fireTableDataChanged();
}
}
});
add(tree, BorderLayout.WEST);
add(new JScrollPane(table), BorderLayout.CENTER);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private String getFullPath(DefaultMutableTreeNode defaultMutableTreeNode){
StringBuilder sb = new StringBuilder();
Object[] objs = defaultMutableTreeNode.getUserObjectPath();
for (int i = 0; i objs.length; i++) {
sb.append(objs[i]);
}
return sb.toString().replace(ROOT,"");
}
public void init(){
for (int i = 0; i GRADE_NUM; i++) {
for (int j = 0; j CLASS_NUM; j++) {
int n = new Random().nextInt(30)+1;
ListStudent stdList = new ArrayList(n);
for (int k = 0; k n; k++) {
stdList.add(new Student(k,"学生"+k,"男",
12,“”));
}
classes.put(CH_NUM[i] + "年级"+ CH_NUM[j] +"班", stdList);
}
}
}
public static void main(String[] args) {
StudentManage studentManage = new StudentManage();
studentManage.init();
}
}
class Student {
private int id;
private String name;
private String sex;
private int age;
private String address;
public Student(int id, String name, String sex, int age, String address) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.address = address;
}
public Student() {
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流