Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to set the state of a radio button based on a configuration file.

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to set the state of a radio button based on a configuration file.

    Hi,
    I am using Java Swing to create a GUI. I have some radio buttons on a JFrame that gets opened when the user makes a certain selection. I want to read a config file to have the radio buttons selected or deselected when the form is opened to indicate the current state of the radio button based on the saved config file.
    How do I go about setting the radio button as selected (visibly selected) when I open up the JFrame?
    I was trying to use RadioButton.setSelected(True); but this doesn't actually visibly select the radio button.
    Any help would be greatly appreciated


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to set the state of a radio button based on a configuration file.

    Can you show us an SSCCE that demonstrates the problem? Because setSelected(true) should definitely select the JRadioButton.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to set the state of a radio button based on a configuration file.

    Here is the code. I am new to java so I may be doing something fundamentally wrong. The main is called from another JFrame

    private void getInputSettings()
    {
    String TxString;
    String RxString;
    System.out.print("Initializing window\n");

    //Create an instance of the command interface
    CmdInterface cmdIf = new CmdInterface();

    //Get Input#1 enable status

    *Check the file. I have verified that this is done properly*

    if (enabled != 0)
    {
    System.out.print("Test message");
    input1EnableButton.setSelected(true);
    }
    else
    {
    input1EnableButton.setSelected(false);
    }
    }
    /**
    * @param args the command line arguments
    */
    public void manualCfg_start()
    {

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    //Get the currently configured settings
    getInputSettings();
    new manualCfg().setVisible(true);

    }
    });
    }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to set the state of a radio button based on a configuration file.

    That really isn't an SSCCE. Here is an SSCCE that shows that setSelected() does indeed work:

    import javax.swing.JFrame;
    import javax.swing.JRadioButton;
     
    import java.awt.FlowLayout;
     
    public class ButtonTest {
     
        public ButtonTest() {
     
        	JFrame frame = new JFrame("Button Test");
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	frame.setSize(200,200);
     
        	JRadioButton button = new JRadioButton("JRadioButton");
        	button.setSelected(true);
     
        	frame.setLayout(new FlowLayout());
        	frame.add(button);
     
        	frame.setVisible(true);
        }
     
        public static void main(String[] args) {
           new ButtonTest();
        }
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Button and Radio Buttons not working!!!
    By rokere1 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 19th, 2013, 03:59 PM
  2. Radio Button Help Please!
    By Rick Sebastian in forum What's Wrong With My Code?
    Replies: 18
    Last Post: March 3rd, 2013, 07:26 PM
  3. [SOLVED] set specific location for radio buttons
    By nosxlimit in forum Java Theory & Questions
    Replies: 3
    Last Post: June 18th, 2012, 08:15 AM
  4. Radio button hide based on input..please help guys
    By ravi9999 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: April 7th, 2012, 06:24 AM
  5. Action from Radio Button
    By halfwaygone in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 25th, 2010, 10:52 AM