-
Notifications
You must be signed in to change notification settings - Fork 0
/
Swing51.java
40 lines (33 loc) · 1.23 KB
/
Swing51.java
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
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
// Static List creation example
public class Swing51 extends JFrame {
public static void main(String[] args) { new Swing51();}
Swing51() {
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
JList list = new JList(items);
list.setVisibleRowCount(5);
JPanel panel = new JPanel();
panel.add(new JScrollPane(list));
add(panel);
JLabel label = new JLabel();
revalidate();
list.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2) {
int index = list.getSelectedIndex();
String item = String.valueOf(list.getSelectedIndex());
label.setText("Selected item is " + item + "and selected index" + index);
}
}
});
panel.add(label);
}
String[] items = {"item0","item1","item2","item3","item4","item5","item6","item7","item8"};
}