How to close JTextArea by escape key2019 Community Moderator ElectionHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How do I determine whether an array contains a particular value in Java?How do I convert a String to an int in Java?How do I fix android.os.NetworkOnMainThreadException?add KeyListener to JLabelIs there a version of postActionEvent for KeyEvent (specifically for a JTextArea)?JTextArea editable or not editable depending on how it's called

Cycles on the torus

Short scifi story where reproductive organs are converted to produce "materials", pregnant protagonist is "found fit" to be a mother

How do spaceships determine each other's mass in space?

Is divide-by-zero a security vulnerability?

Can one live in the U.S. and not use a credit card?

Traveling to heavily polluted city, what practical measures can I take to minimize impact?

Writing text next to a table

Professor forcing me to attend a conference, I can't afford even with 50% funding

Why do phishing e-mails use faked e-mail addresses instead of the real one?

Is there a logarithm base for which the logarithm becomes an identity function?

If sound is a longitudinal wave, why can we hear it if our ears aren't aligned with the propagation direction?

"If + would" conditional in present perfect tense

The (Easy) Road to Code

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

Numerical value of Determinant far from what it is supposed to be

What does the Digital Threat scope actually do?

If nine coins are tossed, what is the probability that the number of heads is even?

What would be the most expensive material to an intergalactic society?

Do Paladin Auras of Differing Oaths Stack?

What can I do if someone tampers with my SSH public key?

Logistic regression BIC: what's the right N?

Create chunks from an array

School performs periodic password audits. Is my password compromised?

What does *dead* mean in *What do you mean, dead?*?



How to close JTextArea by escape key



2019 Community Moderator ElectionHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How do I determine whether an array contains a particular value in Java?How do I convert a String to an int in Java?How do I fix android.os.NetworkOnMainThreadException?add KeyListener to JLabelIs there a version of postActionEvent for KeyEvent (specifically for a JTextArea)?JTextArea editable or not editable depending on how it's called










-1















In my method bellow, the JTextArea will be displayed when i click on the button helpBut. After displaying the help text (the text written in JTextArea : helpText), i would like to close this text by pressing on the escape key. I tried to use the method addKeyListener (bellow), but it has not worked.



public void clickButton (JButton helpBut, JTextArea helpText)

helpBut.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
JPopupMenu helpPopup = new JPopupMenu();
helpPopup.add(helpText);
if (!helpPopup.isVisible())
helpPopup.show(helpBut,20,20);
else
helpPopup.setVisible(false);


);
helpText.addKeyListener(new KeyAdapter()
@Override
public void keyPressed(KeyEvent e)
if(helpText.isVisible())
if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
helpText.setVisible(false);


);



I figured out that the compiler calls the method addKeyListener only if i click on the JtextArea.










share|improve this question









New contributor




Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Use keybindings

    – Snowy_1803
    Mar 6 at 23:19






  • 2





    1. Don't use a MouseListener to listen for a button click. Instead the API is designed for you to use an ActionListener. 2) The escape key should be the default key to close the popup. At least is works by default for me. Post your Minimal, Complete, and Verifiable example that demonstrates the problem.

    – camickr
    Mar 7 at 0:49











  • Use helpBut.addActionListener(). ActionListener will be notified when the button is clicked.

    – Prasad Karunagoda
    2 days ago











  • @camickr why dont use MouseListener for a button click ? Do you mean using mousePressed is better ? I have one JPanel that content of the help button (helpBut), i click on this button and after that, i press on the JTextArea, both JPanel and JText will be closed in the same time, i would like to close JTextArea first, that is reason why i use addKeyListener in this method buttonClick.

    – Hitokiri
    2 days ago











  • Do you mean using mousePressed is better? - No. I said using an ActionLIstener is better. The clicking of a button, check box etc. was designed to be used with an ActionListener. Read the section from the Swing tutorial on [How to Use Buttons]() for more information and working examples. Your explanation of the problem still makes no sense to me. You are asking about a JTextArea in a popup menu, but then you talk about a panel closing. Unless you post a proper Minimal, Complete, and Verifiable example demonstrating the problem I can't help.

    – camickr
    2 days ago















-1















In my method bellow, the JTextArea will be displayed when i click on the button helpBut. After displaying the help text (the text written in JTextArea : helpText), i would like to close this text by pressing on the escape key. I tried to use the method addKeyListener (bellow), but it has not worked.



public void clickButton (JButton helpBut, JTextArea helpText)

helpBut.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
JPopupMenu helpPopup = new JPopupMenu();
helpPopup.add(helpText);
if (!helpPopup.isVisible())
helpPopup.show(helpBut,20,20);
else
helpPopup.setVisible(false);


);
helpText.addKeyListener(new KeyAdapter()
@Override
public void keyPressed(KeyEvent e)
if(helpText.isVisible())
if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
helpText.setVisible(false);


);



I figured out that the compiler calls the method addKeyListener only if i click on the JtextArea.










share|improve this question









New contributor




Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Use keybindings

    – Snowy_1803
    Mar 6 at 23:19






  • 2





    1. Don't use a MouseListener to listen for a button click. Instead the API is designed for you to use an ActionListener. 2) The escape key should be the default key to close the popup. At least is works by default for me. Post your Minimal, Complete, and Verifiable example that demonstrates the problem.

    – camickr
    Mar 7 at 0:49











  • Use helpBut.addActionListener(). ActionListener will be notified when the button is clicked.

    – Prasad Karunagoda
    2 days ago











  • @camickr why dont use MouseListener for a button click ? Do you mean using mousePressed is better ? I have one JPanel that content of the help button (helpBut), i click on this button and after that, i press on the JTextArea, both JPanel and JText will be closed in the same time, i would like to close JTextArea first, that is reason why i use addKeyListener in this method buttonClick.

    – Hitokiri
    2 days ago











  • Do you mean using mousePressed is better? - No. I said using an ActionLIstener is better. The clicking of a button, check box etc. was designed to be used with an ActionListener. Read the section from the Swing tutorial on [How to Use Buttons]() for more information and working examples. Your explanation of the problem still makes no sense to me. You are asking about a JTextArea in a popup menu, but then you talk about a panel closing. Unless you post a proper Minimal, Complete, and Verifiable example demonstrating the problem I can't help.

    – camickr
    2 days ago













-1












-1








-1


2






In my method bellow, the JTextArea will be displayed when i click on the button helpBut. After displaying the help text (the text written in JTextArea : helpText), i would like to close this text by pressing on the escape key. I tried to use the method addKeyListener (bellow), but it has not worked.



public void clickButton (JButton helpBut, JTextArea helpText)

helpBut.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
JPopupMenu helpPopup = new JPopupMenu();
helpPopup.add(helpText);
if (!helpPopup.isVisible())
helpPopup.show(helpBut,20,20);
else
helpPopup.setVisible(false);


);
helpText.addKeyListener(new KeyAdapter()
@Override
public void keyPressed(KeyEvent e)
if(helpText.isVisible())
if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
helpText.setVisible(false);


);



I figured out that the compiler calls the method addKeyListener only if i click on the JtextArea.










share|improve this question









New contributor




Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












In my method bellow, the JTextArea will be displayed when i click on the button helpBut. After displaying the help text (the text written in JTextArea : helpText), i would like to close this text by pressing on the escape key. I tried to use the method addKeyListener (bellow), but it has not worked.



public void clickButton (JButton helpBut, JTextArea helpText)

helpBut.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
JPopupMenu helpPopup = new JPopupMenu();
helpPopup.add(helpText);
if (!helpPopup.isVisible())
helpPopup.show(helpBut,20,20);
else
helpPopup.setVisible(false);


);
helpText.addKeyListener(new KeyAdapter()
@Override
public void keyPressed(KeyEvent e)
if(helpText.isVisible())
if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
helpText.setVisible(false);


);



I figured out that the compiler calls the method addKeyListener only if i click on the JtextArea.







java swing jtextarea keylistener






share|improve this question









New contributor




Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Mar 6 at 23:02









Dr Mido

840431




840431






New contributor




Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Mar 6 at 22:46









HitokiriHitokiri

12




12




New contributor




Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Use keybindings

    – Snowy_1803
    Mar 6 at 23:19






  • 2





    1. Don't use a MouseListener to listen for a button click. Instead the API is designed for you to use an ActionListener. 2) The escape key should be the default key to close the popup. At least is works by default for me. Post your Minimal, Complete, and Verifiable example that demonstrates the problem.

    – camickr
    Mar 7 at 0:49











  • Use helpBut.addActionListener(). ActionListener will be notified when the button is clicked.

    – Prasad Karunagoda
    2 days ago











  • @camickr why dont use MouseListener for a button click ? Do you mean using mousePressed is better ? I have one JPanel that content of the help button (helpBut), i click on this button and after that, i press on the JTextArea, both JPanel and JText will be closed in the same time, i would like to close JTextArea first, that is reason why i use addKeyListener in this method buttonClick.

    – Hitokiri
    2 days ago











  • Do you mean using mousePressed is better? - No. I said using an ActionLIstener is better. The clicking of a button, check box etc. was designed to be used with an ActionListener. Read the section from the Swing tutorial on [How to Use Buttons]() for more information and working examples. Your explanation of the problem still makes no sense to me. You are asking about a JTextArea in a popup menu, but then you talk about a panel closing. Unless you post a proper Minimal, Complete, and Verifiable example demonstrating the problem I can't help.

    – camickr
    2 days ago

















  • Use keybindings

    – Snowy_1803
    Mar 6 at 23:19






  • 2





    1. Don't use a MouseListener to listen for a button click. Instead the API is designed for you to use an ActionListener. 2) The escape key should be the default key to close the popup. At least is works by default for me. Post your Minimal, Complete, and Verifiable example that demonstrates the problem.

    – camickr
    Mar 7 at 0:49











  • Use helpBut.addActionListener(). ActionListener will be notified when the button is clicked.

    – Prasad Karunagoda
    2 days ago











  • @camickr why dont use MouseListener for a button click ? Do you mean using mousePressed is better ? I have one JPanel that content of the help button (helpBut), i click on this button and after that, i press on the JTextArea, both JPanel and JText will be closed in the same time, i would like to close JTextArea first, that is reason why i use addKeyListener in this method buttonClick.

    – Hitokiri
    2 days ago











  • Do you mean using mousePressed is better? - No. I said using an ActionLIstener is better. The clicking of a button, check box etc. was designed to be used with an ActionListener. Read the section from the Swing tutorial on [How to Use Buttons]() for more information and working examples. Your explanation of the problem still makes no sense to me. You are asking about a JTextArea in a popup menu, but then you talk about a panel closing. Unless you post a proper Minimal, Complete, and Verifiable example demonstrating the problem I can't help.

    – camickr
    2 days ago
















Use keybindings

– Snowy_1803
Mar 6 at 23:19





Use keybindings

– Snowy_1803
Mar 6 at 23:19




2




2





1. Don't use a MouseListener to listen for a button click. Instead the API is designed for you to use an ActionListener. 2) The escape key should be the default key to close the popup. At least is works by default for me. Post your Minimal, Complete, and Verifiable example that demonstrates the problem.

– camickr
Mar 7 at 0:49





1. Don't use a MouseListener to listen for a button click. Instead the API is designed for you to use an ActionListener. 2) The escape key should be the default key to close the popup. At least is works by default for me. Post your Minimal, Complete, and Verifiable example that demonstrates the problem.

– camickr
Mar 7 at 0:49













Use helpBut.addActionListener(). ActionListener will be notified when the button is clicked.

– Prasad Karunagoda
2 days ago





Use helpBut.addActionListener(). ActionListener will be notified when the button is clicked.

– Prasad Karunagoda
2 days ago













@camickr why dont use MouseListener for a button click ? Do you mean using mousePressed is better ? I have one JPanel that content of the help button (helpBut), i click on this button and after that, i press on the JTextArea, both JPanel and JText will be closed in the same time, i would like to close JTextArea first, that is reason why i use addKeyListener in this method buttonClick.

– Hitokiri
2 days ago





@camickr why dont use MouseListener for a button click ? Do you mean using mousePressed is better ? I have one JPanel that content of the help button (helpBut), i click on this button and after that, i press on the JTextArea, both JPanel and JText will be closed in the same time, i would like to close JTextArea first, that is reason why i use addKeyListener in this method buttonClick.

– Hitokiri
2 days ago













Do you mean using mousePressed is better? - No. I said using an ActionLIstener is better. The clicking of a button, check box etc. was designed to be used with an ActionListener. Read the section from the Swing tutorial on [How to Use Buttons]() for more information and working examples. Your explanation of the problem still makes no sense to me. You are asking about a JTextArea in a popup menu, but then you talk about a panel closing. Unless you post a proper Minimal, Complete, and Verifiable example demonstrating the problem I can't help.

– camickr
2 days ago





Do you mean using mousePressed is better? - No. I said using an ActionLIstener is better. The clicking of a button, check box etc. was designed to be used with an ActionListener. Read the section from the Swing tutorial on [How to Use Buttons]() for more information and working examples. Your explanation of the problem still makes no sense to me. You are asking about a JTextArea in a popup menu, but then you talk about a panel closing. Unless you post a proper Minimal, Complete, and Verifiable example demonstrating the problem I can't help.

– camickr
2 days ago












1 Answer
1






active

oldest

votes


















0














Thank you so much, i use keybindings like snowy_1803's advice, and it works.



helpPopup.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "closeJTextArea");
helpPopup.getActionMap().put("closeJTextArea", new AbstractAction()
@Override
public void actionPerformed(ActionEvent e)
helpPopup.setVisible(false);

);





share|improve this answer








New contributor




Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Hitokiri is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033406%2fhow-to-close-jtextarea-by-escape-key%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Thank you so much, i use keybindings like snowy_1803's advice, and it works.



    helpPopup.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "closeJTextArea");
    helpPopup.getActionMap().put("closeJTextArea", new AbstractAction()
    @Override
    public void actionPerformed(ActionEvent e)
    helpPopup.setVisible(false);

    );





    share|improve this answer








    New contributor




    Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.
























      0














      Thank you so much, i use keybindings like snowy_1803's advice, and it works.



      helpPopup.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "closeJTextArea");
      helpPopup.getActionMap().put("closeJTextArea", new AbstractAction()
      @Override
      public void actionPerformed(ActionEvent e)
      helpPopup.setVisible(false);

      );





      share|improve this answer








      New contributor




      Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















        0












        0








        0







        Thank you so much, i use keybindings like snowy_1803's advice, and it works.



        helpPopup.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "closeJTextArea");
        helpPopup.getActionMap().put("closeJTextArea", new AbstractAction()
        @Override
        public void actionPerformed(ActionEvent e)
        helpPopup.setVisible(false);

        );





        share|improve this answer








        New contributor




        Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        Thank you so much, i use keybindings like snowy_1803's advice, and it works.



        helpPopup.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "closeJTextArea");
        helpPopup.getActionMap().put("closeJTextArea", new AbstractAction()
        @Override
        public void actionPerformed(ActionEvent e)
        helpPopup.setVisible(false);

        );






        share|improve this answer








        New contributor




        Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 2 days ago









        HitokiriHitokiri

        12




        12




        New contributor




        Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Hitokiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















            Hitokiri is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            Hitokiri is a new contributor. Be nice, and check out our Code of Conduct.












            Hitokiri is a new contributor. Be nice, and check out our Code of Conduct.











            Hitokiri is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033406%2fhow-to-close-jtextarea-by-escape-key%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme