Monday, 8 April 2019

JList in Java | Online Help

JList in java:

The JList is used to let the user select one or more items from the list. The JList class defines the following constructors:

JList():

It is used to create an empty JList with default four rows visible.

JList(vector v)

It is used to create a JList with the specified vector object.

Methods for JList in Java:

The most commonly used JList() method is as follows:

void addItem(object obj)

This method is used to add a new object to the list.

int getSelectedIndex()

This method is used to return the index of the selected item.

int[] getSelectedItem()

This method is used to return the string value of the item selected.

Sample program for JList in Java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class jlistdemo
{
public static void main(String args[])
{
String[] str={"professor","Associate professor","Assistant professor")
JFrame jf=new JFrame("JListDemo");
JList j1=new JList(str);
jf.getContentPane().add(j1,BorderLayout.CENTER);
jf.pack();
jf.setVisible(true);
}
}

No comments:

Post a Comment