[BLUEJ-117] Terminal doesn't accept and pass input unless main execution thread is running
When a program creates a GUI (eg with a button), and the button invokes a method that attempts to read from System.in via a Scanner, then the system deadlocks. It appears that the Terminal window is run in the GUI thread, but the GUI thread hangs while waiting for user input, which prevents the Terminal window from accepting user input. This means that programs that work correctly when run from the command line do not work when run inside BlueJ.
If this is a correct diagnosis, then it may be necessary to spawn the terminal window in a new thread rather than the GUI thread.
I have included a sample program that causes this behaviour:
{noformat} import java.util.; import javax.swing.; import java.awt.event.*; import java.awt.BorderLayout;
/** Appears to cause deadlock: the terminal window is unable to respond to user input */
public class BluejBug implements ActionListener{ private JFrame frame = new JFrame("BluejBug");
public BluejBug(){
frame.setSize(100, 100);
JButton button = new JButton("Ask");
button.addActionListener(this);
frame.getContentPane().add(button, BorderLayout.CENTER);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e){
Scanner scan = new Scanner(System.in);
System.out.print("Enter name: ");
String name = scan.nextLine();
System.out.println("Hello "+name);
frame.dispose();
}
public static void main(String[] arguments){
new BluejBug();
}
} {noformat}
Issue metadata
- Issue type: Bug
- Priority: Medium