Results 1 to 3 of 3
  1. #1
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default 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).

    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...
                }
            }
        }
    }
    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?? )

    Thank you in advance for any help/information/direction that you can provide.

    Chris
    Last edited by tashimoto; 11-19-2010 at 08:34 PM. Reason: Added [code] tag

  2. #2
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    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)

  3. #3
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    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

  1. SetBackground for JComponent
    By tulasi.neppali in forum AWT / Swing
    Replies: 5
    Last Post: 09-12-2010, 11:14 PM
  2. Random size of JComponent
    By Karl-von-bahnhof in forum AWT / Swing
    Replies: 0
    Last Post: 04-13-2010, 09:14 PM
  3. Detecting mouseEntered event in JComponent
    By djhallx in forum AWT / Swing
    Replies: 1
    Last Post: 02-26-2009, 11:39 PM
  4. JComponent gradient background
    By snipered in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2008, 01:38 AM
  5. polygon-shaped JComponent
    By zenMarko in forum New To Java
    Replies: 2
    Last Post: 11-04-2008, 07:06 PM

Posting Permissions

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