Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


valarmathi
jbutton
1 Answer(s)      4 years and 9 months ago
Posted in : Java Beginners



Advertisement
View Answers

November 24, 2009 at 1:13 PM


Hi Friend,

Try the following code:

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

public class JListExample extends JFrame{
static final String Select = "Sports";
String input = "";
DefaultListModel listModel;
JList list ;
public JListExample() {
super(Select);
JButton button=new JButton("Add");
listModel = new DefaultListModel();
list = new JList(listModel);
JScrollPane pane=new JScrollPane(list);
JPanel panel=new JPanel();

ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] sport = new String[] {"Cricket", "FootBall", "Tennis", "Hockey" };
input = (String) JOptionPane.showInputDialog(JListExample.this,"Please select your favorite sport",Select, JOptionPane.INFORMATION_MESSAGE, null, sport,"Tennis");

listModel.addElement(input);
}
};

button.addActionListener(lst);
panel.add(button);
panel.add(pane);

add(panel);
pack();
setVisible(true);

}
public static void main(String argv[]) {
new JListExample();

}

}

Thanks


Related Tutorials/Questions & Answers:
 

 

 

DMCA.com