Select Menu

Combined Post

Mag Posts

Theme images by konradlew. Powered by Blogger.

Blogger templates

Blogger news

Facebook

Advertising

Blogroll

Popular Posts

Combined Posts 2

Mag Post 2

2 Column Post

Simple Post

Simple Post 2

New Carousel

Video Posts

» » How to create a simple list program using java!


SLVIKI 12:57 AM 0

This simple program demonstrate a simple JList, which holds a list of names. Each time a name is selected in the list, a ListSelectionEvent is generated, which is handled by the valueChanged() method defined by ListSelectionListener. It responds by obtaining the index of the selected item and displaying the corresponding name.



Here is the Java code for the program


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Demonstrate a simple JList.

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

class ListDemo implements ListSelectionListener {
 JList jlst;
 JLabel jlab;
 JScrollPane jscrlp;

 // Create an array of names.
 String names[] = {"Sherry", "Jon", "Rachel", "Sasha", "Josselyn", "Randy", "Tom", "Mary", "Ken", "Andrew", "Matt", "Todd"};
 
 ListDemo() {
  // Create a new JFrame container.
  JFrame jfrm = new JFrame("JList Demo");
  
  // Specify a flow Layout.
  jfrm.setLayout(new FlowLayout());

  // Give the frame an initial size.
  jfrm.setSize(200, 160);

  // Terminate the program when the user closes the application.
  jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  // Create a JList.
  jlst = new JList(names);

  // Set the list selectin mode to single-selection.
  jlst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

  // Add list to a scroll pane.
  jscrlp = new JScrollPane(jlst);

  // Set the preffered size of the scroll pane.
  jscrlp.setPreferredSize(new Dimension(120,90));

  // set a label that displays the selection.
  jlab = new JLabel("Please choose a name");

  // Add ist selectin handler.
  jlst.addListSelectionListener(this);

  // Add the list and the label to the content pane.
  jfrm.add(jscrlp);
  jfrm.add(jlab);

  // Display the frame.
  jfrm.setVisible(true); 
  
 }

 // Handle the list selection events.
 public void valueChanged(ListSelectionEvent le) {
  // Get the index of the changed item.
  int idx = jlst.getSelectedIndex();

  // Display selection, if item was selected.
  if(idx != -1)
   jlab.setText("Current selection: " + names[idx]);
  else // Otherwise, reprompt.
   jlab.setText("Please choose a name");
 } 

 public static void main(String args[]) {
  // Create the frame on the event dispatching thread.
  SwingUtilities.invokeLater(new Runnable() {
   public void run() {
    new ListDemo();
   }
  });
 }
}


Here is the output of the above program when you compile and run the code.



«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply