Java-How to extend InputStream to read from a JTextField?
Solution 1
How about this implementation
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JTextField;
public class JTextFieldInputStream extends InputStream {
byte[] contents;
int pointer = 0;
public JTextFieldInputStream(final JTextField text) {
text.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyChar()=='\n'){
contents = text.getText().getBytes();
pointer = 0;
text.setText("");
}
super.keyReleased(e);
}
});
}
@Override
public int read() throws IOException {
if(pointer >= contents.length) return -1;
return this.contents[pointer++];
}
}
to use this input stream, do the following
InputStream in = new JTextFieldInputStream( someTextField );
char c;
while( (c = in.read()) != -1){
//do whatever with c
}
does it read only when I hit enter?
it reads when you call in.read()
if the return value -1
it means end of the stream
(And will I be able to modify so that the Enter key empties the JTextField?)
you need to add an action listener and this functionality has nothing to do with the job of input stream
Solution 2
What I don't understand if why you need a JTextField that extends InputStream? Basically, what you are looking for is:
- Add an
ActionListener
on theJTextField
(ie, when use presses Enter,actionPerformed
will be invoked) - You need to grab the
text
of the JTextField usinggetText()
- You can then "transform" the
String text
to anInputStream
withnew 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();
}
});
}
}
Related videos on Youtube
Angelos Chalaris
I'm Angelos Chalaris, a web developer from Athens, Greece. Programming is not just a career path for me, but also a hobby and a passion of mine. I started coding sometime around 2011, which is when I started studying Computer Science in the University of Piraeus. After getting a BSc in Computer Science, I started studying for a MSc in Advanced Information Systems with a concentration in Advanced Software Development Technologies. After a few years of working with Java, C# and Python, I took an interest in web and mobile development. Lately, I am particularly interested in Javascript and Node.js, along with many of the libraries and frameworks developed for this ecosystem, as well as things such as progressive web apps and web applications.
Updated on September 14, 2022Comments
-
Angelos Chalaris over 1 year
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!
-
Guillaume Polet over 11 years@Spiritios Havaing multiple ActionListener is not a problem. It is also possible that the ActionListener you have, is actually the same as the one I am mentionning in my answer. I am almost 99% sure that this is the way to go, so you are probably overlooking ;-).
-
Angelos Chalaris over 11 yearsHmmm ok... But I am using Apache Commons PumpStreamHandler and basically I need an InputStream for it that will be passed to the constructor in order to be used by the subprocess I call so can this be done using Listeners really?? :O
-
Guillaume Polet over 11 years@Spiritios I added a sample code that should give you the basic idea on how to go. In the action listener, you can call your Apache commons pumpstreamhandler etc...