This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2023/5/2

trying to get radio button in java to work

1
2
divinity divinity

2023/5/2

#
hi there, It has been quite a while since I have last post anything. i am doing a programming application, it is a home project for me, this is just for practice until I am perfect anyways, I am building a project where it say to write a program name banking that imitates the working of a bank. It has to use a gui interface, along with performing three actions, like enter personal and account information for new custmers, perform basic operations such as deposit and withdraw on account that exist in the bank database and print the content of the database to the consle. here is where I am stuck, I have been trying to get the radio buttons that I have used to work, not all as yet, i have so far gotten the gender buttons (male and female) buttons to work) I am trying now to get the deposit and withdraw buttons to work and have no idea where and what I have gone wrong. I know am missing something but what I dont know. I just need to knw what to do and how do i get it to work when u click either the deposit or withdraw buttons, here is the codes that I have used.
  ButtonGroup operations = new ButtonGroup();
        
        JRadioButton deposit = new JRadioButton("Deposit");
        JRadioButton withdraw = new JRadioButton("Withdraw");       
        
        operations.add(deposit);
        operations.add(withdraw);

         JPanel panel = new JPanel();
         panel.add(deposit);
         panel.add(withdraw);
            
              
        JFrame frame = new JFrame("");
        frame.setLayout(new BorderLayout());
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

   private void btndepositsActionPerformed(java.awt.event.ActionEvent evt) {                                            
        btndeposits.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent ae) {               
                btndeposits.setSelected(true);
                btndeposits.setSelected(false);
                
            }
        });

    }                                           
  private void btnwithdrawActionPerformed(java.awt.event.ActionEvent evt) {                                            
            btnwithdraw.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent ae) {               
                btnwithdraw.setSelected(true);
                btnwithdraw.setSelected(false);
               
            }
        });
danpost danpost

2023/5/3

#
divinity wrote...
I have been trying to get the radio buttons that I have used to work, not all as yet, i have so far gotten the gender buttons (male and female) buttons to work) I am trying now to get the deposit and withdraw buttons to work and have no idea where and what I have gone wrong. I know am missing something but what I dont know. I just need to knw what to do and how do i get it to work when u click either the deposit or withdraw buttons, << Code Omitted >>
It does not look like lines 3 through 17 are within a method. That would be okay for lines 3, 4, 9 and 14; but, the other lines need to be executed from inside a method.
divinity divinity

2023/5/8

#
hi danpost, I have tried doing what you have suggested but I am getting one set of errors.
danpost danpost

2023/5/8

#
divinity wrote...
hi danpost, I have tried doing what you have suggested but I am getting one set of errors.
Please show entire class codes and error trace.
divinity divinity

2023/5/9

#
 
from private buttonGroup() go right down to the last codes is errors. 


private void ButtonGroup(){
        ButtonGroup operations = new ButtonGroup();
        
        JRadioButton deposit = new JRadioButton("Deposit");
        JRadioButton withdraw = new JRadioButton("Withdraw");       
        
        operations.add(deposit);
        operations.add(withdraw);

         JPanel panel = new JPanel();
         panel.add(deposit);
         panel.add(withdraw);
            
              
        JFrame frame = new JFrame("");
        frame.setLayout(new BorderLayout());
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
        
        
        JFrame jfram = new JFrame("");
        jfram.setLayout(new BorderLayout());
        jfram.setLocationRelativeTo(null);
        jfram.setVisible(true);
       }

   }
        
        public String getSelectedOption(){
            Enumeration<AbstractButton>radioButtons=btngrp.getElements();
            while(radioButtons.hasMoreElements()){
                AbstractButton currentRadioButton=radioButtons.nextElement();
                if(currentRadioButton.isSelected()){
                    return currentRadioButton.getText();
                }
            }
            return null;
        }
        private void currentSelectedOption(){
            String selected = getSelectedOption();
            if(selected == null){
                System.out.println("Thee is something wrong! Nothing is selected");
                return;
            }
            switch(selected.toLowerCase()){
                case "Male":
                    System.out.println("Male is selected");
                    break;
                    
                case "female":
                    System.out.println("female is selected");
                    break;
                            
            }
        }
       
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        gendergrp = new javax.swing.ButtonGroup();
        operation = new javax.swing.ButtonGroup();
        creation = new javax.swing.JLabel();
        perinfo = new javax.swing.JLabel();
        jfname = new javax.swing.JLabel();
        jtxtfname = new javax.swing.JTextField();
        jlname = new javax.swing.JLabel();
        jtxtlname = new javax.swing.JTextField();
        female = new javax.swing.JRadioButton();
        male = new javax.swing.JRadioButton();
        jcntact = new javax.swing.JLabel();
        jtxtcontact = new javax.swing.JTextField();
        jacctinfo = new javax.swing.JLabel();
        jaccttype = new javax.swing.JLabel();
        jcomboxtype = new javax.swing.JComboBox<>();
        jdraft = new javax.swing.JLabel();
        jtxtlimit = new javax.swing.JTextField();
        jinitialbal = new javax.swing.JLabel();
        jtxtinitbal = new javax.swing.JTextField();
        jintrate = new javax.swing.JLabel();
        jtxtintrate = new javax.swing.JTextField();
        jsub = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        joperations = new javax.swing.JLabel();
        jacctID = new javax.swing.JLabel();
        jtxtacctID = new javax.swing.JTextField();
        jchoose = new javax.swing.JLabel();
        btndeposits = new javax.swing.JRadioButton();
        btnwithdraw = new javax.swing.JRadioButton();
        jbutexecute = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtxtArea = new javax.swing.JTextArea();
        jbutprint = new javax.swing.JButton();
        jbutexit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        creation.setFont(new java.awt.Font("Cambria", 3, 18)); // NOI18N
        creation.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        creation.setText("Account Creation");

        perinfo.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        perinfo.setText("Personal Information");

        jfname.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        jfname.setText("First Name");

        jtxtfname.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        jtxtfname.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtfnameActionPerformed(evt);
            }
        });

        jlname.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        jlname.setText("Last Name");

        jtxtlname.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        jtxtlname.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtlnameActionPerformed(evt);
            }
        });

        gendergrp.add(female);
        female.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        female.setText("Female");
        female.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                femaleActionPerformed(evt);
            }
        });

        gendergrp.add(male);
        male.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        male.setText("Male");
        male.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maleActionPerformed(evt);
            }
        });



                                   

    private void maleActionPerformed(java.awt.event.ActionEvent evt) {                                     
       male.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent ae) {
            String gender = "male";
            male.setSelected(true);
            male.setSelected(false);
           }
       });
       
    }                                    

    private void femaleActionPerformed(java.awt.event.ActionEvent evt) {                                       
        female.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
               String gender = "female";
               female.setSelected(true);
               female.setSelected(false);
            
            }
        });
        
    }                                      

    private void btndepositsActionPerformed(java.awt.event.ActionEvent evt) {                                            
        btndeposits.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent ae) {               
                btndeposits.setSelected(true);
                btndeposits.setSelected(false);
                
            }
        });

    }                                           

    private void btnwithdrawActionPerformed(java.awt.event.ActionEvent evt) {                                            
            btnwithdraw.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent ae) {               
                btnwithdraw.setSelected(true);
                btnwithdraw.setSelected(false);
               
            }
        });

        
    }                                           

    private void jtxtfnameActionPerformed(java.awt.event.ActionEvent evt) {                                          
       jtxtfname.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent ae) {
              jtxtfname.getText();
              
           }
       });
    }                                         

    private void jtxtlnameActionPerformed(java.awt.event.ActionEvent evt) {                                          
       jtxtlname.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent ae) {
              jtxtlname.getText();
           }
       });
       
    }                                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
         SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    
                }
            });
        
        
        
danpost danpost

2023/5/10

#
divinity wrote...
from private buttonGroup() go right down to the last codes is errors. << Code Omitted >>
Is this in the BUttonGroup class? Should line 4 have void in it and should it be private? I guess I am asking: shouldn't it be 'public ButtonGroup()"?
divinity divinity

2023/5/11

#
hi danpost: if it is 'public ButtonGroup' then why am i still getting errors? one minute it say, create a missing javadoc for ButtonGroup, what is a missing javadoc: and I am still getting a set of errors, I am not home at the moment, so I cant post the codes but when I reach home will post it.
danpost danpost

2023/5/11

#
divinity wrote...
from private buttonGroup() go right down to the last codes is errors.
When you do get a chance, only show what comes before "private buttonGroup()" (at least one method before, plus include the method after also). Also, please provide the name of the class this code is located in. Oftentimes, the transition from an erroneous line to the next will cause a ripple efffect that will not include the line with the error (usually, due to bracketing).
divinity divinity

2023/5/15

#
hi danpost: sorry for taking this long but here is the codes
  private  ButtonGroup(){  the error is right here n the method 
            
         
        ButtonGroup operations = new ButtonGroup();
        
        JRadioButton deposit = new JRadioButton("Deposit");
        JRadioButton withdraw = new JRadioButton("Withdraw");       
        
        operations.add(deposit);
        operations.add(withdraw);

         JPanel panel = new JPanel();
         panel.add(deposit);
         panel.add(withdraw);
            
              
        JFrame frame = new JFrame("");
        frame.setLayout(new BorderLayout());
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
danpost danpost

2023/5/15

#
divinity wrote...
hi danpost: sorry for taking this long but here is the codes << Code Omitted >>
You better put the "void" back in line 1. Still, will need to see more of the codes in the class (preferably the entire class codes).
divinity divinity

2023/5/15

#
 private void ButtonGroup(){  the error is saying; illegal start of expression
            
         
        ButtonGroup operations = new ButtonGroup();
        
        JRadioButton deposit = new JRadioButton("Deposit");
        JRadioButton withdraw = new JRadioButton("Withdraw");       
        
        operations.add(deposit);
        operations.add(withdraw);

         JPanel panel = new JPanel();
         panel.add(deposit);
         panel.add(withdraw);
            
              
        JFrame frame = new JFrame("");
        frame.setLayout(new BorderLayout());
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
        
        
        JFrame jfram = new JFrame("");
        jfram.setLayout(new BorderLayout());
        jfram.setLocationRelativeTo(null);
        jfram.setVisible(true);
        
   }
        
        public String getSelectedOption(){
            Enumeration<AbstractButton>radioButtons=btngrp.getElements();
            while(radioButtons.hasMoreElements()){
                AbstractButton currentRadioButton=radioButtons.nextElement();
                if(currentRadioButton.isSelected()){
                    return currentRadioButton.getText();
                }
            }
            return null;
        }
        private void currentSelectedOption(){
            String selected = getSelectedOption();
            if(selected == null){
                System.out.println("Thee is something wrong! Nothing is selected");
                return;
            }
            switch(selected.toLowerCase()){
                case "Male":
                    System.out.println("Male is selected");
                    break;
                    
                case "female":
                    System.out.println("female is selected");
                    break;
                            
            }
        }
danpost danpost

2023/5/16

#
divinity wrote...
 private void ButtonGroup(){  the error is saying; illegal start of expression
Please provide all codes before this line.
divinity divinity

2023/5/16

#
these are the codes that is before these lines of codes
 
    ButtonGroup btngrp = new ButtonGroup();

    /**
     *
     */
    public Banking() {
        initComponents();
    
    
        //radio button
        JRadioButton female = new JRadioButton("Female");
        JRadioButton male = new JRadioButton("Male");
        
        
        female.setSelected(true);        
        btngrp.add(male);
        btngrp.add(female);
        
        JPanel jpan = new JPanel(new FlowLayout(FlowLayout.CENTER));
        jpan.add(female);
        jpan.add(male);
danpost danpost

2023/5/16

#
divinity wrote...
these are the codes that is before these lines of codes << Code Omitted >>
I do not see a class declaration line anywhere (should be first line if no import lines are given). Would be:
public class Banking extends Object
with everything below it in curly brackets ( '{ }' ). You probably should have an import line or two also. At least, this one:
import javax.swing.*;
which is not the first line (where it must be) that you have shown. Okay, you probably did not provide anything above and including the class declaration line. The last code provided does not have a closing curly bracket at the end ( '}' ), which is probably the cause of all the errors below that code.
divinity divinity

2023/5/17

#
I see the error and put in the curly (}) that I had miss out but now am getting another error
 java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
              new Banking().setVisible(true); the error is set.Visible(true), the error says: cannot find symbol and to create a method for set.Visible which I dont think it should happen
            }
        });
    }
There are more replies on the next page.
1
2