Hi Greg
Please find my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class FileInputDemo extends JFrame
implements ActionListener {
private JTextArea textArea;
private JButton openButton;
private JTextField nameField;
private JTextField dataRead;
private JLabel nameLabel;
public static void main (String [] args) {
FileInputDemo frame = new FileInputDemo();
frame.setSize(400, 300);
frame.createGUI();
frame.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
nameLabel = new JLabel("File name: ");
window.add(nameLabel);
nameField = new JTextField("C:\\Data.txt", 10);
window.add(nameField);
nameField.addActionListener(this);
dataRead = new JTextField(7);
window.add(dataRead);
textArea = new JTextArea("",10,30);
JScrollPane scrollPane = new JScrollPane(textArea);
window.add(scrollPane);
openButton = new JButton("open");
window.add(openButton);
openButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
java.io.File file = new java.io.File("C:\\Data.txt");
if (event.getSource() == openButton) {
try {
Scanner input = new Scanner(file);
do {
String num = input.next();
dataRead.setText(num);
} while (input.hasNext());
input.close();
}
catch (FileNotFoundException e) {
System.err.format("File does not exist");
}
}
}
}
--- Update ---
Hi Greg
Thank you for your help.
What I want to do is to read data from a data.txt file which contains one column of numbers with many rows.
such as:
23456
23458
23460
23455 and so on.
I want these rows of numbers to be displayed in a JTextField one by one so I can compare each row with a target number.
Please find below my program which have both a JTextField and a JTextArea and you may run to see.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Ken
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class FileInputDemo extends JFrame
implements ActionListener {
private JTextArea textArea;
private JButton openButton;
private JTextField nameField;
private JTextField dataRead;
private JLabel nameLabel;
public static void main (String [] args) {
FileInputDemo frame = new FileInputDemo();
frame.setSize(400, 300);
frame.createGUI();
frame.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
nameLabel = new JLabel("File name: ");
window.add(nameLabel);
nameField = new JTextField("C:\\Data.txt", 10);
window.add(nameField);
nameField.addActionListener(this);
dataRead = new JTextField(7);
window.add(dataRead);
textArea = new JTextArea("",10,30);
JScrollPane scrollPane = new JScrollPane(textArea);
window.add(scrollPane);
openButton = new JButton("open");
window.add(openButton);
openButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
java.io.File file = new java.io.File("C:\\Data.txt");
if (event.getSource() == openButton) {
try {
Scanner input = new Scanner(file);
do {
String num = input.next();
dataRead.setText(num);
textArea.append(num + "\n");
} while (input.hasNext());
input.close();
}
catch (FileNotFoundException e) {
System.err.format("File does not exist");
}
}
}
}