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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:22:13+00:00 2026-06-05T14:22:13+00:00

This piece of code is a simplified version of a program I would convert

  • 0

This piece of code is a simplified version of a program I would convert to swing (using JTextField and DocumentListener). I have read some tutorials but I can’t do it…
I shouldn’t use global variables and I have to use some like getSource() (getDocument() in this case?), because in the original program the number of JTextField is variable (they are generated inside a for, so they haven’t a “name”). This number depends on a value written in a text file.

import java.awt.*;
import java.awt.event.*;

class TestWindow extends Frame {
    public TestWindow() {
        Panel p = new Panel(new FlowLayout());
        Label l = new Label("Temp");
        TextField tf1 = new TextField();
        TextField tf2 = new TextField();
        tf1.addTextListener(new myTextListener(l));
        tf2.addTextListener(new myTextListener(l));
        p.add(tf1);
        p.add(tf2);
        tf1.setColumns(10);
        tf2.setColumns(10);
        p.add(l);
        add(p);
        pack();
        setVisible(true);
    }

    class myTextListener implements TextListener {
        Label input;
        myTextListener(Label input) {
            this.input = input;
        }
        public void textValueChanged(TextEvent e) {
            input.setText(((TextField)(e.getSource())).getText());
        }
    }

}

public class Test {

    public static void main(String[] args) {
        new TestWindow();
    }

}
  • 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-05T14:22:14+00:00Added an answer on June 5, 2026 at 2:22 pm

    This is a direct conversion of the code you posted to Swing that performs exactly the same task:

    import javax.swing.*;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.text.BadLocationException;
    import java.awt.FlowLayout;
    
    public class TestWindow extends JFrame {
    
    public TestWindow() {
        JPanel p = new JPanel(new FlowLayout());
        JLabel l = new JLabel("Temp");
        JTextField tf1 = new JTextField(10);
        JTextField tf2 = new JTextField(10);
        tf1.getDocument().addDocumentListener(new MyDocumentListener(l));
        tf2.getDocument().addDocumentListener(new MyDocumentListener(l));
        p.add(tf1);
        p.add(tf2);
        p.add(l);
        add(p);
        pack();
        setVisible(true);
    }
    
    class MyDocumentListener implements DocumentListener{
    
        private JLabel label;
    
        MyDocumentListener(JLabel label) {
            this.label = label;
        }
    
        @Override
        public void insertUpdate(DocumentEvent e) {
            handleTextChange(e);
        }
    
        @Override
        public void removeUpdate(DocumentEvent e) {
            handleTextChange(e);
        }
    
        @Override
        public void changedUpdate(DocumentEvent e) {
            handleTextChange(e);
        }
    
        private void handleTextChange(DocumentEvent e) {
            try {
                label.setText(e.getDocument().getText(0,e.getDocument().getLength()));
            } catch (BadLocationException ignored) {
                //todo: handle exception properly although this should never happen
            }
        }
    }
    
    public static void main(String[] args) {
        new TestWindow();
    }
    
    }
    

    Please note that DocumentListener provides more control for handling text change events than the TextListener, but I chose to handle them with one single method in order to exactly match your example’s functionality

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This piece of code is supposed to go through a list and preform some
I have a piece of code (simplified): if(reentrantLockObject.isLocked()) { reentrantLockObject.unlock(); } where reentrantLockObject is
I have extracted this piece of code from my project, because I was trying
I have a piece of code that looks something like this (ClearImportTable and InsertPage
Here's a piece of simplified code: static Activity longLivedField; onCreate(...) { longLivedField = this;
This piece of code has previously worked but I have C&P it to a
I have a setInterval running a piece of code 30 times a second. This
I have a piece of Perl code somewhat like the following (strongly simplified): There
This piece of code compiles and runs as expected on GCC 3.x and 4.x:
This piece of code is from Adobe docs : package { import flash.display.Loader; import

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.