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:
- How to Change background and selection color in jList?
- How to Change the Selection mode in jList?
- How to Show data or items in jList in design time?
- How to Show data or items in jList Dynamically?
- 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.
➤ 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.
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 List in Java |
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”.
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 Process |
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 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 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 |
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: