Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8539913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:28:47+00:00 2026-06-11T11:28:47+00:00

So, I have a JTextArea. I have added keyboard action to it’s input/action maps.

  • 0

So, I have a JTextArea. I have added keyboard action to it’s input/action maps.

On enter press, JDialog is supposed to be created, along with it’s contents. And I need to add keyListener to a button it will contain, which i can’t, because that button doesn’t ahve final modifier. If I set it to final, I can’t edit it’s properties.

Here’s a snippet of the code:

class blabla extends JTextArea
{
getInputMap.put(KeyStroke.getKeyStroke("ENTER"), "pressedEnter");
getActionMap.put("pressedEnter", new AbstractAction()
        {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) 
            {
                JDialog dialog;
                JButton confirm;;

                //JDialog
                dialog = new JDialog(Main.masterWindow, "newTitle", true);
                dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));
                dialog.addWindowListener(new WindowAdapter()
                {
                    public void windowActivated(WindowEvent e)
                    {
                        //this doen't work, it asks me to declare confirm as final
                        //and I have to request focuse here due to Java bug
                        confirm.requestFocus();
                    }
                });

                //JButton for confirming
                confirm = new JButton(lang.getString("ok"));
                confirm.setAlignmentX(Component.CENTER_ALIGNMENT);

                confirm.addKeyListener(new KeyAdapter()
                {
                    @Override
                    public void keyPressed(KeyEvent e)
                    {
                        if (e.getKeyCode() == KeyEvent.VK_ENTER)
                        {
                            //this doen't work, it asks me to declare confirm as final
                            confirm.doClick();
                        }
                    }       
                });

                dialog.add(confirm);

                dialog.pack();
                dialog.setLocationRelativeTo(Main.masterWindow);
                dialog.setVisible(true);
}

How can I make this work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:28:48+00:00Added an answer on June 11, 2026 at 11:28 am
    • Option 1: make confirm a class field.
    • Option 2: You could create a dummy final JButton variable, final JButton finalConfirm = confirm; and pass in the confirm reference, and then work on this variable inside of the inner class.
    • Option 3: don’t use an anonymous inner class for your Key Binding’s AbstractAction, but rather a private inner class with a constructor that takes the JButton instance.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created JTextpane and inserted components inside textpane (components like Jtextarea). (vertical scrollbar
I have created a zend form. I have added some elements. I have put
JPanel(a).JPanel(b).JLabel JPanel(a).JTextArea I have added a KeyListener to JTextArea . When it gets invoked,
I have created LWUIT TextArea ,and I have added paragraphs of text to my
I have a JTextArea where lines are highlighted upon right-click. While I could achieve
I have a JTextArea which consists of lines (some of them possibly duplicates of
I have embedded a JTextArea on a JScrollPane and am using that JTextArea for
I have this class for my UI public class MyFrame extends JFrame{ JTextArea textArea;
I have textarea containing one or more URLs, along with other text content. I
I have textarea in my site(link shortening site) i want to user enter some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.