Java Swing JTable 的使用
表格的创建 数据增加 删除
官方JavaDocsApi: javax.swing.JTable
package com.mian;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
public class UserManager extends JFrame {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JPanel panel;
private JTable table;
public static void main(String[] args) {
run();
}
public static void run() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UserManager window = new UserManager();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public UserManager() {
initialize();
}
private void initialize() {
frame = new JFrame("Swing Jtable Test");
frame.setBounds(100, 100, 684, 430);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JButton button = new JButton("添加"); // 添加
button.setBounds(510, 352, 113, 27);
panel.add(button);
textField = new JTextField();
textField.setBounds(108, 353, 138, 24);
panel.add(textField);
textField.setColumns(10);
JLabel lblId = new JLabel("ID:");
lblId.setBounds(63, 355, 31, 18);
panel.add(lblId);
JLabel label = new JLabel("密码:");
label.setBounds(294, 358, 38, 18);
panel.add(label);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(339, 353, 138, 24);
panel.add(textField_1);
JButton button_3 = new JButton("删除");
button_3.setBounds(510, 320, 113, 27);
panel.add(button_3);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(14, 13, 638, 294);
panel.add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
// 用户数据
Vector vData = new Vector();
Vector vRow = new Vector();
Vector vName = new Vector();
//设置表格标题
vName.add("登录ID");
vName.add("密码");
vName.add("用户类型");
//设置表格行数据
vRow.clear();
vRow.add(1);
vRow.add("a");
vRow.add("a类");
vData.add(vRow.clone());
//创建表格
refreshTable(vData, vName);
// 删除的点击事件
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
if (row < 0) {
JOptionPane.showMessageDialog(null, "请选择正确的联系人序号!", "通讯录管理系统提示", JOptionPane.ERROR_MESSAGE);
}
int id = Integer.parseInt(table.getValueAt(row, 0).toString()); // 获取第row行第1列的的用户主键id
int col = table.getSelectedRow();
System.out.println(col); // 获取选定行的下标 eg:第一行 0
vData.remove(col);
refreshTable(vData, vName);
}
});
// 添加的点击事件
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String idText = textField.getText();
String pwd = textField_1.getText();
if (idText == null || pwd == null || "".equals(idText) || "".equals(pwd)) {
JOptionPane.showMessageDialog(null, "账号密码不可为空!", "系统提示", JOptionPane.ERROR_MESSAGE);
return;
}
vRow.clear();
vRow.add(idText);
vRow.add(pwd);
vRow.add("a类");
vData.add(vRow.clone());
refreshTable(vData, vName);
}
});
}
protected void refreshTable(Vector vData, Vector vName) {
DefaultTableModel model = new DefaultTableModel(vData, vName) {
public boolean isCellEditable(int rowIndex, int ColIndex) {
return false; // 设置表格元素不可编辑
}
};
// 设置表格内容颜色
table.setForeground(Color.BLACK); // 字体颜色
table.setFont(new Font(null, Font.PLAIN, 14)); // 字体样式
table.setSelectionForeground(Color.DARK_GRAY); // 选中后字体颜色
table.setSelectionBackground(Color.LIGHT_GRAY); // 选中后字体背景
table.setGridColor(Color.GRAY); // 网格颜色
// 设置表头
table.getTableHeader().setFont(new Font(null, Font.BOLD, 14)); // 设置表头名称字体样式
table.getTableHeader().setForeground(Color.RED); // 设置表头名称字体颜色
table.getTableHeader().setResizingAllowed(false); // 设置不允许手动改变列宽
table.getTableHeader().setReorderingAllowed(false); // 设置不允许拖动重新排序各列
// 设置行高
table.setRowHeight(30);
table.setModel(model);
}
}