package com.main.list;
import javax.swing.*;
public class SimpleTable {
public void init(){
JFrame jf = new JFrame("简单操作");
//创建一维数组
Object titles[]={"姓名","年龄","性别"};
//创建二位数组
Object data[][]={
{"李昭庆",29,"女"},
{"苏格拉里",56,"男"},
{"李白",35,"女"},
{"弄头",18,"男"},
{"虎头",2,"女"},
};
JTable jTable = new JTable(data, titles);
jf.add(new JScrollPane(jTable));
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new SimpleTable().init();
}
}
image.png
选择调整
package com.main.list;
import javax.swing.*;
import javax.swing.table.TableColumn;
public class SimpleTable {
public void init(){
JFrame jf = new JFrame("简单操作");
//创建一维数组
Object titles[]={"姓名","年龄","性别"};
//创建二位数组
Object data[][]={
{"李昭庆",29,"女"},
{"苏格拉里",56,"男"},
{"李白",35,"女"},
{"弄头",18,"男"},
{"虎头",2,"女"},
};
JTable jTable = new JTable(data, titles);
//设置选择模式
// jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);//默认没有限制
// jTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);//只能选择单个范围
jTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只选择单行
TableColumn column = jTable.getColumn(titles[0]);
column.setMinWidth(40);
TableColumn column2 = jTable.getColumn(titles[2]);
column2.setMaxWidth(50);
jf.add(new JScrollPane(jTable));
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new SimpleTable().init();
}
}
自定义实现TableModel
package com.main.list;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Vector;
public class SimpleTable {
Vector title = new Vector();//存储标题
Vector <Vector> dataV = new Vector();//存储数据
public void init() {
JFrame jf = new JFrame("简单操作");
//创建一维数组
Object titles[] = {"姓名", "年龄", "性别"};
//创建二位数组
Object data[][] = {
{"李昭庆", 29, "女"},
{"苏格拉里", 56, "男"},
{"李白", 35, "女"},
{"弄头", 18, "男"},
{"虎头", 2, "女"},
};
for (int i = 0; i < titles.length; i++) {
title.add(titles[i]);
}
for (int i = 0; i < data.length; i++) {
Vector td = new Vector();
for (int j = 0; j < data[i].length; j++) {
td.add(data[i][j]);
}
dataV.add(td);
}
System.out.println(dataV);
MyTableModel myTableModel = new MyTableModel();
JTable jTable = new JTable(myTableModel);
JButton selectTd = new JButton("获取选中行数据");
selectTd.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedColumn = jTable.getSelectedColumn();
int selectedRow = jTable.getSelectedRow();
System.out.println("当前选中行"+selectedRow+"当前选中列"+selectedColumn);
Object valueAt = myTableModel.getValueAt(selectedRow, selectedColumn);
System.out.println("当前选中的数据是"+valueAt);
}
});
jf.add(new JScrollPane(jTable));
jf.add(selectTd, BorderLayout.SOUTH);
jf.setDefaultCloseOperation(WindowConstants.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 title.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return dataV.get(rowIndex).get(columnIndex);
}
@Override
public String getColumnName(int column) {
return (String) title.get(column);
}
//是否支持编辑
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}
public static void main(String[] args) {
new SimpleTable().init();
}
}
image.png
DefaultTableModel
package com.main.list;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Vector;
public class SimpleTable {
Vector title = new Vector();//存储标题
Vector <Vector> dataV = new Vector();//存储数据
public void init() {
JFrame jf = new JFrame("简单操作");
//创建一维数组
Object titles[] = {"姓名", "年龄", "性别"};
//创建二位数组
Object data[][] = {
{"李昭庆", 29, "女"},
{"苏格拉里", 56, "男"},
{"李白", 35, "女"},
{"弄头", 18, "男"},
{"虎头", 2, "女"},
};
for (int i = 0; i < titles.length; i++) {
title.add(titles[i]);
}
for (int i = 0; i < data.length; i++) {
Vector td = new Vector();
for (int j = 0; j < data[i].length; j++) {
td.add(data[i][j]);
}
dataV.add(td);
}
System.out.println(dataV);
//通过
DefaultTableModel defaultTableModel = new DefaultTableModel(dataV, title);
JTable jTable = new JTable(defaultTableModel);
JButton addRow = new JButton("add Row");
JButton addColumn = new JButton("add Column");
JButton delete_row = new JButton("delete Row");
addRow.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
defaultTableModel.addRow(new Object[]{"柳岩",18,"女"});
}
});
addColumn.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
defaultTableModel.addColumn("职业");
}
});
delete_row.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = jTable.getSelectedRow();
defaultTableModel.removeRow(selectedRow);
}
});
JPanel jPanel = new JPanel();
jPanel.add(addRow);
jPanel.add(addColumn);
jPanel.add(delete_row);
jf.add(new JScrollPane(jTable));
jf.add(jPanel, BorderLayout.SOUTH);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new SimpleTable().init();
}
}
image.png
网友评论