Create a CheckBox and a ComboBox in Java - ProgrammerTech
Language:

Search

Create a CheckBox and a ComboBox in Java

  • Share this:
Create a CheckBox and a ComboBox in Java

Create a CheckBox

The Checkbox in Java is a tool that enables the user to select some or all options from a set of options. I mean, when we browse a website or install a program, we will encounter a selection of options and ask to choose from them or all of them....etc.

At first, we call the swing library, as we learned in the previous lessons, which is a package that provides us with many tools, including CheckBox.

Example 1

package cheeeeek;
import javax.swing.*;
public class Cheeeeek{
     public static void main(String []args){
        JFrame f=new JFrame("CheckBox");
        JCheckBox j=new JCheckBox("sajo");
        j.setBounds(100, 100, 50, 100);
        f.add(j);
        f.setSize(500, 500);
        f.setLayout(null);
        f.setVisible(true);
     }
}

We called the required library swing and entered the main program and created an interface class. This is JFrame f=new. We created a JCheckBox j=new class and the text that appears on the checkbox is sajo. Then we started by specifying the location coordinates and sizes of the checkbox and adding it to the interface via the f.add command. Then we determined the size of the square interface with a width of 400 and a length of 400 and we fitted the interface with the elements added to it. We show the interface and its elements via setVisible and take the value true ie show the elements.

And when you run this program, it will be as follows.

1-77.webp

As we can see from the previous interface, a small checkbox appears with a check mark next to it, the name that we put within the program.

 

Create a ComboBox checklist

Checklist ( ComboBox ) The ComboBox is widely used in programs and on Internet sites due to its ease and ease of dealing with the user. We can choose one value from among several and we will learn how to create a ComboBox and how to fill it with values. It is a java swing tool so first we call the swing library.

Example 2

package java_lesson;
import java.awt.event.*;
import javax.swing.*;
public class Java_Lesson{
     public static void main(String []args){
        JFrame f=new JFrame("ComboBox");
        String a[]={"Kaissar","DatabaseHK","SajaCS"};
        JComboBox co=new JComboBox(a);
        co.setBounds(200, 100, 100, 20);
        f.setSize(500, 500);
        f.add(co);
        f.setLayout(null);
        f.setVisible(true);
     }
}

We called the swing library and the event library, then entered the main program and created a class called the interface. Then we define a text array a which has three text values ​​and create a ComboBox class and specify its coordinates. Then we set the width and length of the interface and add them to the interface via add and then we fit the interface with the added elements. And we showed the interface via setVisible.

And when you run this program, the result will be in the form.

2-71.webp

As we can see in the previous image we have created a list of checkboxes and we can choose only one of the available options.

 

Create an event with a ComboBox checklist

In this section, we will learn how to add an event when selecting a value on a ComboBox, for example when we choose. A value or item from the ComboBox will show us the value we chose on the interface.

Example 3

package java_lesson;
import java.awt.event.*;
import javax.swing.*;
public class Java_Lesson{
     public static void main(String []args){
        JFrame f=new JFrame();
        String a[]={"Kaissar","DatabaseHK","SajaCS"};
        JLabel l1=new JLabel();
        final JComboBox co=new JComboBox(a);
        co.setBounds(200, 100, 100, 20);
        l1.setHorizontalAlignment(0);
        l1.setSize(200, 200);
        f.setSize(500, 500);
        f.add(co);
        f.add(l1);
        f.setLayout(null);
        f.setVisible(true);
        co.addActionListener((ActionEvent e)->{
            String txt="Admin acttive " + co.getItemAt(co.getSelectedIndex())
            l1.setText(txt);
        });
     }
}

We called the swing library and the awt event library, and included the JFrame interface's class name, f=new. And we created a text variable, a, and it had three text values, which are the names of the owners of this site. And then we created a tool to show titles and sentences in the JLabel class. Then we set the coordinates via co.setBounds and set the coordinate sizes to the center via setHorizontalAlignment. And then we set the size of the checkbox 200*200 and the interface sizes 500*500 and we added the elements to the interface and its fit and showing the elements in the interface. Then we created the event via the co.addActionListener function and inside this event we created a text variable named txt and wrote a statement in it so that it appears when we choose anything from the list. And the We used the getItemAt function which enables us to get the value you should need. This function is used to determine the index, and we used the getSelectedIndex function to determine the index. And in the end, we put the value in the txt via l1.setText and when we run the program and choose one of the values, it will appear in the form.

3-66.webp

And if we choose another value, it will also appear in the form

4-46.webp

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com