Featured Post

Step Wise Project Planning

Planning is the most difficult process in project management. The framework described is called the Stepwise method to help to distinguis...

  1. Home

Write a program to create a list using Swings

Write a program to create a list using Swings

import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
public class SwingList implements ListSelectionListener
{
            JTextField tf1,tf2;
            JList l;
           
public SwingList()
{
JFrame f=new JFrame("list eg");
f.setLayout(new GridBagLayout());
f.setVisible(true);
f.setSize(280,200);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
f.add(p1);
f.add(p2);
p1.setSize(80,60);
p2.setSize(60,70);
p2.setLayout(new GridLayout(2,2));
 tf1=new JTextField(10);
 tf2=new JTextField(10);
p2.add(new Label("selected index"));
p2.add(tf1);
p2.add(new Label("selected city"));
p2.add(tf2);
 l=new JList(new String[]{"Sonipat","Panipat","Karnal","Ambala"});
p1.add(l);
l.addListSelectionListener(this);
}
public static void main(String args[])
{
new SwingList();
}
public void valueChanged(ListSelectionEvent e)
{
int i=l.getSelectedIndex();
String str=(String)l.getSelectedValue();
tf1.setText(i+1+"");
tf2.setText(str);
}
}

Output


Previous
Next Post »