中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 用户界面
JTable 的密码项
作者:未知 时间:2005-07-24 21:16 出处:JR 责编:chinaitpower
              摘要:JTable 的密码项


有时候表格里需要显示密码项,又不能直接让用户看到密码。
这里做了个小例子,算是最初级的实现, 见笑了

原理也简单:
1、 设置“密码”列的编辑器为 JPasswordField
2、 设置“密码”列的表现器,并重写 setValue 方法
    用 “*” 来代替密码字符显示

示例代码下载:http://my.nbip.net/homepage/zhouB403/MyCode/TablePassWord.exe


===========================================================================
  1. /**
  2.  *  [PasswordTable.java]   表格密码项示例
  3.  *
  4.  *
  5.  * 创建日期:(2003-9-5)
  6.  * @author:ONE_Fox
  7.  */
  8.  
  9.  
  10.  
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import javax.swing.*;
  14. import javax.swing.table.*;
  15.     
  16.     
  17. public class PasswordTable extends JFrame {
  18.     
  19.     //表头信息
  20.     private String[] colname = {"姓名","性别","年龄","帐号","密码"};
  21.     //表内容
  22.     private Object[][] data = {{"张三"new Boolean(true), "18""Zhang3""12345"},
  23.                                {"李四"new Boolean(true), "22""Li4""54321"},
  24.                                {"王五"new Boolean(false), "23""Wang5""52052"},
  25.                                {"啊明"new Boolean(true), "19""A_ming""28865"},
  26.                                {"珊珊"new Boolean(false), "21""Sam3""33333"}}; 
  27.     
  28.     //界面组件----------------------//
  29.     private JButton showPassword = new JButton("显示密码");
  30.     
  31.     private JScrollPane scroPanel = new JScrollPane(); //中层滚动面板
  32.     private DefaultTableModel model; //列表默认TableModel
  33.     private JTable table;
  34.     
  35.     private JTextArea show = new JTextArea(6, 10);
  36.     private JScrollPane showPanel = new JScrollPane(show); //底层滚动面板
  37.  
  38.  
  39.  
  40. //-----------------------------------------------------------//
  41.  
  42. /**
  43.  * 构造方法 PasswordTable()
  44.  */    
  45.     public PasswordTable() {
  46.         makeFace();  //界面构建
  47.         addListener();  //添加监听
  48.         showFace();  //界面显示
  49.     }
  50.  
  51.  
  52.  
  53. //-----------------------------------------------------------//
  54.  
  55. /**
  56.  * 方法: 界面构建 makeFace()
  57.  */
  58.     private void makeFace() {
  59.         JPanel upPanel = new JPanel();
  60.         upPanel.add(showPassword);
  61.         
  62.         
  63.         table = new JTable(model = new DefaultTableModel(data,colname) {
  64.                     public Class getColumnClass(int column) {
  65.                         return getValueAt(0, column).getClass();
  66.                     }
  67.                 });
  68.         
  69.         
  70.         //设置密码格编辑器-------------------//
  71.         JPasswordField passwordEdit = new JPasswordField();
  72.         passwordEdit.setBorder(null);
  73.         table.getColumn("密码").setCellEditor(new DefaultCellEditor(passwordEdit));
  74.         
  75.         
  76.         //设置密码格表现器-------------------//
  77.         table.getColumn("密码").setCellRenderer(new DefaultTableCellRenderer() {
  78.             //重写 setValue 方法
  79.             public void setValue(Object value) {
  80.                 String password = "";
  81.                 int wordLong = value.toString().length();
  82.                 
  83.                 for(int i = 0; i < wordLong; i++)
  84.                     password += "*";
  85.                 
  86.                 super.setValue(password);
  87.             }
  88.         });
  89.         
  90.         scroPanel.getViewport().setBackground(Color.white);
  91.         scroPanel.getViewport().add(table);
  92.         
  93.         
  94.         JPanel downPanel = new JPanel();
  95.         downPanel.setLayout(new BorderLayout());
  96.         downPanel.add(showPanel);
  97.         
  98.         //总体界面布局------------------------//
  99.         getContentPane().add(upPanel, BorderLayout.NORTH);
  100.         getContentPane().add(scroPanel, BorderLayout.CENTER);
  101.         getContentPane().add(downPanel, BorderLayout.SOUTH);
  102.     }
  103.  
  104.  
  105. /**
  106.  * 方法: 界面显示 showFace()
  107.  */
  108.     private void showFace() {
  109.         setTitle("JTable 密码格示例");
  110.         setSize(500,300);
  111.         Toolkit tmpTK = Toolkit.getDefaultToolkit();
  112.         Dimension dime = tmpTK.getScreenSize();
  113.         
  114.         setLocation(dime.width2 - 250, dime.height2 - 200);
  115.         show();
  116.     }
  117.  
  118.  
  119. /**
  120.  * 方法: 添加事件监听 addListener()
  121.  */   
  122.     private void addListener() {
  123.         //添加窗口关闭事件
  124.         this.addWindowListener(new WindowAdapter(){
  125.             public void windowClosing(WindowEvent e){
  126.                 new JFrame().setVisible(false);
  127.                 dispose();
  128.                 System.exit(0);
  129.             }
  130.         });
  131.         
  132.         //显示密码
  133.         showPassword.addMouseListener(new MouseAdapter(){  
  134.             public void mousePressed(MouseEvent e){
  135.                 String txt = "张三 >>> " + table.getValueAt(0, 4) + "\n"
  136.                            + "李四 >>> " + table.getValueAt(1, 4) + "\n"
  137.                            + "王五 >>> " + table.getValueAt(2, 4) + "\n"
  138.                            + "啊明 >>> " + table.getValueAt(3, 4) + "\n"
  139.                            + "珊珊 >>> " + table.getValueAt(4, 4) + "\n";
  140.                show.setText(txt);
  141.             }
  142.         });
  143.     }
  144.  
  145.  
  146.  
  147. //----------------------------------------------------------//
  148.  
  149. /**
  150.  * 程序入口 main(String args[])
  151.  */
  152.     public static void main(String args[]) {
  153.         
  154.         //获取设置系统风格-------------------//
  155.         try {
  156.             String laf = UIManager.getSystemLookAndFeelClassName();
  157.             UIManager.setLookAndFeel(laf);
  158.         } catch (Exception e) {}
  159.         
  160.         //全局字体设置-----------------------//
  161.         Font font1 = new Font("宋体",Font.PLAIN,12);
  162.         Font font2 = new Font("宋体",Font.PLAIN,15);
  163.         UIManager.put("Button.font",font1);
  164.         UIManager.put("Table.font",font1);
  165.         UIManager.put("TableHeader.font",font1);
  166.         
  167.         //启动示例--------------------------//
  168.         new PasswordTable();
  169.     }
  170. }
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有