Site icon FahmidasClassroom

Use of Java JFrame with Radio buttons, Checkbox, Dropdown list, and TextArea

P3

The way of creating form with a label, textbox, and button by using Java swing control has been shown in this tutorial. Follow the steps properly to create the java application.

Steps:

01. Create a Java application with the class named JavaForm.java.

02. Add the following lines before the class declaration to import necessary packages for the application

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

03. Add the following lines inside the main method to create a Form with a label and a text field.

//Create a JFrame Object
JFrame frm = new JFrame();
 
//Create label object and set the position 
JLabel lbl = new JLabel("Name:", JLabel.LEFT);
lbl.setBounds(80,20,60,20);
//Add label to frame
frm.add(lbl);

//Create text object and set position
JTextField txt = new JTextField();
txt.setBounds(210,20,200,20);
frm.add(txt);

04. Add the following lines to add a label, two radio buttons with the buttongroup in the form.

//Create label object and set the position 
JLabel lbl2 = new JLabel("Gender:", JLabel.LEFT);
lbl2.setBounds(80,60,60,20);
//Add label to frame
frm.add(lbl2);
      
//Create the radio buttons
JRadioButton rbtn1 = new JRadioButton("Male");
rbtn1.setActionCommand("Male");
JRadioButton rbtn2 = new JRadioButton("Female");
rbtn2.setActionCommand("Female");
      
//Set the position of the radio buttons
rbtn1.setBounds(210,45,60,50);  
rbtn2.setBounds(270,45,100,50);  

//Add radio buttons to group
ButtonGroup bg = new ButtonGroup();  
bg.add(rbtn1);
bg.add(rbtn2);  
     
//Add buttons to frame
frm.add(rbtn1);
frm.add(rbtn2);   

05. Add the following lines to add a label, two checkboxes with a panel in the form.

//Create label object and set the position 
JLabel lbl3 = new JLabel("Interest:", JLabel.LEFT);
lbl3.setBounds(80,100,60,20);
//Add label to frame
frm.add(lbl3);
      
//Create a checkbox
JCheckBox chk1 = new JCheckBox("Music"); 
JCheckBox chk2 = new JCheckBox("Swimming"); 
//Create a new panel
JPanel p = new JPanel(); 
//Add a checkbox to the panel 
p.add(chk1); 
p.add(chk2); 

//Set the position of the label
p.setBounds(160,90,250,30);
//Add panel to frame 
frm.add(p); 

06. Add the following lines to add a label, a dropdown list of 5 elements with a panel in the form.

//Create label object and set the position 
JLabel lbl4 = new JLabel("Favourite Place:", JLabel.LEFT);
lbl4.setBounds(80,140,100,20);
//Add label to frame
frm.add(lbl4);
      
//Create array of strings 
String str[] = { "Bangladesh", "USA", "UK", "Germany", "Canada" }; 

//Create combobox object using the array values  
JComboBox combobox = new JComboBox(str); 
      
//Create a new panel
JPanel p2 = new JPanel();
//Add the combobox into the panel      
p2.add(combobox); 
//Set the panel position
p2.setBounds(140,130,250,30);
//Add the panel to the frame
frm.add(p2); 

07. Add the following lines to add a label and a textarea in the form.

//Create label object and set the position 
JLabel lbl5 = new JLabel("Details:", JLabel.LEFT);
lbl5.setBounds(80,180,100,20);
//Add label to frame
frm.add(lbl5);

//Create textarea object and set the position         
JTextArea txtArea = new JTextArea();
txtArea.setBounds(220,180,250,80);
//Add textarea to frame
frm.add(txtArea); 

08. Add the following lines to add Submit and Exit buttons in the form.

//Create button
JButton btn1 = new JButton("Submit");
//Set button position
btn1.setBounds(150,270,80,20);
//Add button to frame
frm.add(btn1);

//Create button
JButton btn2 = new JButton("Exit");
//Set button position
btn2.setBounds(250,270,80,20);       
//Add button to frame
frm.add(btn2);

09. Add the following lines to set the form size and layout, and display the form in the center of the screen.

//Set the size of the form
frm.setSize(500, 350);  
//Set the layout  
frm.setLayout(null);
//Set the title of the form
frm.setTitle("Welecome to fahmidasclassroom");
//set form in the center of the screen
frm.setLocationRelativeTo(null);      
//Display the form 
frm.setVisible(true);

10. Add the following lines to add event handler code for the Submit button that will read the values of the swing controls.

btn1.addActionListener((ActionEvent e) -> {
          String val = txt.getText();
          //ButtonModel b = bg.getSelection();
          val += "\nGender:"+ bg.getSelection().getActionCommand();
          val += "\nInterest:";
          if(chk1.isSelected())  val += "\n"+chk1.getText();
          if(chk2.isSelected())  val += "\n"+chk2.getText();
          val += "\nPlace:"+ combobox.getSelectedItem().toString();
          val += "\nDetails:";
          val += "\n"+txtArea.getText();
          JOptionPane.showConfirmDialog(null, "name:"+val, "Info", JOptionPane.DEFAULT_OPTION, 
        JOptionPane.INFORMATION_MESSAGE);
});

11. Add the following lines to add event handler code for the Exit button that will close the application.

btn2.addActionListener((ActionEvent e) -> {
    System.exit(0);
});

12. Now, you can run the application.

You can check the video to do the steps properly shown in this tutorial.

Exit mobile version