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);
}
}
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());
}
}
JRadioButton
s in a ButtonGroup
to ensure that only one can be selected. Refer to How to Use Buttons, Check Boxes, and Radio ButtonsgetText
is deprecated for JPasswordField
. Refer to How to Use Password Fields.JTextArea
in a JScrollPane
but there are other options. Refer to Using Text Components and How to Use Scroll Panes.ActionListener
interface can be implemented via a method reference.