Results 1 to 3 of 3
- 10-01-2010, 06:40 PM #1
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
Using JComponent methods on a JComponent that was found using .getComponents()
Hello,
I am trying to create a class with methods that retrieve components from a container using .getComponents() and then find the values/settings of those components (example: isSelected() from JCheckBox, getText() from JTextField). I will be saving the values into a file for use later (as in saving user preferences).
Is there a better way to locate components within a container and still be able to use the method calls for that component? ( I can use .getName() and get the name of the component... is there a way to then use that to call the methods for that particular component?? )Java Code:import java.awt.Component; import java.awt.Container; import javax.swing.JCheckBox; import javax.swing.JList; public class WindowContent { private Component[] comp; private Container container; public WindowContent() { } public void saveWindowContent(Container cont) { container = cont; comp = container.getComponents(); //I want to get values of JComponents that are in the container //which will more than likely be a JPanel for (Component c : comp) { if (c instanceof JCheckBox) { // c.isSelected(); } if (c instanceof JList) { // get values of JList ... etc... } } } }
Thank you in advance for any help/information/direction that you can provide.
ChrisLast edited by tashimoto; 11-19-2010 at 07:34 PM. Reason: Added [code] tag
- 10-01-2010, 07:03 PM #2
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
I just found a reference in a book* that talks about "..casting each element of the array back to JCheckBox and call its isSelected() method..." (for those that don't know: getComponents() creates an array of references to the container's child components.)
I will read/search for more information about casting the elements back to their JComponent ...
*O'Reilly's Learning Java by patrick Niemeyer & Jonathan Knudsen (page.468)
- 10-01-2010, 07:18 PM #3
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
Wow, I feel "dumb"... I referred back to my Java Text from class (Java Software Solutions 4th Edition by Lewis & Loftus) and on page 88 was my answer:
Casting (one of the basic lessons of java)
"A cast is a Java operator that is specified by a type name in parentheses. It is placed in front of the value to be converted."
Example:
Component comp = new Component();
JCheckBox checkbox = new JCheckBox();
checkbox = (JCheckBox) comp;
------------------------------------
Sometimes the solutions seems like it will be more complicated than it really is! :)
Similar Threads
-
SetBackground for JComponent
By tulasi.neppali in forum AWT / SwingReplies: 5Last Post: 09-12-2010, 10:14 PM -
Random size of JComponent
By Karl-von-bahnhof in forum AWT / SwingReplies: 0Last Post: 04-13-2010, 08:14 PM -
Detecting mouseEntered event in JComponent
By djhallx in forum AWT / SwingReplies: 1Last Post: 02-26-2009, 10:39 PM -
JComponent gradient background
By snipered in forum AWT / SwingReplies: 0Last Post: 12-30-2008, 12:38 AM -
polygon-shaped JComponent
By zenMarko in forum New To JavaReplies: 2Last Post: 11-04-2008, 06:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks