Results 1 to 10 of 10
Thread: Iterating components
- 04-15-2011, 01:05 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
Iterating components
I'm trying to iterate the components in a jdialog to find the textboxes and store the text into an array. How can I edit my code so it works?
Thanks.Java Code:Component[] components = dialog.getComponents(); for (Component com : components) { if (com instanceof JTextField) { names.add(com.getText); }
- 04-15-2011, 01:11 AM #2
By adding correct code and/or removing incorrect code.
Unless you tell us how it isn't working we cannot provide more help.
- 04-15-2011, 01:18 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
This part doesn't work:
It doesn't recognise the method as part of java.awt.component. I'm guessing I may have to cast "com" to JTextField but not sure.Java Code:names.add(com.getText);
- 04-15-2011, 01:23 AM #4
It's a method. just like add is. So what is different about them.
In future if you get compiler error messages, do you think it might be a good idea to paste them into your post instead of making us guess?
- 04-15-2011, 01:32 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
This is the error I get:
Have no file for C:\Program Files\Java\jdk1.6.0_24\jre\lib\modules\jdk.boot.ja r
Exception occurred during event dispatching:
java.lang.RuntimeException: Uncompilable source code - variable player might not have been initialized
at TicTacToe.actionPerformed(TicTacToe.java:181)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6289)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:605 4)
at java.awt.Container.processEvent(Container.java:204 1)
at java.awt.Component.dispatchEventImpl(Component.jav a:4652)
at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
at java.awt.Component.dispatchEvent(Component.java:44 82)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:44 82)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:644)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:617)
at java.awt.EventQueue$2.run(EventQueue.java:615)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 614)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:178)
at java.awt.Dialog$1.run(Dialog.java:1046)
at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:642)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:617)
at java.awt.EventQueue$2.run(EventQueue.java:615)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 614)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
- 04-15-2011, 01:37 AM #6
That has got nothing to do with the code you posted. Stop wasting my time by providing incomplete and incorrect information.
Did you read the error message. Did it not occur to you to initialise the variable?variable player might not have been initialized
-
Junky's right, your error has nothing to do with the code you've posted. If you read the error carefully (as you always should do), it tells you which variable is at fault, and in fact Junky posts the most important line of the error with this variable name above.
- 04-15-2011, 01:47 PM #8
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
Thanks all.
I've edited the code so it loops all the child elements of dialog and fixed the old error:
The error goes away when I comment out the system.out line.Java Code:List<Component> compList = Utils.getAllComponents(dialog); for (Component comp : compList) { if (comp instanceof JTextField) { System.out.println(comp.getText()); } }
Here's what the error console says with the new error:
Exception occurred during event dispatching:
java.lang.RuntimeException: Uncompilable source code
at TicTacToe.setNames(TicTacToe.java:195)
at TicTacToe.actionPerformed(TicTacToe.java:174)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6289)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:605 4)
at java.awt.Container.processEvent(Container.java:204 1)
at java.awt.Component.dispatchEventImpl(Component.jav a:4652)
at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
at java.awt.Component.dispatchEvent(Component.java:44 82)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:44 82)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:644)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:617)
at java.awt.EventQueue$2.run(EventQueue.java:615)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 614)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:178)
at java.awt.Dialog$1.run(Dialog.java:1046)
at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:642)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:617)
at java.awt.EventQueue$2.run(EventQueue.java:615)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 614)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
...and the source code window tells me it can't find the method getText() for java.awt.Component.
- 04-15-2011, 02:18 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
You shouldn't even attempt to run the code because, as the exception stack trace tells you: the code isn't even compilable; have a look at the code: with the instanceof operator you check if a comp is a JTextField; it is, you know it but the compiler still doesn't know it, i.e. it is still just a Component, and a Component doesn't have a getText() method. You should cast the comp variable to a JTextField to be able to call getText() on it ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-15-2011, 02:32 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
Iterating over a map of objects
By javaaz in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 07-08-2010, 10:29 PM -
Iterating through ArrayList using a GUI
By DC200 in forum New To JavaReplies: 0Last Post: 08-02-2009, 01:24 AM -
Iterating through a HashSet
By Java Tip in forum Java TipReplies: 0Last Post: 01-21-2008, 04:34 PM -
Iterating through ArrayList
By Java Tip in forum Java TipReplies: 0Last Post: 01-20-2008, 08:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks