How to list/access swing components?

hi all,
Can anyone give me a clue, how can I access or simply list swing components I have on a frame, long after I created them. E.g. I'd like to change their setEnabled() attribute at a later time, not at creation time, but I don't know how to access them.
Does Swing/AWT/JFC provides a component hierarchy? can someone share some code on this issue?
Thanks for any help.
Thomas

I guess, you are saying the use of getComponent(int n)?
Since I'm a beginner in Swing see my short code below:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
public class EasyFrame {
    public EasyFrame() {
        super();
        startHere();
    public void startHere() {
        JFrame frame = new JFrame("SwingApplication");
        JButton button = new JButton("click here");
        JTextArea area = new JTextArea();
        frame.setSize(300, 200);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(area, BorderLayout.CENTER);
        frame.getContentPane().add(button, BorderLayout.SOUTH);
        area.setText("Hello World");
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("A Menu");
        menuBar.add(menu);
        JMenuItem menuItem = new JMenuItem("A text item");
        menu.add(menuItem);
        menu.addSeparator();
        menuItem = new JMenuItem("hello");
        menu.add(menuItem);
        JMenu subMenu = new JMenu("submenu");
        menuItem = new JMenuItem("sub1");
        subMenu.add(menuItem);
        menuItem = new JMenuItem("sub2");
        subMenu.add(menuItem);
        menu.add(subMenu);
        frame.getContentPane().add(menuBar, BorderLayout.NORTH);
        frame.show();
     public static void main(String[] args) {
     EasyFrame ef = new EasyFrame();
}If I make a System.out.println(frame.getContentPane().getComponentCount());I get 3. But I should dig deeper (with getComponent(2)) to access JMenuBar. But this way I receive a AWT Component / not a JMenuBar Swing component to get its JMenu / and its JMenuItems. And I cannot make it cast to JMenuBar since this walk-through routine should be a universal while(isThereMoreCompenent){} procedure, I don't know when to cast to what (JMenu, JMenuItem, Button and so on).
My problem is that how can I access the JMenuItems one by one, and change e.g. the setEnabled attribute of the "hello" menu?
I hope you can make it clear to me.
Please help if so.

Similar Messages

  • How can i understand swing components?

    hi
    i am tonmoy. i have some question about swing component
    i,e JTextField,JListBox JTable etc.
    1. i have made a class name StyledDOcument extends
    DefaultStyledDocument so i can access any public method of
    DefaultStyledDocument. but my problem is that i cannot understand
    which method is implicitly called and whcih are not.
    for example InsertString(int offs, String str, AttributeSet a)
    is internally called because if i dont override it or call it using
    super.insertstring(), it will be called internally everytime i press
    char from keybord.
    so i think there are many method of swing components that are called
    internally but by reading java doc i can't understand
    which method is internally called and which are not.
    so plese answer my questions in details.
    thank you

    Tutorials, Teachers, and books are all good ways to learn these things.

  • How does one access MX components -- e.g. DataGrid -- in Design View in Flash Builder Burrito

    Hi,
    Is there a way to access the mx version of components in Design View (Flash Builder Burrito), for those components that have both a Spark and a Halo/MX version, e.g. DataGrid?
    Thanks

    ... I know I can go to Source View and type in part of it, and switch to design view, however I'm doing a bunch of tutorials where the mx components are used, where one is simply dropping data services onto the components, apparently not supported yet on the newer Spark components?

  • How can i access to components of a table?

    Hi,
    I have a table and i need to set to some componen's value in the prerrender method.
    Some components are asociated with the database and others not (i want to modify the second case) .
    I dont want to modify all rows.
    What sould i do?
    Thanks in advance.
    Message was edited by:
    walfrido

    Some components are asociated with the database and
    others not (i want to modify the second case) .
    I dont want to modify all rows.I use Map objects to store the values for the columns that are not associated with the database. I then create properties with getters and setters for the elements in the map bind the components in those columns to those properties.
    http://blogs.sun.com/roller/page/divas?entry=table_component_sample_project
    Hope this helps,
    diva#2

  • I'd like to make a list of interactive Swing components....

    EDIT: I've been doing more research about Layout Managers and it appears that BoxLayout may provide me with the solution I need. I'll mess around with it for awhile and report back if I still need help. Thanks pals!+
    I would like to make a list of JPanels laid out in basically a column. One on top of the other. That's not so difficult, but I need to make it able able to add and remove panels in response to user-driven events (in other words, I can't just hard-code them all, and the panels are generated on the fly, so I don't even know how many I'll need to be displaying until I need to display them.
    Here's a simple mockup of the window I'm talking about.
    |      - o x |
    |____________|
    |            |
    |            |
    |   Panel1   |
    |   Panel2   |
    |   Panel3   |
    |   Panel4   |
    |____________|At first, I thought a JList would serve my purpose, but you can't add interactive Swing components to JLists (or JTables)... they'll render, but that's all. My panels will contain a button and a checkbox, so this is a no-go.
    I was also thinking I could just manage the placement with GridBagLayout and just keep indexes for the current y position, but if there's an easier way, I'd rather not go this route. It has me writing a lot of extra code and manipulating a lot of fields of GridBagConstraints, which opens me to less-understandable code and more error-prone code, on account of I'm great at making little mistakes when I'm setting craploads of constraints.
    Does anyone have any ideas? It'd be ideal if there was just some sort of container I could plop them in with add and remove methods (a la JTabbedPane or something).
    I'm pretty new to Swing, so maybe I'm missing something obvious.
    Edited by: Caryy on Sep 30, 2010 4:15 PM

    I would recommend using BoxLayout or Box.createVerticalBox(). As mKorbel already mentioned, you need to call revalidate() after adding any component to an already realized/visible container, and additionally call repaint() after removing a component. To resize the top level window to accommodate its new content, you would call pack(). Or, again as suggested by mKorbel, wrap your panel/Box in a JScrollPane.
    db

  • How do I access templates in InDesign?  They are not listed on the welcome screen.

    How do I access the templates in InDesign?  They are not listed on my "welcome screen". 

    There are no templates with recent versions of InDesign.
    However, the website InDesignSecrets.com has rescued the old ones from vintage CS4 days and has posted them on their website:
    http://indesignsecrets.com/indesign-templates

  • How can i access my wish list on my iphone?

    I created a wish list on itunes on my computer but i want to know how can i access the wish list on my iphone itself. can someone help me?

    The Wish List is there on the iPad, it's one of the links below Movies, but it isn't there on the iPhone.
    If you'd like, you can leave feedback for Apple.

  • How do I access my iTunes wish list on mini pad purchased 1/11/2013?

    How do I access my ITunes wish list?  I cannot find a link.

    Thanks for your answer.  Prior to submitting my question, I called Apple Support and they tried to sell me a software insurance policy for $249.50/year before they would answer my question.

  • How to access/identify components in JSF Declarative Components?

    Hi,
    I am beginner on ADF. Trying to build first Declarative Components.
    Use Case is as follows -
    I have put 2 InputTexts in Declarative Component.
    Want to set some value in second InputText (txtAddressLine2) in Validator/ValueChangeListner method of first InputText(txtAddressLine2).
    <?xml version='1.0' encoding='windows-1252'?>
    <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
    <jsp:directive.page contentType="text/html;charset=windows-1252"/>
    <af:componentDef var="attrs" componentVar="component">
    <af:xmlContent>
    <component xmlns="http://xmlns.oracle.com/adf/faces/rich/component">
    <display-name>Test</display-name>
    <attribute>
    <attribute-name>
    AddressLine2
    </attribute-name>
    <attribute-class>
    java.lang.String
    </attribute-class>
    </attribute>
    <component-extension>
    <component-tag-namespace>Address3</component-tag-namespace>
    <component-taglib-uri>/Address3</component-taglib-uri>
    </component-extension>
    </component>
    </af:xmlContent>
    <af:inputText label="Address Line 1"
    binding="#{backing_Address3.txtAddressLine1}"
    id="txtAddressLine1"
    validator="#{backing_Address3.txtAddressLine1_validator}"
    autoSubmit="true" immediate="true" rendered="true"
    valueChangeListener="#{backing_Address3.txtAddressLine1_valueChangeListener}"/>
    <af:inputText label="Address Line 2" binding="#{backing_Address3.txtAddressLine2}"
    id="txtAddressLine2"
    validator="#{backing_Address3.txtAddressLine2_validator}"
    autoSubmit="true" immediate="true"/>
    </af:componentDef>
    <!--oracle-jdev-comment:auto-binding-backing-bean-name:backing_Address3-->
    </jsp:root>
    This is sample I am working on.
    I am trying following approaches in bean of Component itself.
    Approach I: This does not give any error, value is not set in txtAddressLine2
    this.getTxtAddressLine2().setValue("Some Value");
    Approach II: Not able to access txtAddressLine2 using findComponent() method
    UIViewRoot uiViewRoot = facesContext.getViewRoot();
    RichInputText inputText;
    inputText = null;
    if (uiViewRoot.findComponent("txtAddressLine2") != null) {
    System.out.println("Found ");
    inputText = (RichInputText)uiViewRoot.findComponent("txtAddressLine2");
    inputText.setValue("my value");
    } else {
    System.out.println("Not Found "); //Always not found
    Can anybody tell me correct way to access components and set their values inside Declarative Components itself?

    Thanks buddies....its resolved!
    This is how I have done it -
    Components have these 2 Input Texts :
    <af:inputText label="Label 1" id="txt1" autoSubmit="true" immediate="true"
    binding="#{DC2.txt1}" validator="#{DC2.txt1_validator}"/>
    <af:inputText label="Label 2" binding="#{DC2.txt2}" id="txt2"
    immediate="true" autoSubmit="true" partialTriggers="txt1"/>
    Code in Component Bean setting value is as follows:
    RichInputText txt22;
    txt22 = getTxt2();
    txt22.setSubmittedValue("Some Value");

  • How can I use two fonts in swing components?

    I want to implement a project. When a part of the JMenuBar, JButton or other swing components' text is English, I use Tahoma font, and other of these components' text is Chinese, I use SimSun font, just like the Chinese Windows OS. How can I implement it??
    Thanks!!

    Ooh, thats a tricky one. I'm not sure whether you can do that, but maybe someone else with more experienced can correct me.
    You'll probably have to create your own custom JMenu that displays two labels, one for English and one for the Chinese parts?

  • How do I access reading list???

    How do I access reading list???

    In Safari tap the icon of the open pages to the left of the address fieldto open your shortcuts popup, and you then might have to navigate up one or more levels via the 'bookmarks' button at the top left of the shortcuts - you should eventually get to a level where you can then select Reading List'

  • How to access unamed components

    If I had a frame with 100 text fields which was added to the frame using the following loop. How can I access and identify individual textfields
    for (int i=0;i<100;i++)
       someframe.add(new JTextField());
    }Any help is appreciated

    You could do something like the following:
    for (int i=0;i<100;i++)
    someframe.add(new JTextField(),setName("someUniqueName"));
    code]But that would require searching through the list of fields in the container looking for the correct field (not efficient).
    I suggest using the array suggestion. Or if you don't want to index them numerically, you could use a hashmap and map then to specific names.

  • How to access UI components in event handler

    Hi
    How can I access all or a particular UI component's object in my event handler of a event which is not caused by that UI component which is being accessed.

    You need variable resolution:
    FacesContext context = FacesContext.getCurrentInstance();
    Application application = context.getApplication();
    WaferMap waferMap = (WaferMap)application.getVariableResolver().resolveVariable(context, "waferMap");or you can ask the event for ones near by
        //action from an h:commandLink
         public void seeMore(javax.faces.event.ActionEvent event)
           System.out.println("action: seeMore "+((HtmlCommandLink)event.getSource()).getAttributes());
            java.util.List map=((HtmlCommandLink)event.getSource()).getChildren();
            Iterator i=map.iterator();
            System.out.println("action: seeMore ");
            while(i.hasNext())
                Object obj=i.next();
                if(obj instanceof UIParameter)
                    UIParameter param=(UIParameter)obj;
                    System.out.println("\t seeMore "+param.getValue()+" ");
        }

  • How do you access folders/files in other users Finder list?

    How do you access folders in other users finder list, once you have shared the folder or file

    You should be able to open and view the contents of any directories you have access permissions to using FInder, by navigating the directory tree to the folders in question and opening them as you would your own folders. If you know the full path-name of the directory, you can enter it into the Finder navigator opened by Apple-Shift-G; the dialog that pops up uses the same directory listing method as all unix systems, and has tab completion for ease of use. In general, all users have their home directories in /Users/, or you can simply use ~/../<name> to go to the home folder of a user via the parent (..) directory of your home (~) directory. If you are experiencing bugs or errors with this, as opposed to simply not knowing how to do this, please post any details of your experience so that others can help troubleshoot your problems.

  • How can I learn "Drag and Drop" with Swing components?

    Hello,
    Im using swing components, so using paint() to draw a "drag icon" doesnt seem acceptable. Im basically trying to move a JPanel image to be dragged/dropped over another JPanel in the same program/applet.
    I looked at Sun's Java Tutorial for information on doing drag and drop, but there was no trail completed yet.
    Does anyone know of any good resources to learn? Or can at least give me a general idea of how to do drag and drop?

    Did you see this?
    http://java.sun.com/docs/books/tutorial/dnd/index.html
    http://java.sun.com/docs/books/tutorial/reallybigindex.html

Maybe you are looking for