Design jList and Show data or items in jList in java Netbeans

In java, jList basically is use for showing items according to requirement. It can display items, data or files in static or dynamic way. So, here SKOTechLearn will describe step by step procedure of how to design jList in java and show data or Add items in jList in Java Netbeans.

But we first start with design process of this component. In this process we proceed in following way:

  1. How to Change background and selection color in jList?
  2. How to Change the Selection mode in jList?
  3. How to Show data or items in jList in design time?
  4. How to Show data or items in jList Dynamically?
  5. How to Show items details in Horizontal or Vertical Scrolling way ?

Now let’s start.

jOptionPane with Different type and Style

 (1) How to Change background and Selection color in jList?

In This point we describe the process to change jList Background Color and also we change jList Selection Color in java. In NetBeans IDE you have to follow the steps describe bellow:

  First you have to drag this control to jFrame and go to Properties window.

 Find “background”, “foreground”, “selectionBackground” and “selectionForeground” option.

Then change color according to your desire.

background: this will change the control’s back color.
foreground: this will change font color.
selectionBackground: this will change the color of selected item’s back color.
selectionForeground: this will change the font color of selected item.


Color_Settings_of_jList_in_Java
Color Settings of List in Java
If you don’t want to change it through properties, you can write code as mention bellow:

public MyJframe() {
        initComponents();
        MyLst1.setBackground(new Color(204,255,204));
        MyLst1.setForeground(new Color(204,0,51));
        MyLst1.setSelectionBackground(new Color(255,204,204));
        MyLst1.setSelectionForeground(new Color(0,0,255));
    }


(2) How to Change Selection mode in jList?

Just go to “Properties” and find “selectionmode” option. If you want select one item at a time that means you are using Single selection mode or want to select multiple content then it is in “MULTIPLE_INTERVAL” mode. This properties option will help according to requirement.

✷ For Single item selection, just select “SINGLE”.
✷ For multiple selection, just select “MULTIPLE_INTERVAL”.

Selectionmode_of_jList_in_Java
Selection mode Setting


 (3) How to Show Data in jList or Add items in jList?

If you want to add data or add items in jList static way in design time, you can use Properties window or you can define all in “formWindowOpened” event. First we add data through Properties window:

jRadioButton with Attractive Style and look

Just find “model” option and click on “Custom editor” button like bellow.
add_data_in_list_in_java
Add Data Process
As you can see a “model” screen will appear, just press “Enter” key and input your item name. Then press “OK” button. Now it will be add on list.

Now come to code with static way in application’s opened event.
private void formWindowOpened(java.awt.event.WindowEvent evt) { 
        DefaultListModel MyLstModel = new DefaultListModel();
         MyLstModel.addElement("MyFirstdata_1");
         MyLstModel.addElement("MySecondtdata_2");
         MyLstModel.addElement("MyThirddata_3");
         MyLstModel.addElement("MyForthdata_4");
         MyLstModel.addElement("MyFifthdata_5");
        MyLst1.setModel(MyLstModel);
    }  

When you run application, it will present you the following output.
Static_Way_To_Show_Data_in_JList
Static Way of displaying Data


 (4) How to Show data or items in jList Dynamically?

If we consider about displaying details in list by button’s press. And want to display loop wise content, then we will say it as dynamic in run time process.

 Add jFrame and Design jFrame with Swing Controls

Suppose we have record approx 1 to 100 or more and want to display in list through loop. Then we write following code in jButton’s “ActionPerformed” event.

Show or Add Items in jList through Loop.
private void Show_BtnActionPerformed(java.awt.event.ActionEvent evt) {  
    DefaultListModel MyLstModel = new DefaultListModel();
    for (int ls = 0; ls < 101; ls++) {
           MyLstModel.addElement("SK_"+ ls);
      } 
      MyLst1.setModel(MyLstModel);
 } 

And when you press “Show Items” button the following output will appear.
Dynamic_way_to_Show_Data_in_jList
Dynamic way to displaying Data


 (5) How to Show jList items details in Horizontal or Vertical Scrolling way?

Now come to the last point. Here, you can easily show list details in HORIZONTAL or VERTICAL. You will find an option of “layoutOrientation” it contain three options:

VERTICAL_WRAP
VERTICAL
HORIZONTAL_WRAP


Just select any option and see what will happen at runtime. The details will define bellow.
Orientation_of_List_Details_in_Java
Orientation of List Details
This way you can change the orientation of Details as describe in above image. You can find complete details of jList from here.

So, there is some features and properties of List, by which you can easily display items in java application, Just follow the steps and learn How to Design jList in java and Show Data or Add Items in jList in java Swing Netbeans with SKOTechLearn Tips.

0 comments: