📄 tablebuilderframe.java
字号:
package jdbc_bible;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TableBuilderFrame extends JInternalFrame
{
protected int nRows=15;
protected int nColumns=6;
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)
{
this.setSize(600,400);
this.setLocation(10,10);
this.setClosable(true);
this.setMaximizable(true);
this.setIconifiable(true);
this.setResizable(true);
this.getContentPane().setLayout(new BorderLayout());
this.tableName=tableName;
SQLCommandRoot="CREATE TABLE " +tableName;
setTitle(SQLCommandRoot);
init();
this.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);
this.getContentPane().add(splitter,BorderLayout.CENTER);
this.getContentPane().add(createButton,BorderLayout.SOUTH);
createButton.addActionListener(new ButtonListener());
}
private JTable createTable(int nRows)
{
String[] dataTypes={"CHAR","VARCHAR","INT","FLOAT","DATE"};
String[] defNull={"","NULL","NOT NULL"};
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+",\n";
}
}
if(tableValues.endsWith(",\n"))
{
int tvLen=tableValues.length()-2;
if(tvLen>0)
{
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 + -