CurrencyTable : 网格 « Swing组件 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » Swing组件 » 网格屏幕截图 
CurrencyTable
CurrencyTable

/*
Core SWING Advanced Programming 
By Kim Topley
ISBN: 0 13 083292 8       
Publisher: Prentice Hall  
*/


import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;

public class CurrencyTable {
  public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    catch (Exception evt) {}
  
    JFrame f = new JFrame("Currency Table");
    JTable tbl = new JTable(new CurrencyTableModel());

    TableColumnModel tcm = tbl.getColumnModel();
    tcm.getColumn(0).setPreferredWidth(150);
    tcm.getColumn(0).setMinWidth(150);

    tbl.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    tbl.setPreferredScrollableViewportSize(tbl.getPreferredSize());

    JScrollPane sp = new JScrollPane(tbl);
    f.getContentPane().add(sp, "Center");
    f.pack();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) {
        System.exit(0);
      }
    });
    f.setVisible(true);
  }
}

class DataWithIcon {
  public DataWithIcon(Object data, Icon icon) {
    this.data = data;
    this.icon = icon;
  }

  public Icon getIcon() {
    return icon;
  }

  public Object getData() {
    return data;
  }

  public String toString() {
    return data.toString();
  }

  protected Icon icon;

  protected Object data;
}

class CurrencyTableModel extends AbstractTableModel {
  protected String[] columnNames = "Currency""Yesterday""Today",
      "Change" };

  // Constructor: calculate currency change to create the last column
  public CurrencyTableModel() {
    for (int i = 0; i < data.length; i++) {
      data[i][DIFF_COLUMNnew Double(
          ((Doubledata[i][NEW_RATE_COLUMN]).doubleValue()
              ((Doubledata[i][OLD_RATE_COLUMN]).doubleValue());
    }
  }

  // Implementation of TableModel interface
  public int getRowCount() {
    return data.length;
  }

  public int getColumnCount() {
    return COLUMN_COUNT;
  }

  public Object getValueAt(int row, int column) {
    return data[row][column];
  }

  public Class getColumnClass(int column) {
    return (data[0][column]).getClass();
  }

  public String getColumnName(int column) {
    return columnNames[column];
  }

  protected static final int OLD_RATE_COLUMN = 1;

  protected static final int NEW_RATE_COLUMN = 2;

  protected static final int DIFF_COLUMN = 3;

  protected static final int COLUMN_COUNT = 4;

  protected static final Class thisClass = CurrencyTableModel.class;

  protected Object[][] data = new Object[][] {
      {
          new DataWithIcon("Belgian Franc"new ImageIcon(thisClass
              .getResource("belgium.gif"))),
          new Double(37.6460110)new Double(37.6508921)null },
      {
          new DataWithIcon("British Pound"new ImageIcon(thisClass
              .getResource("gb.gif")))new Double(0.6213051),
          new Double(0.6104102)null },
      {
          new DataWithIcon("Canadian Dollar"new ImageIcon(thisClass
              .getResource("canada.gif"))),
          new Double(1.4651209)new Double(1.5011104)null },
      {
          new DataWithIcon("French Franc"new ImageIcon(thisClass
              .getResource("france.gif"))),
          new Double(6.1060001)new Double(6.0100101)null },
      {
          new DataWithIcon("Italian Lire"new ImageIcon(thisClass
              .getResource("italy.gif"))),
          new Double(1181.3668977)new Double(1182.104)null },
      {
          new DataWithIcon("German Mark"new ImageIcon(thisClass
              .getResource("germany.gif"))),
          new Double(1.8191804)new Double(1.8223421)null },
      {
          new DataWithIcon("Japanese Yen"new ImageIcon(thisClass
              .getResource("japan.gif"))),
          new Double(141.0815412)new Double(121.0040432)null } };
}

           
       
Related examples in the same category
1. 超链接表超链接表
2. 柱弹出菜单柱弹出菜单
3. 树表树表
4. 图形界面组合表图形界面组合表
5. Tabbable货币表Tabbable货币表
6. 图标货币表图标货币表
7. 多行标题表多行标题表
8. 工具提示表工具提示表
9. 条纹货币表条纹货币表
10. 计算栏表计算栏表
11. 表公用事业
12. 部分货币表部分货币表
13. 突出货币表突出货币表
14. 突出货币表2突出货币表2
15. 多行表多行表
16. 更新标明货币表更新标明货币表
17. 编辑标明货币表编辑标明货币表
18. 组合表组合表
19. Groupable (组合)标题范例Groupable (组合)标题范例
20. MultiWidth标题范例MultiWidth标题范例
21. 多行标题范例多行标题范例
22. 表行标题范例表行标题范例
23. 固定表列范例固定表列范例
24. 按钮表范例按钮表范例
25. 单选按钮表范例单选按钮表范例
26. RadioButton表范例2RadioButton表范例2
27. 多行单元范例多行单元范例
28. 每一行的不同编辑器范例每一行的不同编辑器范例
29. 多分量表:复选框和组合表多分量表:复选框和组合表
30. 多种成分表2 :复选框多种成分表2 :复选框
31. 联盟数据表实例联盟数据表实例
32. 共计(计算)行范例共计(计算)行范例
33. 有色单元表范例
34. 多种字体单元表范例
35. 多跨度单元表范例
36. 混合表范例
37. Pushable表头范例Pushable表头范例
38. 排序表范例排序表范例
39. 工具提示头表范例工具提示头表范例
40. 指标表范例指标表范例
41. 固定表行范例固定表行范例
42. 多行标题范例
43. 塔边境表范例塔边境表范例
44. 单元边境表范例单元边境表范例
45. 隐藏栏表范例隐藏栏表范例
46. 动画Icon表范例动画Icon表范例
47. 动画图标头范例
48. 编辑头表范例编辑头表范例
49. 编辑头表范例2编辑头表范例2
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.