Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java-How to extend InputStream to read from a JTextField?

Working on a project I got into running java applications through a small console-like window. Thanks to the wonderful community in here I managed to solve the problem with outputting the data from a proccess but my command-line applications running will constantly give errors as there is no input stream.

Based on the last helpful reply in that thread I suppose I shall approach similarly the JTextFieldInputStream extends InputStream implementation, but looking in the javadocs and throughout google and the internet for some class that does just that I really found nothing explaining how to do this.

So I am asking for some link, example, tutorial, sample code for it just like in the previous topic. Give me just a class that extends InputStream and can be extended to read from a JTextField when I press Enter and I will do the rest with implementing this and making it work! Thanks in advance!

like image 337
Angelos Chalaris Avatar asked Dec 15 '22 18:12

Angelos Chalaris


1 Answers

What I don't understand if why you need a JTextField that extends InputStream? Basically, what you are looking for is:

  1. Add an ActionListener on the JTextField (ie, when use presses Enter, actionPerformed will be invoked)
  2. You need to grab the text of the JTextField using getText()
  3. You can then "transform" the String text to an InputStream with new ByteArrayInputStream(text.getBytes("UTF-8"));

Here is a small snippet that should get you the basic idea:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestTextField {

    private void initUI() {
        JFrame frame = new JFrame(TestTextField.class.getSimpleName());
        frame.setLayout(new FlowLayout());
        final JTextField textfield = new JTextField(20);
        textfield.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String text = textfield.getText();
                    InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
                    // Here do something with your input stream (something non-blocking)
                    System.err.println(text);
                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                }

            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(textfield);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTextField().initUI();
            }
        });
    }

}
like image 122
Guillaume Polet Avatar answered Dec 30 '22 20:12

Guillaume Polet