Java swing
如何实现文本域双击变为可编辑呢?
给文本域添加鼠标事件监听程序即可:
- resultTA1=new AssistPopupTextArea();
- resultTA1.setEditable(false);
- resultTA1.setLineWrap(true);
- resultTA1.setWrapStyleWord(true);
- resultTA1.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if (e.getClickCount() == 2) {
- if (!resultTA1.isEditable()) {
- resultTA1.setEditable(true);
- DialogUtil.focusSelectAllTF(resultTA1);
- resultTA1.requestFocus();
- }
- }
- super.mouseClicked(e);
- }
- });
按Esc 键变为只读(不可编辑)
- /***
- * 按Alt+Enter时,用户名文本框聚焦,<br>按Esc 文本框失去焦点,并变为不可编辑
- * @param tf :密码输入框
- */
- public static void addKeyListener22(final JTextComponent tf,final JTextComponent tf2){
- tf.addKeyListener(new KeyListener() {
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyReleased(KeyEvent e) {
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if ((e.getKeyCode() == KeyEvent.VK_ESCAPE)
- ) {
- // System.out.println("esc");
- tf.setEditable(false);
- }else if ((e.getKeyCode() == KeyEvent.VK_ENTER)
- && (((InputEvent) e )
- .isAltDown())) {
- if(!ValueWidget.isNullOrEmpty(tf2)){
- if (!tf2.isEditable()) {
- tf2.setEditable(true);
- }
- tf2.requestFocus();
- tf2.selectAll();
- }
- }
- }
- });
- }
调用:
- DialogUtil.addKeyListener22(resultTA1);
注意:
(1)e.getClickCount() == 2 表示双击,即短时间间隔之内连续点击两次;
(2)e.getKeyCode() == KeyEvent.VK_ESCAPE 表示按下Esc 键
参考:
http://hw1287789687.iteye.com/blog/2221256
http://hw1287789687.iteye.com/blog/2173977
http://hw1287789687.iteye.com/blog/2155557