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 4 of 4

Thread: Array Output in JTextField

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Array Output in JTextField

    I am having some trouble with outputting data from an array into a text field. When I just do the standard println output everything works fine but it will not output into the text field. I have included the code below.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
     
    public class arrayExample extends JFrame
    {
      private JPanel contentPanel;
      private JLabel lblInput;
      private JTextField txtInput;
      private JTextField txtOutput;
      private JButton btnShow;
      private JButton btnAdd;
      private String temp; //the variable of the input box
      private int counter = 0;
     
      String student [] = new String [10];
     
      private void add (Container con, Component widget, int left, int top, int width,
                        int height)
      {
        widget.setBounds(left, top, width, height);
        con.add(widget);
      }
     
      arrayExample()
      {
        contentPanel = (JPanel)getContentPane();
        contentPanel.setLayout(null);
     
        lblInput = new JLabel ("Enter Name");
        add(contentPanel, lblInput, 10, 0, 100, 20);
     
        txtInput = new JTextField();
        add(contentPanel, txtInput, 0, 30, 100, 20);
     
        btnAdd = new JButton ("Add Name");
        add(contentPanel, btnAdd, 10, 70, 80, 20);
     
        btnShow = new JButton ("Show Names");
        add(contentPanel, btnShow, 10, 110, 110, 20);
     
        txtOutput = new JTextField();
        add(contentPanel, txtOutput, 200, 0, 200, 300);
     
        //details of the window
        setTitle("Class List");
        setSize(500, 500);
        setLocation (new Point (150, 150));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
     
        //adds the input from text box into the array
        btnAdd.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent add)
          {
            temp = txtInput.getText();
            student[counter] = temp;
            counter = counter + 1;
            txtInput.setText("");
          }
        });
     
        //controls the function of the Show button
        //prints out the information put into the input field
        btnShow.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent add)
          {
            for (int i = 0; i < student.length; i++)
            {
              txtOutput.setText(student[i]);
              System.out.println(i + ": " + student[i]);
            }
          }
        });
     
      }
     
      public static void main (String [] args)
      {
        new arrayExample();
      }
    }
    Last edited by pbrockway2; April 13th, 2013 at 09:19 PM. Reason: code tags added


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Array Output in JTextField

    I've added code tags to your post. The idea is that you put [code] at the start of the code and [/code] at the end. That way the forum will format the code so that it's readable.

    --- Update ---

        //controls the function of the Show button
        //prints out the information put into the input field
        btnShow.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent add)
          {
            for (int i = 0; i < student.length; i++)
            {
              txtOutput.setText(student[i]);
              System.out.println(i + ": " + student[i]);
            }
          }
        });

    I'm not sure I understand the comment on this method as it seems to be writing stuff to the output field not the input field. Maybe that's a typo.

    The thing about text fields is that they are only designed to display a single line of text. So they are no good for displaying multiline text. For that you probably want a JTextArea which has an append() method. JTextArea usage is discussed on the How to Use Text Areas page of Oracle's Tutorial.

    (A small point, but your class should be called ArrayExample with a capital A. Using capital letters for classes and lowercase for methods and variables is a Java convention that helps make it easier for others to read your code. Note that the name of the .java file also has to change to ArrayExample.java.)

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    shodai (April 14th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array Output in JTextField

    Thanks so much for your help. I am still figuring out all of the details and subtleties of Java. A couple of minor changes and things work great. Also thanks for giving the advice in terms of naming conventions.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Array Output in JTextField

    You're welcome.

    Because it's been around for a while Java offers a lot gui controls like text areas etc. (and that's ignoring 3rd party ones!) The only way to master them is through using them: experimenting and deciding on what is the most appropriate for your particular problem. Hopefully you'll find the links to the API and the Tutorial useful.

Similar Threads

  1. Printing Output from an array in a gui form.
    By chizzo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 28th, 2012, 08:50 AM
  2. Formatting 2d Array Output
    By dspear in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: December 14th, 2011, 06:45 PM
  3. Store Values in 2D array and output to textarea
    By raverz1tildawn in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 7th, 2011, 03:13 PM
  4. need help Printing output from a JTextfield
    By juanbond311 in forum Java Theory & Questions
    Replies: 27
    Last Post: June 21st, 2010, 08:26 AM
  5. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM