Mariel
Mariel

Reputation: 169

Radio Button does not appear in JFrame

My JRadioButton does not appear in the JFrame.

It should appear below the Method of payment. If the user picks the creditRadioButton, the creditCardLabel and the creditTextField appears.

Code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class FinishTransaction extends JFrame implements ActionListener{
    JLabel totalLabel;
    JLabel nameLabel;
    JLabel addressLabel;
    JLabel contactLabel;
    JLabel custPaymentLabel;
    JLabel changeLabel;
    JLabel methodLabel;
    JLabel creditCardLabel;
    JTextField totalTextField;
    JTextField nameTextField;
    JTextField addressTextField;
    JTextField contactTextField;
    JTextField custPaymentTextField;
    JTextField changeTextField;
    JTextField creditCardTextField;
    final ButtonGroup bGroup = new ButtonGroup();
    final JRadioButton[] buttons = new JRadioButton[2];
    JButton checkoutButton;

    static FinishTransaction fin = new FinishTransaction();

    public FinishTransaction(){
        super("Finish Transaction-Print Receipt");
        setLayout(null);
        totalLabel = new JLabel("Total Payment: ");
        nameLabel = new JLabel("Name: ");
        addressLabel = new JLabel("Address: ");
        contactLabel = new JLabel("Contact: ");
        custPaymentLabel = new JLabel("Payment: ");
        changeLabel = new JLabel("Change: ");
        totalTextField = new JTextField("");
        nameTextField = new JTextField("");
        addressTextField = new JTextField("");
        contactTextField = new JTextField("");
        custPaymentTextField = new JTextField("");
        changeTextField = new JTextField("");
        methodLabel = new JLabel("Method of payment: ");
        creditCardLabel = new JLabel("Credit Card number: ");
        creditCardTextField = new JTextField("");
        checkoutButton = new JButton("Checkout");
        buttons[0] = new JRadioButton("Cash");
        buttons[1] = new JRadioButton("Credit Card");

        totalTextField.setEditable(false);
        changeTextField.setEditable(false);
        creditCardLabel.setVisible(false);
        creditCardTextField.setVisible(false);

        nameLabel.setBounds(50,60,100,20);     nameTextField.setBounds(130,60,200,20);
        addressLabel.setBounds(50,80,100,20);     addressTextField.setBounds(130,80,200,20);
        contactLabel.setBounds(50,100,100,20);     contactTextField.setBounds(130,100,200,20);
        methodLabel.setBounds(50,130,200,20);    
        buttons[0].setBounds(50,150,100,20); 
        creditCardLabel.setBounds(90,170,150,20);     creditCardTextField.setBounds(210,170,200,20);
        buttons[1].setBounds(50,190,100,20);   
        totalLabel.setBounds(50,230,100,20);     totalTextField.setBounds(140,230,200,20);
        custPaymentLabel.setBounds(50,250,100,20);     custPaymentTextField.setBounds(130,250,200,20);
        changeLabel.setBounds(50,270,100,20);     changeTextField.setBounds(130,270,200,20);
                                                    checkoutButton.setBounds(130,310,200,20);

        add(nameLabel);
        add(addressLabel);
        add(contactLabel);
        add(methodLabel);
        bGroup.add(buttons[0]);
        bGroup.add(buttons[1]);
        add(creditCardLabel);
        add(totalLabel);
        add(custPaymentLabel);
        add(changeLabel);
        add(nameTextField);
        add(addressTextField);
        add(contactTextField);
        add(creditCardTextField);
        add(totalTextField);
        add(custPaymentTextField);
        add(changeTextField);
        add(checkoutButton);

        buttons[0].setSelected(true);

        buttons[0].addActionListener(this);
        buttons[1].addActionListener(this);
        checkoutButton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e){
        if(checkoutButton.getName().equals(((Component)e.getSource()).getName())){
        }
        if(buttons[0].isSelected()){
            creditCardLabel.setVisible(true);
            creditCardTextField.setVisible(true);
        }
    }
    public static void main(String args[]){
        fin.setVisible(true);
        fin.setResizable(false);
        fin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fin.setSize(500,400);
    }
}

The output:
enter image description here

Upvotes: 0

Views: 1077

Answers (1)

Bahramdun Adil
Bahramdun Adil

Reputation: 6079

You did not add buttons[0] and buttons[1] to the panel. Try to add them after this line add(methodLabel); and check the result.

add(buttons[0]);
add(buttons[1]);

Upvotes: 3

Related Questions