Results 1 to 4 of 4
  1. #1
    markious's Avatar
    markious is offline Member
    Join Date
    Oct 2008
    Rep Power

    Default Java GUI, reading an Array Of JTextFields

    So I have a slight problem with a java project..

    Java Code:
    //Usual jargon here
    //Declare the  Array of JTextFields
    private JTextField[][] tCells = new JTextField[rows][columns];
    //Further on down the code
    //Nested for loop to print out the JTextFields
                      for (int rowCount = 0; rowCount < rows; rowCount++) {
                for (int columnCount = 0; columnCount < columns; columnCount++) {
                    tCells = new JTextField[rows][columns];
                    tCells[rowCount][columnCount] = new JTextField(10);
    // /////////
    //Rest of the GUI creation code is here
    All that works fine..

    What I need to do is add something in my actionPerformed method that will read in a character entered into any of the fields..

    So I added a button called "check".


    Java Code:
    public void actionPerformed(ActionEvent userAction) {
            if (userAction.getActionCommand().equals("Check")){
                                  //What would I put in here to take in the text inserted into anyone of the text fields??
    As my comment says I dont know what to put in that if statement.

    If it was a single JTextField what I usually do is

    use the .getText method to read whats in the textField so I can pass the information on to some control logic.

    But what could I use to read information from an array of text fields, And if possible find the position in the array that holds the information

    Help is as always appreciated

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    You either register a separate ActionListener to each JTextField or you register one ActionListener to all the JTextFields and make it search for the one that fired the event (hint: the ActionEvent.getSource() gives you the component that fired the event).

    kind regards,

    ps. don't allocated an entire new array in the body of that nested loop; do it outside of the loops.

  3. #3
    markious's Avatar
    markious is offline Member
    Join Date
    Oct 2008
    Rep Power


    Thanking you, ill work on that now.

    Much appreciated

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power


    Create an Action that extends from TextAction() and add the Action to your button. Now you can use the getFocusedComponent() method of TextAction to determine the text field that had focus before the button was clicked.

    Also, there is no need to keep an array of text fields.

Similar Threads

  1. Reading text file into an array
    By Mahesh_ps in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 04:04 PM
  2. reading in unsigned ints into a 2D array
    By newToIt in forum New To Java
    Replies: 9
    Last Post: 03-06-2009, 01:36 PM
  3. Array not reading. Plz help
    By cmizer in forum New To Java
    Replies: 14
    Last Post: 02-12-2009, 01:02 AM
  4. Reading from a file to make an array
    By Bomber_Will in forum New To Java
    Replies: 11
    Last Post: 01-21-2009, 09:19 AM
  5. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-19-2008, 12:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts