?? dbtableviewer.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 + -