-1

I am new to Java and want to start with making simple user input fields without MySQL. Until now I got two problems that I can't solve. First of all, how to get inputs from JCheckBox and JRadioButton? And I get these user inputs in console, but how to get it to show just below the registration form in panel?

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login implements ActionListener {
    private static JTextField nameText;
    private static JTextField emailText;
    private static JPasswordField passwordText;
    private static JPasswordField confirmPasswordText;

    public void loginForm() {
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();
        frame.setSize(600, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.setTitle("Registration Form");
        panel.setLayout(null);
        JLabel headingLabel = new JLabel("REGISTRATION FORM");
        headingLabel.setBounds(285, 25, 160, 25);
        panel.add(headingLabel);
        JLabel nameLabel = new JLabel("Name");
        nameLabel.setBounds(150, 70, 80, 25);
        panel.add(nameLabel);
        nameText = new JTextField(20);
        nameText.setBounds(270, 70, 165, 25);
        panel.add(nameText);
        JRadioButton maleButton = new JRadioButton("Male");
        maleButton.setBounds(270, 100, 60, 25);
        panel.add(maleButton);
        JRadioButton femaleButton = new JRadioButton("Female");
        femaleButton.setBounds(370, 100, 100, 25);
        panel.add(femaleButton);
        JLabel emailLabel = new JLabel("E-mail");
        emailLabel.setBounds(150, 130, 80, 25);
        panel.add(emailLabel);
        emailText = new JTextField(20);
        emailText.setBounds(270, 130, 165, 25);
        panel.add(emailText);
        JLabel passwordLabel = new JLabel("Password");
        passwordLabel.setBounds(150, 160, 80, 25);
        panel.add(passwordLabel);
        passwordText = new JPasswordField();
        passwordText.setBounds(270, 160, 165, 25);
        panel.add(passwordText);
        JLabel confirmPasswordLabel = new JLabel("Confirm password");
        confirmPasswordLabel.setBounds(150, 190, 120, 25);
        panel.add(confirmPasswordLabel);
        confirmPasswordText = new JPasswordField();
        confirmPasswordText.setBounds(270, 190, 165, 25);
        panel.add(confirmPasswordText);
        JCheckBox c1 = new JCheckBox("I agree to websites rules!");
        c1.setBounds(260, 220, 200, 25);
        panel.add(c1);
        JButton button = new JButton("Submit");
        button.setBounds(300, 260, 100, 25);
        button.addActionListener(new Login());
        panel.add(button);
        JLabel success = new JLabel();
        success.setBounds(260, 290, 300, 25);
        panel.add(success);

        frame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String name = nameText.getText();
//        String male = maleButton.getText();
//        String female = femaleButton.getText();
        String email = emailText.getText();
        String password = passwordText.getText();
        String confirmPassword = confirmPasswordText.getText();
//        String c1 = String.valueOf(JCheckBox.getDefaultLocale());
        System.out.println(name + ", " + email + ", " + password + ", " + confirmPassword);
    }
}

Image

Abra
  • 19,142
  • 7
  • 29
  • 41
KORENS
  • 13
  • 4
  • 1
    you can "get" the values by using the getters of those objects. You can set a text by using the set, or setValue/setText/... method of the target object – Stultuske Jun 09 '22 at 11:20
  • @Stultuske ive tried some get functions, but I still dont get what I need – KORENS Jun 09 '22 at 11:34
  • "some get functions", either you use the wrong ones, or you use them incorrectly – Stultuske Jun 09 '22 at 11:38
  • @Stultuske thats why im here.. – KORENS Jun 09 '22 at 12:04
  • so, be specific, which get methods do you use? when/how do you use them? what is the result you get? .. – Stultuske Jun 09 '22 at 12:07
  • did you delete and repost your previous question? If so, don't don't don't - all comments will have to be repeated, wasting time -yours and Kurs! – kleopatra Jun 09 '22 at 12:12
  • [mcve] required - demonstrating the problem, how you tried to solve it and how that didnt work. – kleopatra Jun 09 '22 at 12:15
  • unrelated - never-ever do any manual sizing/positioning of components, instead use a suitable layout manager – kleopatra Jun 09 '22 at 12:17
  • @kleopatra No i didnt delete any questions and reposted them. And thanks! From those JCheckboxes and JButtons from getters i get only true or false, how can I get the gender name instead.. – KORENS Jun 09 '22 at 12:26
  • 1
    `JCheckBox` and `JRadioButton` are just extensions of `JButton`. *"how can I get the gender name instead.."* - You'd base it on the variable name, `maleButton.isSelected()` for example. Honestly, most of this is demonstrated in [How to Use Buttons, Check Boxes, and Radio Buttons](https://docs.oracle.com/javase/tutorial/uiswing/components/button.html) – MadProgrammer Jun 09 '22 at 12:34
  • @MadProgrammer thank you for this source, very helpful! Now I just need to figure out how to place user input onto label. – KORENS Jun 09 '22 at 12:47
  • @KORENS myLabel.setText(""); -> instead of "" put the information you want there – Stultuske Jun 09 '22 at 12:51
  • thanks for the info - then I mis-remembered:) – kleopatra Jun 09 '22 at 13:57

1 Answers1

1

Below code is a rewrite of your GUI application.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Login {
    private JCheckBox c1;
    private JPasswordField confirmPasswordText;
    private JPasswordField passwordText;
    private JRadioButton femaleButton;
    private JRadioButton maleButton;
    private JTextArea textArea;
    private JTextField emailText;
    private JTextField nameText;

    private void createAndDisplayGui() {
        JFrame frame = new JFrame("Registration");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createHeading(), BorderLayout.PAGE_START);
        frame.add(createForm(), BorderLayout.CENTER);
        frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JButton createButton(String text,
                                 int mnemonic,
                                 ActionListener listener) {
        JButton button = new JButton(text);
        button.setMnemonic(mnemonic);
        button.addActionListener(listener);
        return button;
    }

    private JPanel createButtonsPanel() {
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.add(createButton("Submit", KeyEvent.VK_S, this::submit));
        return buttonsPanel;
    }

    private JPanel createForm() {
        JPanel form = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.LINE_START;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets.bottom = 5;
        gbc.insets.left = 10;
        gbc.insets.right = 10;
        gbc.insets.top = 0;
        JLabel nameLabel = new JLabel("Name");
        form.add(nameLabel, gbc);
        gbc.gridx = 1;
        nameText = new JTextField(16);
        form.add(nameText, gbc);
        gbc.gridy = 1;
        form.add(createRadioButtons(), gbc);
        gbc.gridx = 0;
        gbc.gridy = 2;
        JLabel eMailLabel = new JLabel("E-mail");
        form.add(eMailLabel, gbc);
        gbc.gridx = 1;
        emailText = new JTextField(16);
        form.add(emailText, gbc);
        gbc.gridx = 0;
        gbc.gridy = 3;
        JLabel passwordLabel = new JLabel("Password");
        form.add(passwordLabel, gbc);
        gbc.gridx = 1;
        passwordText = new JPasswordField(16);
        form.add(passwordText, gbc);
        gbc.gridx = 0;
        gbc.gridy = 4;
        JLabel confirmLabel = new JLabel("Confirm password");
        form.add(confirmLabel, gbc);
        gbc.gridx = 1;
        confirmPasswordText = new JPasswordField(16);
        form.add(confirmPasswordText, gbc);
        gbc.gridx = 1;
        gbc.gridy = 5;
        c1 = new JCheckBox("I agree to websites rules!");
        form.add(c1, gbc);
        gbc.gridx = 1;
        gbc.gridy = 6;
        textArea = new JTextArea(2, 30);
        textArea.setEditable(false);
        textArea.setFocusable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        form.add(scrollPane, gbc);
        return form;
    }

    private JPanel createHeading() {
        JPanel heading = new JPanel();
        JLabel label = new JLabel("REGISTRATION FORM");
        label.setFont(label.getFont().deriveFont(Font.BOLD, 14.0f));
        heading.add(label);
        return heading;
    }

    private JPanel createRadioButtons() {
        JPanel radioButtons = new JPanel();
        ButtonGroup group = new ButtonGroup();
        maleButton = new JRadioButton("Male");
        maleButton.setSelected(true);
        radioButtons.add(maleButton);
        group.add(maleButton);
        femaleButton = new JRadioButton("Female");
        radioButtons.add(femaleButton);
        group.add(femaleButton);
        return radioButtons;
    }

    private void submit(ActionEvent event) {
        textArea.setText("");
        String name = nameText.getText();
        textArea.append(name);
        String gender;
        if (maleButton.isSelected()) {
            gender = "male";
        }
        else {
            gender = "female";
        }
        textArea.append(", " + gender);
        String email = emailText.getText();
        textArea.append(", " + email);
        String password = new String(passwordText.getPassword());
        textArea.append(", " + password);
        String confirmPassword = new String(confirmPasswordText.getPassword());
        textArea.append(", " + confirmPassword);
        textArea.append(", " + c1.isSelected());
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new Login().createAndDisplayGui());
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41