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
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
New contributor
add a comment |
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
New contributor
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 anActionListener
. 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
UsehelpBut.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
add a comment |
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
New contributor
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
java swing jtextarea keylistener
New contributor
New contributor
edited Mar 6 at 23:02
Dr Mido
840431
840431
New contributor
asked Mar 6 at 22:46
HitokiriHitokiri
12
12
New contributor
New contributor
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 anActionListener
. 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
UsehelpBut.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
add a comment |
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 anActionListener
. 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
UsehelpBut.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
add a comment |
1 Answer
1
active
oldest
votes
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);
);
New contributor
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
);
New contributor
add a comment |
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);
);
New contributor
add a comment |
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);
);
New contributor
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);
);
New contributor
New contributor
answered 2 days ago
HitokiriHitokiri
12
12
New contributor
New contributor
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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