public class Program {
public static void main(String[] args) {
ReadTxt reader = new ReadTxt();
}
}
class ReadTxt{
private Scanner scan= new Scanner(System.in);
public ReadTxt(){
Thread t= new Thread("askT");
GuiConsole guiCon= new GuiConsole(t);
char response=' ';
File f = new File("C:/name.txt");
try {
FileInputStream fis = new FileInputStream(f);
BufferedReader buff= new BufferedReader(new InputStreamReader(fis));
String str="";
while((str=buff.readLine())!=null){
guiCon.printText(str + " : is a name? (y / n)");
// response= scan.next().charAt(0);
synchronized (t) {
try {
t.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
response=guiCon.getResp().charAt(0);
switch(response){
case'y': guiCon.printText("Name !");
break;
default: guiCon.printText("no...");
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class GuiConsole {
private JFrame window;
private JScrollPane scroll;
private JTextArea console;
private JTextField answerField;
private JPanel textPanel;
private String resp = "";
private Thread wT = null;
public GuiConsole(Thread waitingT) {
window = new JFrame("GUI CONSOLE");
wT=waitingT;
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println("Error setting native LookAndFeel: ");
}
console = new JTextArea("Started:\n**********");
console.setLineWrap(true);
scroll = new JScrollPane(console);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setWheelScrollingEnabled(true);
answerField = new JTextField();
textPanel = new JPanel();
GridBagLayout layout = new GridBagLayout();
GridBagConstraints lim = new GridBagConstraints();
textPanel.setLayout(layout);
lim.insets.top = 10;
lim.insets.bottom = 5;
lim.insets.left = 10;
lim.insets.right = 10;
lim.fill = GridBagConstraints.BOTH;
lim.gridx = 0;
lim.gridy = 0;
lim.weightx = 1;
lim.weighty = 1;
layout.setConstraints(scroll, lim);
textPanel.add(scroll);
lim.insets.bottom = 30;
lim.gridx = 0;
lim.gridy = 1;
lim.weightx = 1;
lim.weighty = 0;
lim.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(answerField, lim);
textPanel.add(answerField);
answerField.addKeyListener(keyLis);
window.add(textPanel);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(new Dimension(500, 500));
window.setVisible(true);
}
private KeyListener keyLis = new KeyListener() {
@Override
public void keyTyped(KeyEvent t) {
}
@Override
public void keyReleased(KeyEvent r) {
}
@Override
public void keyPressed(KeyEvent p) {
if (p.getKeyCode() == KeyEvent.VK_ENTER) {
console.append("\n" + answerField.getText());
resp = answerField.getText();
answerField.setText("");
synchronized (wT) {
wT.notify();
}
}
};
};
public void printText(String m) {
console.append("\n" + m);
}
public String getResp() {
return resp;
}
}