简体   繁体   中英

Convert Java program with TextField to swing JTextField

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();
    }

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM