⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbtableviewer.java

📁 实习时做的人事系统
💻 JAVA
字号:
package com.personnel;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.print.PrinterException;import java.sql.SQLException;import javax.swing.JButton;import javax.swing.WindowConstants;import javax.swing.table.TableModel;import org.jdesktop.application.Application;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JToolBar;import javax.swing.ScrollPaneConstants;public class DBTableViewer extends javax.swing.JPanel {	private static final long serialVersionUID = 1L;	private JPanel toolBarPanel;	private JTable dbTable;	private JScrollPane jScrollPane;	private JButton printButton;	private JButton deleteButton;	private JButton editButton;	private JButton newButton;	private JToolBar toolBar;	private JPanel contentPanel;	private TableModel model = null;	private TableModel controller = null;	private String tableName = "员工信息";	private JFrame parent = null;	/**	 * Auto-generated main method to display this JPanel inside a new JFrame.	 */	public static void main(String[] args) {		JFrame frame = new JFrame();		frame.getContentPane().add(new DBTableViewer());		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);		frame.pack();		frame.setVisible(true);	}	public DBTableViewer() {		super();		this.initGUI();	}	public String getDBTableName() {		return this.tableName;	}	public boolean closeDB() {		return ((DBTableModel) this.model).closeDB();	}	public DBTableViewer(JFrame parent, TableModel model, String tableName) {		super();		this.parent = parent;		this.model = model;		this.tableName = tableName;		this.initGUI();	}	private void initGUI() {		try {			BorderLayout thisLayout = new BorderLayout();			this.setLayout(thisLayout);			this.setPreferredSize(new java.awt.Dimension(702, 406));			{				this.toolBarPanel = new JPanel();				BorderLayout toolBarPanelLayout = new BorderLayout();				this.toolBarPanel.setLayout(toolBarPanelLayout);				this.add(this.toolBarPanel, BorderLayout.NORTH);				{					this.toolBar = new JToolBar();					this.toolBarPanel.add(this.toolBar, BorderLayout.CENTER);					{						this.newButton = new JButton();						this.toolBar.add(this.newButton);						this.newButton.setName("newButton");						this.newButton.setSize(88, 59);						this.newButton.setPreferredSize(new java.awt.Dimension(								14, 7));						this.newButton.addActionListener(new ActionListener() {							public void actionPerformed(ActionEvent evt) {								String[] columnNames = ((DBTableModel) DBTableViewer.this.model)										.getColumnNames();								NewJDialog dialog = new NewJDialog(										DBTableViewer.this.parent, true,										columnNames);								dialog.setVisible(true);								if (dialog.getResult() == 0) {									String[] val = dialog.getTextFields();									DBTableModel dbModel = (DBTableModel) DBTableViewer.this.model;									dbModel.add(val);									DBTableViewer.this.model = new DBTableModel(											dbModel.getQuery(), "person",											DBTableViewer.this.tableName);									DBTableViewer.this.controller = new DBTableController(											DBTableViewer.this.model);									DBTableViewer.this.dbTable											.setModel(DBTableViewer.this.controller);									DBTableViewer.this.dbTable.validate();								}							}						});					}					{						this.editButton = new JButton();						this.toolBar.add(this.editButton);						this.editButton.setName("editButton");						this.editButton.setVisible(false);					}					{						this.deleteButton = new JButton();						this.toolBar.add(this.deleteButton);						this.deleteButton.setName("deleteButton");						this.deleteButton								.addActionListener(new ActionListener() {									public void actionPerformed(ActionEvent evt) {										int index = DBTableViewer.this.dbTable												.getSelectedRow();										if (index == -1) {											JOptionPane													.showMessageDialog(															DBTableViewer.this,															"请选择一行");											return;										}										String primaryKey = DBTableViewer.this.dbTable												.getValueAt(index, 0)												.toString();										try {											if (((DBTableModel) DBTableViewer.this.model)													.del(primaryKey)) {												JOptionPane.showMessageDialog(														DBTableViewer.this,														"删除成功");												DBTableModel dbModel = (DBTableModel) DBTableViewer.this.model;												DBTableViewer.this.model = new DBTableModel(														dbModel.getQuery(),														"person",														DBTableViewer.this.tableName);												DBTableViewer.this.controller = new DBTableController(														DBTableViewer.this.model);												DBTableViewer.this.dbTable														.setModel(DBTableViewer.this.controller);												DBTableViewer.this.dbTable														.validate();											} else {												JOptionPane.showMessageDialog(														DBTableViewer.this,														"删除失败");											}										} catch (SQLException e) {											e.printStackTrace();											JOptionPane.showMessageDialog(													DBTableViewer.this, "删除失败");										}									}								});					}					{						this.printButton = new JButton();						this.toolBar.add(this.printButton);						this.printButton.setName("printButton");						this.printButton								.addActionListener(new ActionListener() {									public void actionPerformed(ActionEvent evt) {										try {											DBTableViewer.this.dbTable.print();										} catch (PrinterException e) {											e.printStackTrace();										}									}								});					}				}			}			{				this.contentPanel = new JPanel();				BorderLayout contentPanelLayout = new BorderLayout();				this.contentPanel.setLayout(contentPanelLayout);				this.add(this.contentPanel, BorderLayout.CENTER);				{					this.jScrollPane = new JScrollPane();					this.contentPanel							.add(this.jScrollPane, BorderLayout.CENTER);					this.jScrollPane							.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);					{						if (this.model == null) {							this.model = new DBTableModel(									DBQuery.SELECT_ALL_QUERY, "person",									this.tableName);						}						this.controller = new DBTableController(this.model);						this.dbTable = new JTable(this.controller);						this.jScrollPane.setViewportView(this.dbTable);						this.dbTable.setModel(this.controller);						this.dbTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);					}				}			}			Application.getInstance().getContext().getResourceMap(					this.getClass()).injectComponents(this);		} catch (Exception e) {			e.printStackTrace();		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -