从java中的其他帧重新关注textField

时间:2013-11-26 17:09:12

标签: java jframe

我有两个JFrames,当点击父textField中的form时,form会显示一个小focus textField,在当前form消失的过程中,我希望focus返回到父表单的textField,这在下面的图片中有解释,我该怎么做?

IMAGE

enter image description here

我做了什么,试图通过requestFocus()方法将重点放在父表单的textField上,如下所示!

CODE FOR SMALL WINDOW

quantityField.addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER && priceField.getText().length() > 0) {
                if (containsOnlyNumbers(priceField.getText())) {
                    productPrice = Double.parseDouble(priceField.getText());
                    productQuantity = Double.parseDouble(quantityField.getText());
                    TaxInfo tax = taxeslogic.getTaxInfo(oProduct.getTaxCategoryID(), m_oTicket.getCustomer());
                    addTicketLine(new TicketLineInfo(oProduct, productQuantity, productPrice, tax, (java.util.Properties) (oProduct.getProperties().clone())));
                    status = true;
                    jProductList.requestFocusInWindow();
                    frame.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "Invalid value entered!", "Error", JOptionPane.ERROR_MESSAGE);
                }
            } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                TaxInfo tax = taxeslogic.getTaxInfo(oProduct.getTaxCategoryID(), m_oTicket.getCustomer());
                addTicketLine(new TicketLineInfo(oProduct, dMul, dPrice, tax, (java.util.Properties) (oProduct.getProperties().clone())));
                status = true;
                jProductList.requestFocusInWindow();
                frame.dispose();
            }
        }
    });

父母表格代码

jProductList.getEditor().getEditorComponent().addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                if (jProductList.getSelectedIndex() < 0) {
                    //Do nothing
                } else {
                    //Add product into list
                    m_returnObj = (ProductInfoExt) product.getProductDetail(product.getProductCode(jProductList.getSelectedItem().toString()));
                    jProductList.setSelectedIndex(-1);
                    if (addTicketLine(m_returnObj, 1.0, m_returnObj.getPriceSell())) {
                        System.out.println("Requesting focus");
                        jProductList.requestFocusInWindow();
                    }
                    //jProductList.requestFocus();
                }
            }
        }
    });

WHERE jProductList是父表单的textField我需要关注的重点!

1 个答案:

答案 0 :(得分:0)

  

我有两个JFrame,

应用程序应该只有一个JFrame。在您的情况下,子窗口应为modal JDialog

  

在父表单中单击textField时会出现一个小表单

不要使用KeyListener。

您应该将ActionListener添加到父窗体的文本字段中。按下Enter键时将调用ActionListener。

当关闭模态对话框时,焦点将自动返回到文本字段。

在子表单中,您不应再使用KeyListener。您可以再次向文本字段添加ActionListener。要处理Escape键,您应该使用Key Bindings