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 7758251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:18:38+00:00 2026-06-01T13:18:38+00:00

I want to create a form in which if the User presses Enter the

  • 0

I want to create a form in which if the User presses Enter the form is submitted. The problem is that this form has many components each having its own event handlers.

I wanted to create a KeyListener for the entire panel but the event gets consumed by the components of the panel, and does not reach the panel.

Currently I have added a KeyListener, to each of the components, listening the Enter key. Is there a better way out of this?

Thanks.

  • 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-01T13:18:39+00:00Added an answer on June 1, 2026 at 1:18 pm
    • use getRootPane().setDefaultButton(myButton);

    • KeyListener isn’t designated for Swing JComponents, you have look as to use KeyBindings

    SSCCE code

    import java.awt.event.*;
    import javax.swing.*;
    
    public class ButtonTest {
    
        private JFrame frame;
    
        public void createAndShowGUI() {
            frame = new JFrame();
            frame.add(new JButton("Please press ENTER"));
            frame.setLocation(150, 150);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
            setKeyBindings();
        }
    
        private void setKeyBindings() {
            frame.getRootPane().getInputMap(
                    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                    KeyStroke.getKeyStroke("ENTER"), "clickENTER");
            frame.getRootPane().getActionMap().put("clickENTER", new AbstractAction() {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new ButtonTest().createAndShowGUI();
                }
            });
        }
    }
    

    * code *

    import java.awt.event.*;
    import java.awt.Color;
    import java.awt.AlphaComposite;
    import javax.swing.*;
    import javax.swing.UIManager.LookAndFeelInfo;
    
    public class ButtonTest {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new ButtonTest().createAndShowGUI();
                }
            });
        }
        private JFrame frame;
        private SoftJButton softButton1;
    
        public void createAndShowGUI() {
            softButton1 = new SoftJButton("Transparent Button");
            frame = new JFrame();
            frame.add(softButton1);
            frame.setLocation(150, 150);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
            Timer alphaChanger = new Timer(50, new ActionListener() {
    
                private float incrementer = -.03f;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    float newAlpha = softButton1.getAlpha() + incrementer;
                    if (newAlpha < 0) {
                        newAlpha = 0;
                        incrementer = -incrementer;
                    } else if (newAlpha > 1f) {
                        newAlpha = 1f;
                        incrementer = -incrementer;
                    }
                    softButton1.setAlpha(newAlpha);
                }
            });
            alphaChanger.start();
            Timer uiChanger = new Timer(5500, new ActionListener() {
    
                private LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels();
                private int index = 1;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        UIManager.setLookAndFeel(laf[index].getClassName());
                        SwingUtilities.updateComponentTreeUI(frame);
                    } catch (Exception exc) {
                        exc.printStackTrace();
                    }
                    index = (index + 1) % laf.length;
                }
            });
            uiChanger.start();
            setKeyBindings();
        }
    
        private void setKeyBindings() {
            frame.getRootPane().getInputMap(
                    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                    KeyStroke.getKeyStroke("ENTER"), "clickENTER");
            frame.getRootPane().getActionMap().put("clickENTER", new AbstractAction() {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
        }
    
        public static class SoftJButton extends JButton {
    
            private static final JButton lafDeterminer = new JButton();
            private static final long serialVersionUID = 1L;
            private boolean rectangularLAF;
            private float alpha = 1f;
    
            public SoftJButton() {
                this(null, null);
            }
    
            public SoftJButton(String text) {
                this(text, null);
            }
    
            public SoftJButton(String text, Icon icon) {
                super(text, icon);
    
                setOpaque(false);
                setFocusPainted(false);
            }
    
            public float getAlpha() {
                return alpha;
            }
    
            public void setAlpha(float alpha) {
                this.alpha = alpha;
                repaint();
            }
    
            @Override
            public void paintComponent(java.awt.Graphics g) {
                java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
                if (rectangularLAF && isBackgroundSet()) {
                    Color c = getBackground();
                    g2.setColor(c);
                    g.fillRect(0, 0, getWidth(), getHeight());
                }
                super.paintComponent(g2);
            }
    
            @Override
            public void updateUI() {
                super.updateUI();
                lafDeterminer.updateUI();
                rectangularLAF = lafDeterminer.isOpaque();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to create a form that has two submit buttons, but each one
I want to create a form which has a text entry box where a
I want to create a form for my user which shows a drop down
I want to create a web form which will allow the user to upload
I want to create a javascript form that basically has a bunch of text
I want to create an input form that will take the input text and
I want to create a simple user registration form with First / Last name,
I am trying to create a form which loads upon a user clicking a
I have Django Model with many fields which user must fill. If I'll create
I have got a form which a user can use to create a new

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.