栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

选择密码行并粘贴到记事本中会显示密码

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

选择密码行并粘贴到记事本中会显示密码

您想要的是相当合乎逻辑的。从表中复制粘贴数据时,应遵循呈现方式。

标准复制操作将使用模型中可用的数据,其中密码以纯文本形式提供。我们可以开始讨论是否要让模型包含纯文本形式的密码,而没有对其应用任何哈希运算法则…但是那不能回答您的问题。

对于您的问题,您应该修改的剪切/复制操作的行为

JTable
。查看拖放式Swing教程,更具体地看一下Adding
Cut,Copy和Paste部分
。不幸的是,我没有立即找到要引用的示例。

编辑

在下面找到一个示例,该示例

JXTable
使用呈现值进行复制操作(我没有复制粘贴导入内容)。关于代码的一点说明:

  1. 它也使用SwingX来说明kleopatra
  2. 该示例
    TableModel
    及其元素非常愚蠢。为了避免过多的工作,我需要与我们的真实产品相似的东西,以便能够复制一些代码
  3. SpeedStringValue
    AltitudeStringValue
    类违反了
    StringValue
    他们返回接口
    null
    。我懒得定义一个新接口,并且
    StringValue
    我在SwingX上设置的实例的
    DefaultTableRenderer
    行为与文档一致。但是,我认为拥有单独的
    StringValue
    实例(每个实例都具有将特定类转换为实例的知识)
    String
    是现实世界中的用例,
    SwingX
  4. TransferHandler
    重用
    StringValue
    创建一个逻辑
    Table
    只包含
    String
    实例,然后倒在默认的
    JTable
    行为。这允许重用渲染器中实现的逻辑,并允许复制视觉值而不是模型值。我不确定这是否是最好的解决方案,但是它可以工作。但是,如果在中有类似行为是标准的
    SwingX
    ,那就太好了,因为他们已经有了基础架构
  5. 该代码缺少注释,因为它已经足够长了。如果不清楚,请发表评论,我会尽力澄清
        public class TableTransferHandlerDemo {      public static void main( String[] args ) throws InvocationTargetException, InterruptedException {        EventQueue.invokeAndWait( new Runnable() {          public void run() { Jframe frame = new Jframe( "Testframe" ); JPanel contentPane = new JPanel( new BorderLayout(  ) ); contentPane.add( createTable(), BorderLayout.CENTER ); frame.getContentPane().add( contentPane ); frame.setDefaultCloseOperation( Jframe.EXIT_ON_CLOSE ); frame.pack(); frame.setVisible( true );          }        } );      }      private static CompositeStringValue createStringValue() {        CompositeStringValue stringValue = new CompositeStringValue();        stringValue.delegates.add( new AltitudeStringValue() );        stringValue.delegates.add( new SpeedStringValue() );        return stringValue;      }      public static JXTable createTable(){        final JXTable table = new JXTable(  );        table.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );        table.setModel( createTableModel() );        CompositeStringValue stringValue = createStringValue();        table.setDefaultRenderer( Object.class, new DefaultTableRenderer( stringValue ) );        table.setTransferHandler( new TableTransferHandler( table, stringValue ) );        //make sure ctrl-c triggers a copy        InputMap inputMap = table.getInputMap( JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPonENT );        inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_C, InputEvent.CTRL_MASK ), "copyAction" );        table.getActionMap().put( "copyAction", new AbstractAction() {          public void actionPerformed( ActionEvent e ) { ActionEvent event = new ActionEvent( table, e.getID(), e.getActionCommand(), e.getWhen(), e.getModifiers() ); TransferHandler.getCopyAction().actionPerformed( event );          }        } );        return table;      }      public static class Speed{        public double speed;        public String unit = "km/h";        public Speed( double speed ){ this.speed = speed;}      }      public static class Altitude{        public double altitude;        public String unit = "m";        public Altitude( double altitude ){ this.altitude = altitude; }      }      public static class SpeedStringValue implements StringValue{        public String getString( Object o ) {          if ( o instanceof Speed ){ return ( ( Speed ) o ).speed + ( ( Speed ) o ).unit;          }          return null;        }      }      public static class AltitudeStringValue implements StringValue{        public String getString( Object o ) {          if ( o instanceof Altitude ){ return ( ( Altitude ) o ).altitude + ( ( Altitude ) o ).unit;          }          return null;        }      }      public static class CompositeStringValue implements StringValue{        public List<StringValue> delegates = new ArrayList<StringValue>(  );        public String getString( Object o ) {          for ( StringValue stringValue : delegates ) { String string = stringValue.getString( o ); if ( string != null ) return string;          }          return o != null ? o.toString() : "null";        }      }      public static TableModel createTableModel(){        return new DefaultTableModel( new Object[][]{ new Object[]{ new Speed( 10 ), new Altitude( 100 )},     new Object[]{ new Speed( 20 ), new Altitude( 200 ) }}, new Object[]{"Speed", "Altitude"} );      }      public static class TableTransferHandler extends TransferHandler{        private JXTable table;        private StringValue stringValue;        public TableTransferHandler( JXTable aTable, StringValue aStringValue ) {          table = aTable;          stringValue = aStringValue;        }        @Override        public void exportToClipboard( JComponent aComponent, Clipboard aClipboard, int aAction ) throws IllegalStateException {          JTable table = createTable();          table.getTransferHandler().exportToClipboard( table, aClipboard, aAction );        }        @Override        public void exportAsDrag( JComponent aComponent, InputEvent aEvent, int aAction ) {          JTable table = createTable();          table.getTransferHandler().exportAsDrag( table, aEvent, aAction );        }        @Override        protected Transferable createTransferable( JComponent c ) {          //this transfer handler should not create any transferables          return null;        }                private JTable createTable() {          JTable table = new JTable( new StringTableModel( this.table, stringValue ) );          table.setSelectionModel( this.table.getSelectionModel() );//make sure the selection is synced          return table;        }      }      private static class StringTableModel extends AbstractTableModel {        private JXTable delegateTable;        private StringValue stringValue;        private StringTableModel( JXTable aTable, StringValue aStringValue ) {          delegateTable = aTable;          stringValue = aStringValue;        }        public int getRowCount() {          return delegateTable.getModel().getRowCount();        }        public int getColumnCount() {          return delegateTable.getModel().getColumnCount();        }        public Object getValueAt( int aRowIndex, int aColumnIndex ) {          return stringValue.getString( delegateTable.getValueAt( aRowIndex, aColumnIndex ) );        }      }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/570105.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号