Advertisement
Constructor | Description |
---|---|
public JList() | Creates a JList with an empty, read-only, model. |
public JList(E[] items) | Creates a JList that is populated with the items of specfied array. |
public JList(ListModel <E>) | Creates a JList that displays elements from the specified ListModel. |
Methods | Description |
---|---|
public void addListSelectionListener(ListSelectionListener listener) | Gets a String message of JList. |
public void clearSelection() | Appends the text to the JList. |
public E getSelectedValue()) | Gets the total number of rows in JList. |
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class List1
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});
}//Closing the main method
}//Closing the class Combo
class A
{
String [] seasons;
JFrame jf;
JList<String> list;
JLabel label1;
A()
{
seasons= new String[]{"Spring", "Summer", "Monsoon", "Autumn", "Winter"};
jf= new JFrame("JList");
list= new JList<String>(seasons);
label1 = new JLabel("Select your favorite season from the list :");
jf.add(label1);
jf.add(list);
jf.setLayout(new FlowLayout());
jf.setSize(260,200);
jf.setVisible(true);
}
}
Advertisement
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class List2
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});
}//Closing the main method
}//Closing the class Combo
class A //implements ActionListener
{
String [] continents;
JFrame jf;
DefaultListModel<String> list1;
JList<String> list2;
JLabel label1;
A()
{
list1= new DefaultListModel<String>();
list1.addElement("Asia");
list1.addElement("Africa");
list1.addElement("North America");
list1.addElement("South America");
list1.addElement("Antarctica");
list1.addElement("Europe");
list1.addElement("Australia");
list2= new JList<String>(list1);
jf= new JFrame("JList");
label1= new JLabel("Name of all the continents in the world -");
jf.add(label1);
jf.add(list2);
jf.setLayout(new FlowLayout());
jf.setSize(300,250);
jf.setVisible(true);
}
}
When you run the code, you are presented a window shown in the Figure2. This window displays a list containing all the continents in our world -:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
public class list3
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});
}//Closing the main method
}//Closing the class Combo4
class A implements ListSelectionListener
{
String [] fav;
JFrame jf;
JList<String> list;
JLabel label1, label2;
A()
{
fav = new String[]{"USA", "Switzerland", "India", "New Zealand", "Iceland"};
jf= new JFrame("JList");
list= new JList<String>();
label1 = new JLabel("Please select a country to visit");
label2 = new JLabel();
jf.add(label1);
jf.add(list);
jf.add(label2);
list.addListSelectionListener(this);
jf.setLayout(new FlowLayout());
jf.setSize(250,200);
jf.setVisible(true);
}
public void valueChanged(ListSelectionEvent e)
{
if (!e.getValueIsAdjusting())
{
label2.setText( (String)list.getSelectedValue() + " is selected from the list");
}
}
}
When you run the code, you are presented a window shown in the Figure3. In this window you are asked to select one of the countries in the list that you would like to visit -:
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement