JList class to create a selection list in Java - ProgrammerTech
Language:

Search

JList class to create a selection list in Java

  • Share this:
JList class to create a selection list in Java

JList class to create a list in Java

Class ( JList ) The ListBox is very similar to a ComboBox that you can choose from, meaning it is a selection list, but the difference between ListBox and ComboBox is that ComboBox. You can only see the items when clicking on the ComboBox, but in the ListBox the items appear directly in front of us without clicking on them.

To create a ListBox, we must go through two stages, the first stage is to define the items that will appear in the ListBox, and the second stage is to create a ListBox and then add the elements to it.

package l1
import javax.swing.*;
public class L1{
     public static void main(String []args){
        JFrame f=new JFrame("listbox");
        DefaultListModel<String>l1=new DefaultListModel<>();
        l1.addElement("Saja");
        l1.addElement("Kaissat");
        l1.addElement("Data");
        l1.addElement("Wazer");
        l1.addElement("Parrot");
        JList<String>list=new JList<>(l1);
        list.setBounds(200, 100, 100, 200);
        f.add(list);
        f.setSize(400, 400);
        f.setLayout(null);
        f.setVisible(true);
     }
}

At first, we called the swing library, as usual, which is the library for interfaces. Then we started embedding the interface name class f=new and then we defined ListBox with the package. DefaultListModel We chose it of type String ie the elements will be of type Script. Then we gave it the object with the name l1 and then we added the elements ie creating the elements via the addElement function and we added five elements to the list. Then we create a list using the JList class and specify the coordinates via list.setBounds. We add the elements that we have defined to the list via f.add and specify the width and length of the interface 400*400. Then we fit the elements to the interface via setLayout and show the interface and the elements via setVisible.

And when you run this code, the result will be as shown in the following picture.

1-79.webp

We also note that the list has been created and all the data inside it is shown in front of us, you can choose any item from the list by clicking on it.

 

Get value by addActionListener

Here we will learn how to add an event on a ListBox to extract a value from a ListBox and then show it on the form. We talked at the beginning of the interface tutorials, whenever we create an element such as a button, a label, or a ComboBox we create an event for that element. Events are important We need to learn how to create events for each element so that we can process values ​​within the element or get a value inside the element.

We will now learn when choosing a value from the ListBox and then pressing the button will show the value you chose from the ListBox on the form.

package listbox;
import javax.swing.*;
import java.awt.event.*;
public class Listbox{
    String dat=" ";
    Listbox()
    {
        JFrame f=new JFrame();
        final JLabel label=new JLabel();
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setSize(400, 100);
        JButton b=new JButton("show");
        b.setBounds(200, 150, 80, 30);
        DefaultListModel<String> l1=new DefaultListModel<>();
        l1.addElement("Kaissar");
        l1.addElement("Saja");
        l1.addElement("Parrot");
        l1.addElement("Wazer");
        JList list=new JList<>(l1);
        list.setBounds(100, 100, 150, 150);
        f.add(b);
        f.add(list);
        f.add(label);
        f.setSize(500,500);
        f.setLayout(null);
        f.setVisible(true);
        b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                dat=" " + list.getSelectedValue();
                label.setText(dat);
            }
        });
    }
     public static void main(String []args){
        new Listbox();
     }
}

First we called the swing library and called the event library awt.event. And we created a variable dat to receive a text value. Then we started creating the listbox construction, and within this construction we added a class called JFrame. Then we created a JLabel class and set the sizes to be in the middle via setHorizontalAlignment and the label size to 100*400. And we created a JButton in order for when pressed to execute the desired event. The interface addActionListener is tasked with eavesdropping on the event where the click was made. The actionPerformed function will execute the event. And we called the variable that will get the value from the list, which is dat, via the getSelectedValue function. And inside the main program we called the construction because we created the code inside the construction Listbox.

2-72.webp

As we can see from the image, when we selected the name Wazer from the list and pressed the show button directly, the event was executed and the value appeared on the interface at the top.


Tags:
Muhammad Alush

Muhammad Alush

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

موقعي programmer-tech.com