Results 1 to 5 of 5
- 08-15-2011, 09:41 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 66
- Rep Power
- 0
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; }
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); }
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; }
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!
- 08-15-2011, 10:05 PM #2
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
- 08-15-2011, 11:05 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 66
- Rep Power
- 0
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; }
Java Code:panels.get(i).component.componentMethod
- 08-15-2011, 11:11 PM #4is there a way to access the panels components via a call like this:
Personally, I'd split the chained code up into separate statements.
-
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
-
Getting text from inside a jeditorpane inside a jtabbedpane
By captain alge in forum New To JavaReplies: 9Last Post: 04-12-2011, 08:26 PM -
Displaying panels inside a main panel
By Sneaky Fox in forum AWT / SwingReplies: 4Last Post: 01-21-2011, 05:12 PM -
how to add 2 panels in a panel inside a frame
By clydedoris in forum New To JavaReplies: 4Last Post: 07-31-2010, 10:11 AM -
I can't read a text file located inside my jar.
By Serrano0811 in forum New To JavaReplies: 1Last Post: 04-21-2010, 06:03 PM -
Why the panel text changed?
By ottawalyli in forum AWT / SwingReplies: 1Last Post: 12-17-2007, 06:56 AM
Bookmarks