Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Output in JTextFiled with refreshing every row

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Output in JTextFiled with refreshing every row

    I am a beginner in Java and had started writing a program to read text from a file and want to output each row to JTextField.
    I want to compare each row of number so I need a field where each row of data keeps refreshing.
    I can do it with Visual Basic with loop, Me.refresh, and system.thread.sleep. But I need to achieve it with Java.

    I use System.out.println(num); which showed all rows of the file.

    When I use textArea.append(num + "\n"); to output it, I only got all rows displayed after it reached the end. (textArea = new JTextArea("",10,30);

    If I use dataRead.setText(num); to output it, I only got the last row displayed after it reached the end. dataRead = new JTextField(7);

    Can anyone help?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Output in JTextFiled with refreshing every row

    Confused as to whether you're using a JTextField or a JTextArea. Show the code (as little as possible) that isn't performing as desired and describe current and desired behavior. If showing a little code and describing a lot is too hard, then provide a short, simple example of code that demonstrates the current behavior with a description of what you'd like it to be.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Output in JTextFiled with refreshing every row

    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");
    		} 
                }
            }
       }

Similar Threads

  1. Replies: 2
    Last Post: December 13th, 2013, 12:01 AM
  2. [SOLVED] Refreshing a JTabbedPane?
    By Hallowed in forum Java Theory & Questions
    Replies: 2
    Last Post: May 31st, 2011, 01:02 PM
  3. Re: setVariable to Swing JTextfiled?
    By aknessy in forum Java Theory & Questions
    Replies: 1
    Last Post: April 30th, 2011, 10:42 AM
  4. setVariable to Swing JTextfiled?
    By Frankly3D in forum AWT / Java Swing
    Replies: 1
    Last Post: April 6th, 2011, 08:52 AM
  5. refreshing JEditorPane
    By nasi in forum AWT / Java Swing
    Replies: 9
    Last Post: April 9th, 2010, 04:01 AM