Skip to main content
gamedev.net
🔒 Locked

[java] Swing Component Hierarchy

Started by Elehisie Nov 1, 2005 at 10:46 AM 2 replies 650+ views
Original Post
Elehisie
Elehisie
hi all! ok... lets say I have a JFrame, with a JScrollPanel in it. In the JScrollPanel, I put a few JRadiobuttons. Theres a button called "save" too. I grouped the radiobuttons and all. how do I check to see which one of the radiobuttons is selected when the user presses the "save" button? I made an event handler on the button, through which I want to check the state on everyone of the radiobuttons. it seems pretty simple, but the question is... how do I get to the state of thge radiobuttons? cuz I made a method to create the gui for me: private static void createAndShowGUI(boolean isWeld) {//code here } and all the component declarations are inside that method. so how do I get to it? I mean... is there some way to refer to a component inside a component properly, maybe something of the kind: myFrame.myScrollPanel.radioButton1.isSeleted(); that i can use from OUTSIDE of the method createAndShowGUI ?? god i think its confusing... hehe
Son of Cain
Son of Cain
Hmm.. You can always get components from the container... may be a hassle, they will need casting and etc.. But that's a way. Not the proper way, I might say.

Son Of Cain
a.k.a javabeats at yahoo.ca
Kevinator
Kevinator
You'll want to store a reference to the radiobuttons in the class where you need their state information.

Okay, to be more specific:

public class MyClass {        private static ArrayList<JRadioButton> myRadioButtons;    public static final int RBUTTONS = 3;        public static void main(String[] args) {        myRadioButtons = createAndShowGUI(true);                // Now we have an ArrayList (called myRadioButtons) that contains references to your        // radiobuttons as an instance variable of MyClass!        // this means that all the methods in MyClass can easily        // muck around with those radioButtons.        // here is an example of gaining access to those radiobuttons        // and doing something useful with them        int i = 1;        for(RadioButton rButton : myRadioButtons) {            if(rButton.isSelected())                System.out.printf("Radio button %d selected!%n", i);            i++;        }    }        private static ArrayList<JRadioButton> createAndShowGUI(boolean isWeld) {        // first we set up the GUI like normal        JFrame frame = new JFrame("GUI Program");        // etc...                // set up a collection so that we can return references to the        // JRadioButton objects to the caller        ArrayList<JRadioButton> a = new ArrayList<JRadioButton>();        for(int i = 0; i < RBUTTONS; i++) {            JRadioButton rButton = new JRadioButton();            component.add(rButton); // don't forget to add the buttons to the GUI            a.add(rButton); // add the buttons to our collection        }        return a;    }}


[Edited by - Kevinator on November 1, 2005 3:03:27 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.