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

            Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

            Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

            How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page