1我想实现如下效果:
文本框默认是置灰的,不可编辑,双击变为可编辑,按Esc又恢复为不可编辑.
实现:
- //双击变为可以编辑
- portTextField.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if (e.getClickCount() == 2) {
- if (!portTextField.isEditable()) {
- portTextField.setEditable(true);
- DialogUtil.focusSelectAllTF(portTextField);
- }
- // System.out.println("Double Click!");
- }
- super.mouseClicked(e);
- }
- });
- DialogUtil.addKeyListener22(portTextField);
DialogUtil.addKeyListener22方法实现:
- /***
- * 按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();
- }
- }
- }
- });
- }
- /***
- * 按Esc 文本框失去焦点
- * @param tf
- */
- public static void addKeyListener22(final JTextComponent tf){
- addKeyListener22(tf, null);
- }
双击前:
双击后: