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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:30:22+00:00 2026-06-03T11:30:22+00:00

I want to change the input in the textfield which will instantly update to

  • 0

I want to change the input in the textfield which will instantly update to the display, instead of press ENTER button to update it.

Here is my code.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyProgram01 extends JFrame
{
    private JTextField text1;
    private JCheckBox check1;
    private JCheckBox check2;
    private String message;
    private JLabel label1;
    private JLabel label2;
    private Font font;

    public MyProgram01(String title)
    {
        super(title);
        check1 = new JCheckBox("Bold");
        check2 = new JCheckBox("Italics");

        label1 = new JLabel("Text : ");
        label2 = new JLabel("Style : ");
        message = "Good Morning...";
        text1 = new JTextField(message, 100);
        font = new Font("Times New Roman", Font.PLAIN, 36);
        setBounds(0, 0, 600, 300);

        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(0, 0, 600, 120);
        panel.setBackground(Color.ORANGE);
        label1.setFont(new Font("Times New Roman", Font.PLAIN, 36));
        label1.setBounds(15, 15, 100, 36);
        panel.add(label1);
        text1.setBounds(120, 15, 400, 36);
        panel.add(text1);

        label2.setFont(new Font("Times New Roman", Font.PLAIN, 36));
        label2.setBounds(15, 65, 100, 36);
        panel.add(label2);
        check1.setBounds(120, 65, 100, 36);
        check2.setBounds(220, 65, 100, 36);
        panel.add(check1);
        panel.add(check2);

        check1.addActionListener(new CheckBoxListener());
        check2.addActionListener(new CheckBoxListener());
        text1.addActionListener(new TextFieldListener());

        setLayout(null);
        add(panel);
    }
    public void paint(Graphics g)
    {
        super.paint(g);

        g.setFont(font);
        g.drawString(message, 15, 255);

    }
    private class CheckBoxListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if(check1.isSelected() && check2.isSelected())
            {
                font = new Font("Times New Roman", Font.BOLD + Font.ITALIC, 36);    
                repaint();
            }
            else if(check1.isSelected())
            {
                font = new Font("Times New Roman", Font.BOLD, 36);
                repaint();
            }
            else if(check2.isSelected())
            {
                font = new Font("Times New Roman", Font.ITALIC, 36);
                repaint();
            }
            else
            {
                font = new Font("Times New Roman", Font.PLAIN, 36);
                repaint();
            }
        }
    }
    private class TextFieldListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            message = text1.getText();
            repaint();
        }
    }
    public static void main(String[] args)
    {
        JFrame frame = new MyProgram01("My Program 01");
        frame.setVisible(true);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}    

How can I change the code to instantly update to the display?

EDIT :

It’s work with the keyListener, but my program will only start display after second key is pressed.

For example, I key in abc, the program will start show a when I press b, and when I pressed c, it displays ab and c is missing, unless I press ENTER.

Here the part of the code :

text1.addKeyListener(new KeyAdapter()
    {
            public void keyPressed(KeyEvent e)
            {
                message = text1.getText();
                repaint();
            }
    });
  • 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-03T11:30:26+00:00Added an answer on June 3, 2026 at 11:30 am

    Add a KeyListener to your textfield.
    You can do that like this:

    textField.addKeyListener(new KeyAdapter(){
        @Override
        public void keyPressed(KeyEvent e){
             message = textField.getText();
             repaint();
        }
    });     
    

    OR

    Add a DocumentListener to your textfield’s Document.
    You can do that like this:

    private JFrame getFrame(){
        return this;
    }
    ...
    
    textField.getDocument().addDocumentListener(new DocumentListener(){
    
        @Override
        public void insertUpdate(DocumentEvent e) {
            message = textField.getText();
            getFrame().repaint();
        }
    
        @Override
        public void removeUpdate(DocumentEvent e) {
            message = textField.getText();
            getFrame().repaint();
        }
    
        @Override
        public void changedUpdate(DocumentEvent e) {
            // on change
        }
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to change the number of tableviews depending on my input, which will
I want to restrict input to match the statement change = where word and
The documentation says to use: $(input[type='checkbox']).bind( change, function(event, ui) { But I want to
i want change the content of a UITableView when i change a button, how
I want change following javascript code to jquery code, How is it? With done
I have a RadioButtnList. I want change body background color based on radio button
I am saving items to JComboBox from a textfield(input) when a button is clicked.
I want to have a JTextField (or another text-like input which supports focus and
I just want to fire an event when an input changes value using jQuery
I want to edit links when the user changes the value of a <input

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.