So, I was wondering if Scanner could read from System.in that is set from JFrame. This is what I mean.
This is my WriteToSystemIn (JFrame class), which is the GUI part of the program.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
public class WriteToSystemIn extends JFrame {
private static class ChangeNumber implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
ByteArrayInputStream s = null;
try {
s = new ByteArrayInputStream("1\n".getBytes("UTF-8"));
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
System.setIn(s);
}
}
WriteToSystemIn() {
JButton button = new JButton("try click it m8");
button.addActionListener(new ChangeNumber());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(button);
this.setVisible(true);
this.pack();
}
}
And this is the Main function of the program.
import java.util.Scanner;
public class Main {
private static class MainRunnable implements Runnable {
@Override
public void run() {
new WriteToSystemIn();
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new MainRunnable());
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
System.out.println(s);
System.out.println("ended");
}
}
So, when the button is pressed from WriteToSystemIn, it should write "1\n" to System.in for Scanner to read.
But, it isn't doing that. It won't read anything. It has no problem printing to System.out so I thought it would've been a non-issue, but clearly I'm wrong. So, I'm wondering here, is there something that I'm doing wrong here? Or, am I trying to do something that is impossible?
Continuing on from @Torben's answer, you need to make class
Mainwait until theJButton(in classWriteToSystemIn) is clicked. Once theJButtonis clicked, you can notifyMainthat it can stop waiting and continue executing.Class
MainClass
WriteToSystemIn