📄 tablebuilderframe.java
字号:
/*
* TableBuilderFrame.java
*
* Created on 2007年11月14日, 上午11:44
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package jdbcjframe;
/**
*
* @author user
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TableBuilderFrame extends JInternalFrame{
/** Creates a new instance of TableBuilderFrame */
protected int nRows=15;
protected int NColumns;
protected JTable table;
protected JTextArea SQLPane=new JTextArea();
protected JButton createButton=new JButton("Create Table");
protected ActionListener commandListener=null;
protected String tableName=null;
protected String SQLCommand="";
protected String SQLCommandRoot="";
public TableBuilderFrame(String tableName) {
setSize(600,400);
setLocation(10,10);
setClosable(true);
setMaximizable(true);
setIconifiable(true);
setResizable(true);
getContentPane().setLayout(new BorderLayout());
this.tableName=tableName;
SQLCommandRoot="CREATE TABLE "+tableName;
setTitle(SQLCommandRoot);
init();
setVisible(true);
}
private void init(){
table=createTable(nRows);
TableChangeListener modelListener=new TableChangeListener();
table.getModel().addTableModelListener(modelListener);
JScrollPane sqlScroller=new JScrollPane(SQLPane);
JScrollPane tableScroller=new JScrollPane(table);
JSplitPane splitter=new JSplitPane(JSplitPane.VERTICAL_SPLIT,sqlScroller,tableScroller);
splitter.setDividerLocation(100);
getContentPane().add(splitter,BorderLayout.CENTER);
getContentPane().add(createButton,BorderLayout.SOUTH);
createButton.addActionListener(new ButtonListener());
}
private JTable createTable(int nRows){
String[]dataTypes={"CHAR","VARCHAR","INT","FLOAT","DATE"};
String[]defNULL={"","NULL","NOTNULL"};
String[]defUnique={"","UNIQUE"};
String[]defPriKey={"","PRIMARY KEY"};
String[]colNames={"Name","DataType","SIZE","NULL","UNIQUE","PRIMARY KEY"};
String[][] rowData=new String[nRows][colNames.length];
for(int i=0;i<nRows;i++)
for(int j=0;j<colNames.length;j++){
rowData[i][j]="";
}
JComboBox dTypes=new JComboBox(dataTypes);
JComboBox nullDefs=new JComboBox(defNULL);
JComboBox uniqueDefs=new JComboBox(defUnique);
JComboBox primaryKDefs=new JComboBox(defPriKey);
JTable table=new JTable(rowData,colNames);
table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(dTypes));
table.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(nullDefs));
table.getColumnModel().getColumn(4).setCellEditor(new DefaultCellEditor(uniqueDefs));
table.getColumnModel().getColumn(5).setCellEditor(new DefaultCellEditor(primaryKDefs));
return table;
}
public String parseTable(){
String tableValues="";
int rows=table.getRowCount();
int cols=table.getColumnCount();
if(rows>=0&&cols>=0){
tableValues+="\n(";
for(int i=0;i<rows;i++){
String rowData="";
for(int j=0;j<cols;j++){
String field=(String)table.getValueAt(i,j);
if(field!=null){
if(field.length()==0)break;
if(j==2)rowData+="(";
else if(i>0||j>0)rowData+=" ";
rowData +=field;
if(j==2)rowData+=")";
}
}
if(rowData.length()==0)break;
tableValues+=rowData+")";
}
}
if(tableValues.endsWith(",\n")){
int tvLen=tableValues.length()-2;
if(tvLen>0)tableValues=tableValues.substring(0,tvLen);
}
tableValues+=");";
return tableValues;
}
public void setCommandListener(ActionListener commandListener){
this.commandListener=commandListener;
}
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
String action=event.getActionCommand();
if(commandListener!=null){
ActionEvent evt=new ActionEvent(this,0,SQLCommand);
commandListener.actionPerformed(evt);
}
}
}
class TableChangeListener implements TableModelListener{
public TableChangeListener(){
}
public void tableChanged(TableModelEvent event){
SQLCommand=SQLCommandRoot+parseTable();
SQLPane.setText(SQLCommand);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -