Results 1 to 3 of 3
  1. #1
    skatefreak is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default Radio buttons and obscure code.

    Hey,
    So i'm on my way with this program but i'm having an issue with getting the selection of radio buttons influencing the files save destination.

    The aim is to be able to save the username and password into either the busiUsers or persUsers depending on which radiobutton is selected..
    Alas no matter weather i select personal or buisness it keeps saving the login/password in the same folder...

    Heres the relevant part of the code, i realise the logic behind this is no doubt over complicated but i'm just trying to get it working at the moment :)


    Java Code:
            JRadioButton busButton = new JRadioButton("Business" , true);
            JRadioButton perButton = new JRadioButton("Personal" , false);
            
            ButtonGroup bgroup = new ButtonGroup();
            bgroup.add(busButton);
            bgroup.add(perButton);
    
            boolean perButt = perButton.isSelected(); 
    
            setLayout(new GridLayout(5,2));  
            add(UserLab);
            add(Usernme);
            add(PwLab);
            add(Pw);
            add(select);
            add(account);
            add(busButton);
            add(perButton);
            add(cc);
            add(subButton);
            
    
         if (perButt = (true))
                {
         try
                {
    
                myStrings = new PrintWriter(new FileOutputStream("PersUsers.txt", true));
                subButton.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        
                        myText = Usernme.getText();
                        myStrings.println("\n" + myText);
                        String myText = "";
                        char[] pass = Pw.getPassword();
                        myStrings.println(pass);
                        myStrings.close();
                        
                        JOptionPane.showMessageDialog (null, "User sucessfully saved");
                        
                        Regcont aRegcont = new Regcont();
                        
                        setVisible(false);
                        dispose();
                        }
                });
            }
                    catch (IOException e)
                    {
                System.out.println(e.getMessage());
            }
        }
        
        else if (perButt = (false))
                {
                    try
                    {
                    
                myStrings = new PrintWriter(new FileOutputStream("BusiUsers.txt", true));
                subButton.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        
                        myText = Usernme.getText();
                        myStrings.println("\n" + myText);
                        String myText = "";
                        char[] pass = Pw.getPassword();
                        myStrings.println(pass);
                        myStrings.close();
                        
                        JOptionPane.showMessageDialog (null, "User sucessfully saved");
                        
                        Regcont aRegcont = new Regcont();
                        
                        setVisible(false);
                        dispose();
                        }
                });
            }
                    catch (IOException e)
                    {
                System.out.println(e.getMessage());
            }
        }

    Any thoughts would be much appreciated, any constructive criticism is much appreciated especially any observations on my no doubt obscure coding/naming etc...
    Just trying to get into the swing of things here :)

    Best regards

    -Jvr

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Some issues I see:

    1) You're testing the status of a JRadioButton in the GUI creation code, not in an actionlistener. Since this is Swing GUI code, this is event-driven programming. So the status of the JRadioButton is meaningless at button creation time, and only means something at the time of an event, a button press perhaps. If you do this in a button's actionlistener, then you may wish to find out which button has been selected by calling getSelection() on the ButtonGroup which will give you the button model of the selected JRadioButton. If you've set the radiobutton's actionCommand, you can easily find out which button has been selected.

    2) This is a dangerous construct:

    Java Code:
    if (blah = true)
    {
      //...
    }
    note that = true sets blah to true then checks if it's true (as it always will be). == true on the other hand checks if it equals true, but even better is simply if (blah):
    Java Code:
    if (blah)
    {
      //...
    }
    HTH

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Fubarable mentioned this in point 2, but it is rather critical.

    "=" means assign the value on the right to the field on the left

    "==" means compare the value on the right to the value on the left at the stack level, which means it works with primitives and tests to see if to two object references are the same.

    He also correctly points out that your GUI code is completely wrong. You make the component visible, on the wrong thread, and then immediately dispose() it. I would think the GUI must flash up on the display only long enough for everything to appear, and then disappear.

    Sun's Java Tutorial should have specific information about starting a GUI and listening for events.

Similar Threads

  1. Non functional radio buttons and messed up array
    By flameofSuzaku in forum New To Java
    Replies: 2
    Last Post: 01-10-2011, 05:38 PM
  2. [B]Tab Sequence problem with radio buttons[/B]
    By shobha2k8 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-25-2008, 07:25 AM
  3. How to use Radio Buttons
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:26 PM
  4. radio buttons and paint
    By gtraylo in forum Java Applets
    Replies: 1
    Last Post: 04-19-2008, 12:43 PM
  5. property for radio buttons
    By swapnanair in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-03-2007, 08:39 AM

Posting Permissions

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