老师让我写一个关于数据动态显示的界面,具体功能如下图所示:
点击上面表格中的某一行,显示改行行业名称下面每个行业对应的所有个股显示出来
public static Vector<Industry_data> importExcel(String fileName) { Vector<Industry_data> v = new Vector<Industry_data>(); try { Workbook book = Workbook.getWorkbook(new File(fileName)); Sheet sheet = book.getSheet(0); // 获得第一个工作表对象 int rows = sheet.getRows(); for (int i = 1; i < rows; i++) { Cell[] cell = sheet.getRow(i); if (cell.length == 0) continue; Industry_data data = new Industry_data(); data.setName(sheet.getCell(0, i).getContents()); data.setFinance(sheet.getCell(1, i).getContents()); data.setFinance_increase(sheet.getCell(2, i).getContents()); data.setFinance_proporation(sheet.getCell(6, i).getContents()); data.setFinance_rank(sheet.getCell(7, i).getContents()); v.add(data); } book.close(); } catch (Exception e) { } return v; }在主函数中,只需调用这个函数,传进去一个路径,就可以获取表格数据的内容
String fileName = "D:\\2016-03-09两融个股、行业增幅.xls"; Vector<Industry_data> content = readExcel.importExcel(fileName); Industry_data[] p = new Industry_data[content.size()]; Object[][] rowData = new Object[content.size()][5]; for (int i = 0; i < content.size(); i++) { p[i] = (Industry_data) content.get(i); rowData[i][0] = p[i].getName(); rowData[i][1] = p[i].getFinance(); rowData[i][2] = p[i].getFinance_increase(); rowData[i][3] = p[i].getFinance_proporation(); rowData[i][4] = p[i].getFinance_rank(); System.out.println(p[i].getName() + "\t" + p[i].getFinance() + "\t" + p[i].getFinance_increase() + "\t" + p[i].getFinance_proporation() + "\t" + p[i].getFinance_rank()); }注:在这里,我将表格中数据包装成一个类Industry_data.java
public class Industry_data { private String name; //行业名称 private String finance; //当日融资金额 private String finance_increase;//当日融资增幅 private String finance_proporation;//占总融资比 private String finance_rank;//融资排名 public Industry_data() { // TODO Auto-generated constructor stub } public Industry_data(String name,String finance,String finance_increase,String finance_proporation,String finance_rank){ super(); this.name=name; this.finance=finance; this.finance_increase=finance_increase; this.finance_proporation=finance_proporation; this.finance_rank=finance_rank; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFinance() { return finance; } public void setFinance(String finance) { this.finance = finance; } public String getFinance_increase() { return finance_increase; } public void setFinance_increase(String finance_increase) { this.finance_increase = finance_increase; } public String getFinance_proporation() { return finance_proporation; } public void setFinance_proporation(String finance_proporation) { this.finance_proporation = finance_proporation; } public String getFinance_rank() { return finance_rank; } public void setFinance_rank(String finance_rank) { this.finance_rank = finance_rank; } }
final Object[] columnNames = { "行业名称", "当日融资金额", "当日融资增幅",// 列名最好用final修饰 "占总融资比", "融资排名" }; final JTable Industry = new JTable(rowData, columnNames); Industry.setRowHeight(30);// 设置每行的高度为20 Industry.setRowMargin(5);// 设置相邻两行单元格的距离 JScrollPane pane1 = new JScrollPane(Industry); panel.add(pane1);
Industry.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 1) // 单击表格一次 { //先删除下面的表格 DefaultTableModel ulcTableModel = (DefaultTableModel) example2 .getModel(); for (int i = ulcTableModel.getRowCount() - 1; i >= 0; i--) { ulcTableModel.removeRow(i); } int row = ((JTable) e.getSource()).rowAtPoint(e.getPoint()); // 获得行位置 int col = ((JTable) e.getSource()).columnAtPoint(e .getPoint()); // 获得列位置 String name = (String) Industry.getValueAt(row, 0);</span> name = name.substring(1); System.out.println("第" + (row + 1) + "行,第" + (col + 1) + "列被点击了,内容为" + name); //添加下面的表格的数据部分 Object[][] rowData2 = newTable(name); tablemodel.setDataVector(rowData2, column); example2.updateUI();</span> } else return; } });name是点击表格某一行获取到的行业名称,newTable(String S)方法用来获得该行业下所有
final Object[] column = { "选股日期", "代码", "名称", "行业", "盘子大小(亿)", "收盘价", "当天增幅", "1天增幅", "二日增幅", "2天涨幅", "三日增幅", "3天涨幅", "五日增幅", "十日增幅", "连增天数", "融资/个股总资产", "个股融资/两市总融资产" }; final DefaultTableModel tablemodel=new DefaultTableModel(); final JTable example2 = new JTable(tablemodel); example2.setRowHeight(20); JScrollPane pane2 = new JScrollPane(example2); panel.add(pane2);对于newTable()函数,代码如下:
public static Object[][] newTable(String name) { // 选股日期 代码 名称 行业 盘子大小(亿) 收盘价 当天增幅 1天涨幅 二日增幅 2天涨幅 三日增幅 3天涨幅 五日增幅 十日增幅 // 连增天数 融资/个股总资产 个股融资/两市总融资产 final Object[] column = { "选股日期", "代码", "名称", "行业", "盘子大小(亿)", "收盘价", "当天增幅", "1天增幅", "二日增幅", "2天涨幅", "三日增幅", "3天涨幅", "五日增幅", "十日增幅", "连增天数", "融资/个股总资产", "个股融资/两市总融资产" }; String fileName = "D:\\行业个股数据.xls"; Vector<Individual_stock> content = readExcel.importExcel2(fileName); Individual_stock[] p = new Individual_stock[content.size()]; // System.out.println(content.size()); int j = 0; for (int i = 0; i < content.size(); i++) { p[i] = (Individual_stock) content.get(i); // System.out.println(((Individual_stock)content.get(i)).getIndustry_name()); // System.out.println(p[i].getIndustry_name()+"|"+name+name.equals(p[i].getIndustry_name().substring(1))); if (name.equals(p[i].getIndustry_name().substring(1))) { j++; // System.out.println(p[i].getDate() + "\t" + p[i].getCode() // + "\t" + p[i].getName() + "\t" // + p[i].getIndustry_name() + "\t" + p[i].getSize() // + "\t" + p[i].getClosing_price() + "\t" // + p[i].getToday_Add() + "\t" // + p[i].getOneday_Increase() + "\t" // + p[i].getTwoday_Add() + "\t" // + p[i].getTwoday_Increase() + "\t" // + p[i].getThreeday_Add() + "\t" // + p[i].getThreeday_Increase() + "\t" // + p[i].getFiveday_Increase() + "\t" // + p[i].getTenday_Increase() + "\t" + p[i].getLast_day() // + "\t" + p[i].getTotal_assets1() + "\t" // + p[i].getTotal_assets2()); } else continue; } //System.out.println("j=" + j); Object[][] rowData = new Object[j][17]; int k = 0; for (int i = 0; i < content.size(); i++) { p[i] = (Individual_stock) content.get(i); // System.out.println(((Individual_stock)content.get(i)).getIndustry_name()); // System.out.println(p[i].getIndustry_name()+"|"+name+name.equals(p[i].getIndustry_name().substring(1))); if (name.equals(p[i].getIndustry_name().substring(1))) { //System.out.println("k=" + k); rowData[k][0] = p[i].getDate(); rowData[k][1] = p[i].getCode(); rowData[k][2] = p[i].getName(); rowData[k][3] = p[i].getIndustry_name(); rowData[k][4] = p[i].getSize(); rowData[k][5] = p[i].getClosing_price(); rowData[k][6] = p[i].getToday_Add(); rowData[k][7] = p[i].getOneday_Increase(); rowData[k][8] = p[i].getTwoday_Add(); rowData[k][9] = p[i].getTwoday_Increase(); rowData[k][10] = p[i].getThreeday_Add(); rowData[k][11] = p[i].getThreeday_Increase(); rowData[k][12] = p[i].getFiveday_Increase(); rowData[k][13] = p[i].getTenday_Increase(); rowData[k][14] = p[i].getLast_day(); rowData[k][15] = p[i].getTotal_assets1(); rowData[k][16] = p[i].getTotal_assets2(); // System.out.println(p[i].getDate() + "\t" + p[i].getCode() // + "\t" + p[i].getName() + "\t" // + p[i].getIndustry_name() + "\t" + p[i].getSize() // + "\t" + p[i].getClosing_price() + "\t" // + p[i].getToday_Add() + "\t" // + p[i].getOneday_Increase() + "\t" // + p[i].getTwoday_Add() + "\t" // + p[i].getTwoday_Increase() + "\t" // + p[i].getThreeday_Add() + "\t" // + p[i].getThreeday_Increase() + "\t" // + p[i].getFiveday_Increase() + "\t" // + p[i].getTenday_Increase() + "\t" + p[i].getLast_day() // + "\t" + p[i].getTotal_assets1() + "\t" // + p[i].getTotal_assets2()); k++; } else continue; } // final JTable Industry = new JTable(rowData, column); // JScrollPane pane1 = new JScrollPane(Industry); return rowData; }由于“行业个股数据.xls”表格显示的是所有行业下所有个股的数据,所以先获取到某个行业下的所有个股数量,存到j里面。