Results 1 to 5 of 5
  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default Getting text from JTextFields located inside a panel

    I have this cell class which contains a method that generates a gui for an object of said class. see below:

    Java Code:
            public class Cell
    
            private JTextField textfield[];
    	private JLabel labels[];
    	private JLabel Title;
            ...
            public JPanel createGUI (int cell_length){
    		
    		JPanel panel = new JPanel();
    		
    		textfield = new JTextField [cell_length];
    		labels = new JLabel [cell_length];
    		Title = new JLabel (this.getCellName());
    		panel.add(Title);
    		
    		for (int i = 0; i < textfield.length; i++){
    			textfield[i] = new JTextField(15);
    			labels[i] = new JLabel("Word "+(i+1)+":");
    			panel.add(labels[i]);
    			panel.add(textfield[i]);
    		}
    		
    		return panel;
    	}
    That panel is then attached to a JFrame by the GUI class:

    Java Code:
    public GUI_001 ()
    	{
    		super("Create Cells");
    		setLayout( new FlowLayout() );
    
    		j = this.askNumberofCells();
    		
    		cells = new Cell [j];
    		
    		q = this.askCellLength();
    		
    		for (int i = 0; i < cells.length; i++){
    			
    			cells[i] = new Cell(q) ;
    			cells[i].setCellName(this.askCellName(j,(i+1)));
    			add(cells[i].createGUI(q));
    		}
    		
    		submit = new JButton ("Submit");
    		add(submit);
    	}
    I am currently trying to write a handler that does the following:

    When the submit button is pressed the JPanels of each Cell get their JTextField's text and store it in their instance of an array called words which already has the appropriate size.

    I am currently stuck at the part where I need to access the Jtextfields in each panel. so something like this:

    Java Code:
    for (int i = 0; i < panelsOnScreen.length[]; i++){
    cell_1.getWords[i] = panelsOnScreen[i].jtextfields[i].getText;
    }
    How would I go about doing this?

    Searching the interwebs suggested using a List to keep track of the panels and JTextFields. The later using those references to getText(); However I haven't ever used Lists before and don't know how they work :( So, if lists are involved in a solution, please give an example of how you declared one and added the panels/text fields to it.

    Thanks in advance!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Have you looked at the API doc for ArrayLists for example.
    Or the tutorial. Go to this page and Find List Implementations:
    The Really Big Index

  3. #3
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default

    Alright, Ive added an ArrayList to keep track of the panels being added:

    Java Code:
    import java.util.ArrayList;
               ...
               public static  ArrayList panels;
               ...
    	public JPanel createGUI (int cell_length){
    		
    		JPanel panel = new JPanel();
    		
    		textfield = new JTextField [cell_length];
    		labels = new JLabel [cell_length];
    		Title = new JLabel (this.getCellName());
    		panel.add(Title);
    		
    		for (int i = 0; i < textfield.length; i++){
    			textfield[i] = new JTextField(15);
    			labels[i] = new JLabel("Word "+(i+1)+":");
    			panel.add(labels[i]);
    			panel.add(textfield[i]);
    		}
    		
    		[B]panels.add(panel);[/B]
    		return panel;
    	}
    is there a way to access the panels components via a call like this:
    Java Code:
    panels.get(i).component.componentMethod

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    is there a way to access the panels components via a call like this:
    Possibly. Write a small section of code and see what the compiler says.
    Personally, I'd split the chained code up into separate statements.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend that your classes use more OOP. Rather than muck through a container hierarchy that could and often will change, why not give your Cell class public getter methods to get the text held by the fields it holds. For instance please check my answer in this thread here on StackOverflow: sending-messages-between-two-jpanel-objects.

    I'm hovercraft in that thread.

Similar Threads

  1. Replies: 9
    Last Post: 04-12-2011, 07:26 PM
  2. Displaying panels inside a main panel
    By Sneaky Fox in forum AWT / Swing
    Replies: 4
    Last Post: 01-21-2011, 04:12 PM
  3. how to add 2 panels in a panel inside a frame
    By clydedoris in forum New To Java
    Replies: 4
    Last Post: 07-31-2010, 09:11 AM
  4. I can't read a text file located inside my jar.
    By Serrano0811 in forum New To Java
    Replies: 1
    Last Post: 04-21-2010, 05:03 PM
  5. Why the panel text changed?
    By ottawalyli in forum AWT / Swing
    Replies: 1
    Last Post: 12-17-2007, 05:56 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
  •