?? tableframe.java~25~
字號:
package jtabledemo;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.table.*;
import java.util.*;
import jtabledemo.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2004</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class TableFrame
extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable table = new JTable();
DefaultTableModel tableModel = new DefaultTableModel();
Vector data = new Vector();
Vector columnNames;
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem menuAdd = new JMenuItem();
JMenuItem menuEdit = new JMenuItem();
JMenuItem menuDelete = new JMenuItem();
JMenuItem menuExit = new JMenuItem();
public TableFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
TableFrame tableframe = new TableFrame();
}
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Demo how to use JTable");
jPanel1.setLayout(borderLayout2);
menuAdd.setText("Add");
menuAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menuAdd_actionPerformed(e);
}
});
menuEdit.setText("Edit");
menuEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menuEdit_actionPerformed(e);
}
});
menuDelete.setText("Delete");
menuDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menuDelete_actionPerformed(e);
}
});
menuExit.setText("Exit");
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(table, null);
popupMenu.add(menuAdd);
popupMenu.add(menuEdit);
popupMenu.add(menuDelete);
popupMenu.addSeparator();
popupMenu.add(menuExit);
table.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
table_mousePressed(e);
}
});
table.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
table_mouseReleased(e);
}
});
initDataSource();
initTableModel();
initTableLook();
// this.setDefaultCloseOperation();
}//jbInit();
protected void proecessWindowEvent(WindowEvent e){
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING){
System.out.println("Printing this line after shut down");
System.exit(0);
}
}
void initDataSource(){
for(int i = 0; i < 10; i++){
Floor floor = new Floor();
floor.setDescription("Description" + i);
floor.setFormula("Formula" + i);
floor.setArea(new Double(i));
Vector v = floor.getDataVector();
data.add(v);
}
//?
columnNames = Floor.getColumnNameVector();
}
void initTableModel(){
tableModel.setDataVector(data,columnNames);
table.setModel(tableModel);
}
void initTableLook(){
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
void showPopupMenu(int x, int y){
popupMenu.show(table,x,y);
}
void table_mouseReleased(MouseEvent e){
if(e.isPopupTrigger()){
showPopupMenu(e.getX(),e.getY());
}
}
void table_mousePressed(MouseEvent e){
if(SwingUtilities.isRightMouseButton(e)){
Point point = e.getPoint();
int row = table.rowAtPoint(point);
int column = table.columnAtPoint(point);
// table.changeSelection(row,column,true,false);
}
}
void menuAdd_actionPerformed(ActionEvent e){
EditDialog dlg = new EditDialog();
dlg.initDialog(EditDialog.NEW_OPERATION,null);
showDialog(dlg);
if(dlg.isOK){
addRecord(dlg.getUpdatedFloor());
table.revalidate();
table.repaint();
}
}
//將Dialog居中顯示
void showDialog(JDialog dlg){
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.setVisible(true);
}
void menuEdit_actionPerformed(ActionEvent e) {
//獲取當前選中的行,然后取出相應的向量對象,轉換成為Floor對象。
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
if (row==1||column==-1) return;
Vector v = (Vector)data.elementAt(row);
Floor floorObj = Floor.parseFloor(v);
EditDialog dlg = new EditDialog();
//在顯示對話框前必須指定當前的操作是哪類操作,例如刪除還是添加還是修改。
dlg.initDialog(EditDialog.EDIT_OPERATION,floorObj);
showDialog(dlg);
if (dlg.isOK){
editRecord(dlg.getUpdatedFloor(),row);
table.revalidate();
table.repaint();
}
}
void menuDelete_actionPerformed(ActionEvent e) {
//獲取當前選中的行,然后取出相應的向量對象,轉換成為Floor對象。
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
if (row==1||column==-1) return;
Vector v = (Vector)data.elementAt(row);
Floor floorObj = Floor.parseFloor(v);
EditDialog dlg = new EditDialog();
//在顯示對話框前必須指定當前的操作是哪類操作,例如刪除還是添加還是修改。
dlg.initDialog(EditDialog.DELETE_OPERATION,floorObj);
showDialog(dlg);
if (dlg.isOK){
deleteRecord(row);
table.revalidate();
table.repaint();
}
}
/**
* Floor對象添加入表格模型內,在實際過程中需要加入對數據庫的存盤
* @param floor - 需要添加入表格模型內的Floor對象
*/
void addRecord(Floor floor){
data.add(floor.getDataVector());
}
/**
* 編輯指定行的模型數據,在現實中除了顯示的數據后臺外,還需要更新數據庫
* @param floor - 需要編輯的Floor對象
* @param index - 指定的需要更改的行索引
*/
void editRecord(Floor floor,int index){
Vector v =floor.getDataVector();
data.setElementAt(v,index);
}
/**
* 刪除數據模型里面指定行,在現實中除了顯示的數據后臺外,還需要更新數據庫
* @param index - 需要刪除的行索引
*/
void deleteRecord(int index){
data.removeElementAt(index);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -