基本信息
源码名称:数据挖掘:数据预处理及Apriori算法
源码大小:0.06M
文件格式:.zip
开发语言:Java
更新时间:2018-01-06
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
可以使用文件选择器选择文件将txt文件转为Jtable表格,可以对表格进行删除添加 求行和和平均,求列和和平均,可以简单排序,查询,若导入的为满足要求的表格 形如(T,F)格式的,可以进行apriori算法得出关联规则
package dataOperation;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class DataOperation extends JFrame{
CardLayout cl = new CardLayout();
public static int point=0;//记录卡片在第几张
public int record=0;//记录一共有多少张卡 一直处于表格数组尾部
Container c = getContentPane(); //容器
String string = "Data_Operation";//初始标题
private static final long serialVersionUID = 6345343829488769567L;
private JFileChooser jFileChooser1 = new JFileChooser(new File("."));
private JPopupMenu Pop = new JPopupMenu();
private JMenuBar jmb = new JMenuBar();
private JMenu File = new JMenu("文件");
private JMenu Edit = new JMenu("编辑");
private JMenu Help = new JMenu("帮助");
private JMenu Tool = new JMenu("工具");
private JMenuItem Import = new JMenuItem("导入");
private JMenuItem Outport = new JMenuItem("导出");
private JMenuItem Delete = new JMenuItem("删除");
private JMenuItem Add = new JMenuItem("添加");
private JMenuItem Merge = new JMenuItem("合并");
private JMenuItem previous = new JMenuItem("上一张表");
private JMenuItem next = new JMenuItem("下一张表");
private JMenuItem first = new JMenuItem("第一张表");
private JMenuItem last = new JMenuItem("最后一张表");
private JMenuItem colSumAndAvg = new JMenuItem("求列和及平均");
private JMenuItem rowSumAndAvg = new JMenuItem("求行和及平均");
private JMenuItem apriori = new JMenuItem("频繁项集");
private JMenuItem relationTable = new JMenuItem("导出关系表");
private JMenuItem saveAs = new JMenuItem("另存为");
private JMenuItem im = new JMenuItem("导入");
private JMenuItem inquire1 = new JMenuItem("按值查询");
private JMenuItem inquire2 = new JMenuItem("按范围查询");
public DefaultTableModel[] tableModel=new DefaultTableModel[20];//最多可以二十张卡
public JTable[] table=new JTable[10];
//初始化
public DataOperation(){
super();
this.setLayout(new GridLayout(1,1));
c.setLayout(cl);
jmb.add(File);
jmb.add(Edit);
jmb.add(Tool);
File.add(Import);
File.add(Outport);
Edit.add(Merge);
Edit.add(colSumAndAvg);
Edit.add(rowSumAndAvg);
Tool.add(apriori);
Tool.add(relationTable);
Tool.add(inquire1);
Tool.add(inquire2);
Tool.add(previous);
Tool.add(next);
Tool.add(first);
Tool.add(last);
Pop.add(im);
Pop.add(saveAs);
Pop.add(Delete);
Pop.add(Add);
setJMenuBar(jmb);
addPopMenu();
this.setTitle(string);
this.setBackground(Color.white);
addAction();
setVisible(true);
setBounds(10, 10, 600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* 为每个菜单项添加功能
*/
private void addAction() {
// TODO 自动生成的方法存根
//求频繁项集
apriori.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(point==0){
JOptionPane.showMessageDialog(null, "当前无表!!", "当前无表!!", JOptionPane.ERROR_MESSAGE);
return;
}
if(!tableModel[point-1].getValueAt(0, 1).equals("T")&&!tableModel[point-1].getValueAt(0, 1).equals("F")){
JOptionPane.showMessageDialog(null, "当前表不能得出频繁项集!!", "当前表不能得出频繁项集!!", JOptionPane.ERROR_MESSAGE);
return ; //跳出方法体
}
@SuppressWarnings("rawtypes")
final ArrayList<ArrayList> allList = new ArrayList<ArrayList>(); //记录总清单
for(int i=0;i<tableModel[point-1].getRowCount();i ){
ArrayList<String> allRow = new ArrayList<String>(); //记录清单中每一行的数据
allRow.add((String) tableModel[point-1].getValueAt(i, 0));
for(int j=1;j<tableModel[point-1].getColumnCount();j ){
if(tableModel[point-1].getValueAt(i, j).equals("T")){
allRow.add(tableModel[point-1].getColumnName(j));
}
}
allList.add(allRow);
}
ArrayList<String> firstList = new ArrayList<String>();
for(int i=1;i<tableModel[point-1].getColumnCount();i ){
firstList.add(tableModel[point-1].getColumnName(i));
}
Apriori.apriori(allList,firstList);
}
});
//将普通表格转变为可以求频繁项集的表格
relationTable.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
if(point==0){
JOptionPane.showMessageDialog(null, "当前无表!!", "当前无表!!", JOptionPane.ERROR_MESSAGE);
return;
}
if(tableModel[point-1].getValueAt(0, 1).equals("T")||tableModel[point-1].getValueAt(0, 1).equals("F")){
JOptionPane.showMessageDialog(null, "当前表已经是关系表!!", "当前表已经是关系表!!", JOptionPane.ERROR_MESSAGE);
return; //跳出方法体
}
@SuppressWarnings({ "rawtypes" })
Vector<Vector> rowData = new Vector<Vector>();
Vector<String> col = new Vector<String>();
for(int i=0;i<tableModel[point-1].getColumnCount();i ){
if(tableModel[point-1].getColumnName(i).equals("sex")){
continue;
}
else col.add(tableModel[point-1].getColumnName(i));
}
int nameCount=0;
for(int i=0;i<tableModel[point-1].getRowCount();i ){
Vector<String> temp = new Vector<String>();
temp.add("Item" nameCount );
for(int j = 1;j<tableModel[point-1].getColumnCount();j ){
if(tableModel[point-1].getColumnName(j).equals("sex")){
continue;
}
double number ;
if(((String) tableModel[point-1].getValueAt(i, j)).trim().equals("NA")||((String) tableModel[point-1].getValueAt(i, j)).trim().equals("NaN")){
number=0;
}
else{
number=Double.valueOf((String) tableModel[point-1].getValueAt(i, j));
}
if(number<60.00) temp.add("F");
else temp.add("T");
}
rowData.add(temp);
}
DefaultTableModel model = new DefaultTableModel(rowData,col);
JTable tempTable = new JTable(model);
try {
save(tempTable);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
});
//按照特定值查询
inquire1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
if(point==0){
JOptionPane.showMessageDialog(null, "当前无表!!", "当前无表!!", JOptionPane.ERROR_MESSAGE);
return;
}
final JPanel panel = new JPanel();
final JDialog temp = new JDialog();
//temp.setLayout(null);
temp.add(panel,BorderLayout.CENTER);
final JLabel jl1 = new JLabel("属性:");
panel.add(jl1);
final JTextField aText = new JTextField(7);
//aText.setSize(10, 5);
panel.add(aText);
final JLabel jl2 = new JLabel("查询值:");
panel.add(jl2);
final JTextField bText = new JTextField(7);
panel.add(bText);
JButton jb = new JButton("查询");
panel.add(jb);
jb.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
String property = aText.getText(); //获得输入的属性
String value = bText.getText(); //获得要查询的值
@SuppressWarnings("rawtypes")
Vector<Vector> rowData = new Vector<Vector>();
Vector<String> colData = new Vector<String>();
for(int i=0;i<tableModel[point-1].getColumnCount();i ){
colData.add(tableModel[point-1].getColumnName(i));
}
boolean flag=false;
int num;
for(num=0;num<tableModel[point-1].getColumnCount();num ){
if(property.equals(tableModel[point-1].getColumnName(num))){
flag=true;
break;
}
}
if(!flag){
JOptionPane.showMessageDialog(null, "属性输入错误!!", "属性输入错误!!", JOptionPane.ERROR_MESSAGE);
}
for(int rowNum=0;rowNum<tableModel[point-1].getRowCount();rowNum ){
if( value.equals(((String) tableModel[point-1].getValueAt(rowNum, num)).trim())){
Vector<String> temp = new Vector<String>();
for(int colNum=0;colNum<tableModel[point-1].getColumnCount();colNum ){
temp.add((String) tableModel[point-1].getValueAt(rowNum, colNum));
}
rowData.add(temp);
}
}
final JDialog temp1 = new JDialog();
JPanel panel1 = new JPanel();
temp1.add(panel1);
DefaultTableModel model= new DefaultTableModel(rowData,colData);
final JTable newTable = new JTable(model);
JScrollPane scrollPane = new JScrollPane(newTable);
//tableAddPop();
temp1.add(scrollPane);
/////////////////////////////////// 需要修改
JButton j = new JButton("导出为TXT文件");
temp1.add(j,BorderLayout.SOUTH);
j.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
save(newTable);
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
temp1.setVisible(false);
}
});
temp1.setBounds(400,400,400,400);
temp1.setVisible(true);
temp.setVisible(false);
}
});
temp.setBounds(500, 300, 400, 150);
temp.setBackground(Color.white);
temp.setTitle("inquire");
temp.setVisible(true);
}
});
//按照指定范围查询
inquire2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
if(point==0){
JOptionPane.showMessageDialog(null, "当前无表!!", "当前无表!!", JOptionPane.ERROR_MESSAGE);
return;
}
final JPanel panel = new JPanel();
final JDialog temp = new JDialog();
//temp.setLayout(null);
temp.add(panel,BorderLayout.CENTER);
final JLabel jl1 = new JLabel("属性:");
panel.add(jl1);
final JTextField aText = new JTextField(7);
panel.add(aText);
final JLabel j2 = new JLabel("查找范围:");
panel.add(j2);
final JTextField bText = new JTextField(5);
panel.add(bText);
final JLabel j3 = new JLabel("~");
panel.add(j3);
final JTextField cText = new JTextField(5);
panel.add(cText);
JButton jb = new JButton("查询");
panel.add(jb);
jb.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
String property = aText.getText(); //获得要查找的属性
String value1 = bText.getText(); //获得查找范围的左部
String value2 = cText.getText(); //获得查找范围的右部
@SuppressWarnings("rawtypes")
Vector<Vector> rowData = new Vector<Vector>();
Vector<String> colData = new Vector<String>();
for(int i=0;i<tableModel[point-1].getColumnCount();i ){
colData.add(tableModel[point-1].getColumnName(i));
}
boolean flag=false;
int num;
for(num=0;num<tableModel[point-1].getColumnCount();num ){
if(property.equals(tableModel[point-1].getColumnName(num))){
flag=true;
break;
}
}
if(!flag){
JOptionPane.showMessageDialog(null, "属性输入错误!!", "属性输入错误!!", JOptionPane.ERROR_MESSAGE);
}
for(int rowNum=0;rowNum<tableModel[point-1].getRowCount();rowNum ){
if( Double.valueOf(value1)<Double.valueOf((((String) tableModel[point-1].getValueAt(rowNum, num)).trim()))
&&Double.valueOf((((String) tableModel[point-1].getValueAt(rowNum, num)).trim()))<Double.valueOf(value2)){
Vector<String> temp = new Vector<String>();
for(int colNum=0;colNum<tableModel[point-1].getColumnCount();colNum ){
temp.add((String) tableModel[point-1].getValueAt(rowNum, colNum));
}
rowData.add(temp);
}
}
final JDialog temp1 = new JDialog();
JPanel panel1 = new JPanel();
temp1.add(panel1);
DefaultTableModel model= new DefaultTableModel(rowData,colData);
final JTable newTable = new JTable(model);
JScrollPane scrollPane = new JScrollPane(newTable);
temp1.add(scrollPane);
JButton j = new JButton("导出为TXT文件");
temp1.add(j,BorderLayout.SOUTH);
j.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
save(newTable);
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
temp1.setVisible(false);
}
});
temp1.setBounds(400,400,400,400);
temp1.setVisible(true);
temp.setVisible(false);
}
});
temp.setBounds(500, 300, 400, 150);
temp.setBackground(Color.white);
temp.setTitle("inquire");
temp.setVisible(true);
}
});
//求列和及平均值
colSumAndAvg.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(point==0){
JOptionPane.showMessageDialog(null, "当前无表!!", "当前无表!!", JOptionPane.ERROR_MESSAGE);
return;
}
DecimalFormat df = new DecimalFormat("######0.00");
DefaultTableModel tableTemp ;
tableTemp=tableModel[point-1];
Vector<String> vector1 = new Vector<String>();//记录每一列总和
Vector<String> vector2 = new Vector<String>();//记录每一列平均值
double sum=0;//求总和
double avg=0;//求平均值
vector1.add("总和");
vector2.add("平均值");
for(int colNum=1;colNum<tableTemp.getColumnCount();colNum ){
sum=0;
for(int rowNum=0;rowNum<tableTemp.getRowCount();rowNum ){
if( !isNumeric((String)tableTemp.getValueAt(rowNum, colNum)))
sum =0;
else sum =Double.valueOf((String) tableTemp.getValueAt(rowNum, colNum));
}
vector1.add(String.valueOf(df.format(sum)));
avg=sum/tableTemp.getRowCount();
vector2.add(String.valueOf(df.format(avg)));
}
tableModel[point-1].addRow(vector1);
tableModel[point-1].addRow(vector2);
}
private boolean isNumeric(String str) {
// TODO 自动生成的方法存根
Pattern pattern = Pattern.compile("-?[0-9] \\.?[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
});
//求行和及平均值
rowSumAndAvg.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(point==0){
JOptionPane.showMessageDialog(null, "当前无表!!", "当前无表!!", JOptionPane.ERROR_MESSAGE);
return;
}
DecimalFormat df = new DecimalFormat("######0.00");
DefaultTableModel tableTemp ;
tableTemp=tableModel[point-1];
Vector<String> vector1 = new Vector<String>();//记录每一行总和
Vector<String> vector2 = new Vector<String>();//记录每一行平均值
double sum=0;//求总和
double avg=0;//求平均值
for(int rowNum=0;rowNum<tableTemp.getRowCount();rowNum ){
sum=0;
for(int colNum=2;colNum<tableTemp.getColumnCount();colNum ){
if( !isNumeric((String)tableTemp.getValueAt(rowNum, colNum)))
sum =0;
else sum =Double.valueOf((String) tableTemp.getValueAt(rowNum, colNum));
}
vector1.add(String.valueOf(df.format(sum)));
avg=sum/(tableTemp.getColumnCount()-2);
vector2.add(String.valueOf(df.format(avg)));
}
tableModel[point-1].addColumn("总和", vector1);
tableModel[point-1].addColumn("平均值", vector2);
}
private boolean isNumeric(String str) {
// TODO 自动生成的方法存根
Pattern pattern = Pattern.compile("-?[0-9] \\.?[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
});
//导入功能
Import.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
point ;//此时第几张卡显示
record ;//此时一共有几张卡
open();
//string = "Data_Operation_表格1";
cl.last(c);
setTitle("Data_Operation_表格" record "_共" record "张表");
}
});
//导入功能
im.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
point ;//此时第几张卡显示
record ;//此时一共有几张卡
open();
//setTitle("Data_Operation 表格" point);
cl.last(c);
setTitle("Data_Operation_表格" record "_共" record "张表");
}
});
//导出功能
Outport.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(point<1){
JOptionPane.showMessageDialog(null, "当前无表", "当前无表", JOptionPane.ERROR_MESSAGE);
}
else{
try {
save(table[point-1]);
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
}
});
//导出功能 另存为
saveAs.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(point<1){
JOptionPane.showMessageDialog(null, "当前无表", "当前无表", JOptionPane.ERROR_MESSAGE);
}
else{
try {
save(table[point-1]);
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
}
});
//删除选定行
Delete.addActionListener(new ActionListener(){ //
//
@Override //
public void actionPerformed(ActionEvent e) { //
// TODO 自动生成的方法存根
if(point==0){
JOptionPane.showMessageDialog(null, "当前无表!!", "当前无表!!", JOptionPane.ERROR_MESSAGE);
return;
}//
int[] selectedRows = table[point-1].getSelectedRows(); //删除选中行 //
for(int row = 0;row<selectedRows.length;row ){ //
tableModel[point-1].removeRow(selectedRows[row]-row); //
} //
} //
//
});
//在末尾添加空白行
Add.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) { //在末尾添加一行空白行
// TODO 自动生成的方法存根
if(point==0){
JOptionPane.showMessageDialog(null, "当前无表!!", "当前无表!!", JOptionPane.ERROR_MESSAGE);
return;
}
JTextField aTextField=new JTextField(15);
String[] rowValues = {aTextField.getText(),aTextField.getText()};
tableModel[point-1].addRow(rowValues);
}
});
//合并功能
Merge.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(point==0){
JOptionPane.showMessageDialog(null, "当前无表!!", "当前无表!!", JOptionPane.ERROR_MESSAGE);
return;
}
if(point==1){
JOptionPane.showMessageDialog(null, "当前只有一张表!无法合并!", "当前只有一张表!无法合并!", JOptionPane.ERROR_MESSAGE);
return;
}
final JPanel panel = new JPanel();
final JDialog temp = new JDialog();
temp.add(panel,BorderLayout.CENTER);
final JLabel jl1 = new JLabel("表:");
panel.add(jl1);
final JTextField aText = new JTextField(6);
panel.add(aText);
final JLabel jl2 = new JLabel("表:");
panel.add(jl2);
final JTextField bText = new JTextField(6);
panel.add(bText);
final JLabel jl3 = new JLabel("关键字:");
panel.add(jl3);
final JTextField cText = new JTextField(10);
panel.add(cText);
JButton j1 = new JButton("合并");
panel.add(j1);
temp.setBounds(400,400,250,125);
temp.setVisible(true);
j1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
if(record<2){
JOptionPane.showMessageDialog(null, "当前只有一张表!!", "当前只有一张表!!", JOptionPane.ERROR_MESSAGE);
}else if(aText.getText().length()<1||!isNum(Double.valueOf(aText.getText()))||bText.getText().length()<1||!isNum(Double.valueOf(aText.getText()))
||cText.getText().length()<1){
JOptionPane.showMessageDialog(null, "输入错误!!", "输入错误!!", JOptionPane.ERROR_MESSAGE);
}
else{
temp.setVisible(false);
record ; point=record; //新建表,表格指针自动加一,必须写在这儿 此时最后一张卡显示 每次新建一张表,point必定与record相等
double num1= Double.valueOf(aText.getText());
double num2= Double.valueOf(bText.getText());
String key = cText.getText();
String[] newCol = new String[tableModel[(int) (num1-1)].getColumnCount() tableModel[(int) (num2-1)].getColumnCount()-1];
newCol=dealCol(newCol,(int)num1,(int)num2,key);
String[][] table1 = new String[tableModel[(int) (num1-1)].getRowCount()][tableModel[(int) (num1-1)].getColumnCount()]; //新建两个二维数组分别收纳表格内的数据
String[][] table2 = new String[tableModel[(int) (num2-1)].getRowCount()][tableModel[(int) (num2-1)].getColumnCount()];
for(int i=0;i<tableModel[(int) (num1-1)].getRowCount();i ){
for(int j=0;j<tableModel[(int)(num1-1)].getColumnCount();j ){
table1[i][j]=(String) tableModel[(int) (num1-1)].getValueAt(i, j);
}
}
for(int i=0;i<tableModel[(int) (num2-1)].getRowCount();i ){
for(int j=0;j<tableModel[(int)(num2-1)].getColumnCount();j ){
table2[i][j]=(String) tableModel[(int) (num2-1)].getValueAt(i, j);
}
}//将要合并的两个表导入到新建好的二维数组中
int rowNum=table1.length; //合并后的二维数组维数
for(int i=0;i<table2.length;i ){
boolean flag=true;//设立标志位 相等为false 不等为true
for(int j=0;j<table1.length;j ){
if(table2[i][0].equals(table1[j][0])){
flag=false;
break;
}
}
if(flag) rowNum;
}
System.out.println(rowNum);
String[][] table3= new String[rowNum][newCol.length];//建立合并的数组
int rowCount=0;//记录合并后新数组的维度数 横向
int colCount=0;//记录合并后新数组的列数 纵向
for(int i=0;i<table1.length;i ){//先将表格1数组的关键字和数据写入到合并的数组中
for(int j=0;j<table1[i].length;j ){
table3[rowCount][j]=table1[i][j];////////////
}
rowCount ;
}
boolean flag;
for(int i=0;i<table2.length;i ){
colCount=table1[0].length;
flag=true ;//重制标志位 相等为false 不等为true
for(int j=0;j<table3.length;j ){
if(table2[i][0].equals(table3[j][0])){
flag =false;
for(int k=1;k<table2[i].length;k ){
table3[j][colCount ]=table2[i][k];
}
break;
}
}
// 如果表2中没找到与表一中关键字相等的项
if(flag){ //
table3[rowCount][0]=table2[i][0];
colCount = table1[0].length; //
for(int x=1;i<colCount;i ){ //
table3[rowCount][x]="Na"; //
} //
for(int m=0;m<table2[i].length;m ){
if(tableModel[(int) (num2-1)].getColumnName(m).equals(cText.getText())){ //
System.out.println(tableModel[(int) (num2-1)].getColumnName(m));
continue;
}
table3[rowCount][colCount ]=table2[i][m]; //
}
//
rowCount ; //
} //
}
for(int i=0;i<table3.length;i ){ //如果新建的表内有控制 则变为Na
for(int j=0;j<table3[i].length;j ){
if(table3[i][j]==null||table3[i][j].equals("")){
table3[i][j]="Na";
}
}
}
tableModel[record-1] = new DefaultTableModel(table3,newCol);
table[record-1] = new JTable(tableModel[record-1]);
JScrollPane scrollPane = new JScrollPane(table[record-1]); //滚动面板
c.add(scrollPane); // 添加进容器内
cl.last(c); // 直接显示最新导入的表格
sort(tableModel[record-1],table[record-1]);//
tableAddPop(table[record-1]);
setTitle("Data_Operation_表格" point "_共" record "张表");
}
}
private boolean isNum(Double num) {
// TODO 自动生成的方法存根
if(num<10&&num>0) return true;
return false;
}
private String[] dealCol(String[] newCol, int num1,
int num2, String key) {
// TODO 自动生成的方法存根
int num=0; //列指针 走到哪指到哪
for(int i=0;i<tableModel[num1-1].getColumnCount();i ){
newCol[num]=tableModel[num1-1].getColumnName(i);
num ;
}
for(int j=0;j<tableModel[num2-1].getColumnCount();j ){
if(key.equals(tableModel[num2-1].getColumnName(j)))
continue;
else newCol[num ]=tableModel[num2-1].getColumnName(j);
}
return newCol;
}
});
}
});
//卡片布局选表 下一个
next.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(point==0){
JOptionPane.showMessageDialog(null, "当前无表!!", "当前无表!!", JOptionPane.ERROR_MESSAGE);
return;
}
point ;
if(point>record){
JOptionPane.showMessageDialog(null, "当前为最后一张表!!", "当前为最后一张表!!", JOptionPane.ERROR_MESSAGE);
point--;
}
else{ cl.next(c); setTitle("Data_Operation_表格" point "_共" record "张表");}
}
});
//卡片布局选表 上一个
previous.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(point==0){
JOptionPane.showMessageDialog(null, "当前无表!!", "当前无表!!", JOptionPane.ERROR_MESSAGE);
return;
}
point--;
if(point<1){
JOptionPane.showMessageDialog(null, "当前为第一张表!!", "当前为第一张表!!", JOptionPane.ERROR_MESSAGE);
point ;
}
else {cl.previous(c);setTitle("Data_Operation_表格" point "_共" record "张表");}
}
});
//卡片布局选表 第一个
first.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(point==0){
JOptionPane.showMessageDialog(null, "当前无表!!", "当前无表!!", JOptionPane.ERROR_MESSAGE);
return;
}
cl.first(c);
setTitle("Data_Operation_表格1_共" record "张表");
point=1;
}
});
//卡片布局选表 最后一个
last.addActionListener(new ActionListener(){////////////////////////////////
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(point==0){
JOptionPane.showMessageDialog(null, "当前无表!!", "当前无表!!", JOptionPane.ERROR_MESSAGE);
return;
}
cl.last(c);
setTitle("Data_Operation_表格" record "_共" record "张表");
point=record;
}
});
}
/**
* 给表格添加右键弹出式菜单
* @param table
*/
public void tableAddPop(final JTable table){
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent evt) {
if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
//通过点击位置找到点击为表格中的行
int focusedRowIndex = table.rowAtPoint(evt.getPoint());
if (focusedRowIndex == -1) {
return;
}
//将表格所选项设为当前右键点击的行
table.setRowSelectionInterval(focusedRowIndex, focusedRowIndex);
//弹出菜单
Pop.show(table, evt.getX(), evt.getY());
}
}
});
}
/**
* 在窗体内添加弹出式菜单
*/
public void addPopMenu(){
this.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e){
if(e.isPopupTrigger()){
Pop.show(e.getComponent(),e.getX(),e.getY());
}
}
});
}
/**
* 打开TXT文件 以表格形式展现
*/
private void open(){
if(jFileChooser1.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
open(jFileChooser1.getSelectedFile());
}
/**
* 打开TXT文件 以表格形式展现
*/
private void open(File file) {
// TODO 自动生成的方法存根
try{
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
ArrayList<String> col = new ArrayList<String>();
@SuppressWarnings("rawtypes")
ArrayList<ArrayList> rowData= new ArrayList<ArrayList>();
String eachLine = null;//定义每一行
eachLine = br.readLine();
String[] line = eachLine.split(" ");
for(int j=0;j<line.length;j ){
col.add(line[j]);
}
while((eachLine = br.readLine()) != null){//读文件至末尾
//-----split(String):根据给定正则表达式的匹配拆分此字符串。
String[] temp = eachLine.split(" ");//每一行里的tab
ArrayList<String> row = new ArrayList<String>();
for(int i = 0; i < temp.length; i ){//遍历每一行
row.add(temp[i].trim());//把每一行都加入row
}
rowData.add(row);//再把每一个row的数据给rowData
}
br.close();
createTable(rowData, col);
}
catch(IOException ex){
}
}
/**
* 根据处理好的ArrayList数组 创建表格
* @param rowData
* @param col
*/
@SuppressWarnings({ "unchecked", "rawtypes" })//////////////////////////////
private void createTable(ArrayList<ArrayList> rowData, ArrayList<String> col) {
// TODO 自动生成的方法存根
int colSize=col.size();
String[] colTemp = new String[colSize];
for(int i=0;i<col.size();i ){ //将ArrayList<String> col转换为一维数组
colTemp[i]=col.get(i);
//System.out.println(colTemp[i]); //测试输出数组是否正确
}
String[][] rowTemp = new String[rowData.size()][col.size()];
for(int i=0;i<rowData.size();i ){ //将ArrayList<ArrayList>转换为二维数组
ArrayList<String> eachLine = rowData.get(i);
for(int j=0;j<col.size();j ){
rowTemp[i][j] = (String) eachLine.get(j);
//System.out.print(rowTemp[i][j] " "); //测试输出数组是否正确
}
System.out.println();
}
tableModel[record-1] = new DefaultTableModel(rowTemp,colTemp);
table[record-1] = new JTable(tableModel[record-1]);
JScrollPane scrollPane = new JScrollPane(table[record-1]); //滚动面板
c.add(scrollPane); // 添加进容器内
cl.next(c); // 直接显示最新导入的表格
//点击表头排序
sort(tableModel[record-1],table[record-1]);//
tableAddPop(table[record-1]);
}
/**
* 实现表格的排序功能
*/
public void sort(DefaultTableModel tableModel, JTable table) {////////////////////////////////////////////////////////////
// TODO 自动生成的方法存根
RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);
table.setRowSorter(sorter);
}
/**
* 保存文件
参考http://www.devba.com/index.php/archives/2311.html
* @param table
* @throws IOException
*/
//没有设置当有行的值为空值时的情况
private void save(JTable table) throws IOException{
if(jFileChooser1.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
save(jFileChooser1.getSelectedFile(),table);
}
/**
* 保存文件
参考http://www.devba.com/index.php/archives/2311.html
* @param table
* @throws IOException
*/
private void save(File file,JTable table) throws IOException {
TableModel model=table.getModel();
BufferedWriter bWriter = new BufferedWriter(new FileWriter(file));
for(int i=0;i<model.getColumnCount();i ){
bWriter.write(model.getColumnName(i));
bWriter.write("\t");
}
bWriter.newLine();
for(int i=0;i<model.getRowCount();i ){
for(int j=0;j<model.getColumnCount();j ){
if((String)model.getValueAt(i, j)==null||model.getValueAt(i, j).equals("")){
bWriter.write("Na\t");
System.out.println("Na\t");
}
else{
bWriter.write(model.getValueAt(i, j).toString() "\t");
System.out.println(model.getValueAt(i, j).toString() "\t");
}
//bWriter.write("\t");
}
System.out.println();
bWriter.newLine();
}
bWriter.close();
}
public static void main(String[] args){
new DataOperation();
}
}