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

  • Home
  • SEARCH
  • 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 8737013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:25:27+00:00 2026-06-13T10:25:27+00:00

I am new to Java events, listeners and handlers. I can write code to

  • 0

I am new to Java events, listeners and handlers. I can write code to create a button click event and a working result. However, I cannot get a simple enter event within a TextField to work.

Notice I do declare and call action listeners, input handlers, and define a resulting method execution. (I import java.awt and javax.swing libraries not shown below.)

public convertStringToCapitalLetters() {
    setTitle("Convert String to All Capital Letters");
    Container c = getContentPane();
    c.setLayout(new GridLayout(2, 2));

    inputLabel = new JLabel("Enter String: ", SwingConstants.LEFT);
    stringTextField = new JTextField(50);
    outputLabel = new JLabel("Capitalized String: ", SwingConstants.LEFT);
    newStringLabel = new JLabel("", SwingConstants.RIGHT);

    c.add(inputLabel);
    c.add(stringTextField);
    c.add(outputLabel);
    c.add(newStringLabel);

    inputHandler = new InputHandler();

    stringTextField.addActionListener(inputHandler);

    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}

private class InputHandler implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        String str, newStr;

        str = stringTextField.getText();
        newStr = str.toUpperCase();

        newStringLabel.setText(String.format("", newStr));
    }
}

public static void main(String[] args) {
    convertStringToCapitalLetters capitalConv = new convertStringToCapitalLetters();
}
  • 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-13T10:25:28+00:00Added an answer on June 13, 2026 at 10:25 am

    I think you just made a very small mistake which is to forget to specify the placeholder %s in String.format()

    Try this:

    newStringLabel.setText(String.format("%s", newStr));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If we add event listener to a button in java like this : btn.setOnClickListener(new
I have the following code adding an ActionListener to a JTextField: chatInput.addMouseListener(new java.awt.event.MouseAdapter() {
I'm working on a little Java game in which all sorts of events can
As new to groovy... I'm trying to replace the java idiom for event listeners,
I tried this private void botaoConfIOMouseClicked(java.awt.event.MouseEvent evt) { ConfigurarIO popup = new ConfigurarIO(); popup.setVisible(true);
Eclipse and MyEclipse create new Java files with an extra blank line after the
im trying to setup two listeners for one button. Here is my code: This
I use the following code to listen to global key events: Win32HookManager.java import com.sun.jna.platform.win32.Kernel32;
I don't understand why the compiler doesn't accept this code import javax.swing.event.EventListenerList; import java.util.EventListener;
I am currently implementing custom events and listeners according to the code posted below.

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.