1 package com.instantbank.common.uiutils;
2
3 import javax.swing.JDialog;
4 import javax.swing.JPanel;
5 import javax.swing.JList;
6 import javax.swing.JScrollPane;
7 import javax.swing.JButton;
8 import javax.swing.ListSelectionModel;
9 import java.awt.Container;
10 import java.awt.BorderLayout;
11 import java.awt.event.ActionListener;
12 import java.awt.event.ActionEvent;
13 import javax.swing.border.EmptyBorder;
14 import javax.swing.border.TitledBorder;
15 import javax.swing.border.EtchedBorder;
16
17
23 public class GetLineFromListDialog extends JDialog {
24
27 private JPanel pnlCtrl;
28
31 private JPanel pnlInf;
32
35 private JList lstLines;
36
39 private JScrollPane scrNames;
40
43 private JButton btnOK = new JButton("OK");
44
47 private JButton btnCancel = new JButton("Cancel");
48
49
52 private Object[] answer;
53
56 public static final int OK = 1;
57
60 public static final int CANCEL = 2;
61
62
63
83 public GetLineFromListDialog(String title, String msg, String[] lines,
84 Object[] answer) {
85
86 this.setModal(true);
87 this.answer = answer;
88 this.setTitle(title);
89 this.setSize(300, 250);
90 Container ctp = this.getContentPane();
91 ctp.setLayout(new BorderLayout());
92
93 pnlCtrl = new JPanel();
94 pnlCtrl.setBorder(new EmptyBorder(5, 5, 5, 5));
95 pnlCtrl.setLayout(new BorderLayout());
96
97 lstLines = new JList();
98 lstLines.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
99 lstLines.setListData(lines);
100 scrNames = new JScrollPane(lstLines);
101 scrNames.setBorder(new TitledBorder(new EtchedBorder(), msg));
102
103 pnlCtrl.add(scrNames, BorderLayout.CENTER);
104 ctp.add(pnlCtrl, BorderLayout.CENTER);
105
106 pnlInf = new JPanel();
107 pnlInf.add(btnOK);
108 pnlInf.add(btnCancel);
109 ctp.add(pnlInf, BorderLayout.SOUTH);
110
111 btnOK.addActionListener(
112 new ActionListener() {
113 public void actionPerformed(ActionEvent e) {
114 if(lstLines.isSelectionEmpty()) {
115
116 GetLineFromListDialog.this.answer[1] = null;
117 }
118 else {
119 GetLineFromListDialog.this.answer[1] =
120 lstLines.getSelectedValue().toString();
121 }
122 GetLineFromListDialog.this.answer[0] = new Integer(OK);
123 GetLineFromListDialog.this.dispose();
124 }
125 });
126
127 btnCancel.addActionListener(
128 new ActionListener() {
129 public void actionPerformed(ActionEvent e) {
130 GetLineFromListDialog.this.answer[0] = new Integer(CANCEL);
131 GetLineFromListDialog.this.dispose();
132 }
133 });
134
135 }
136 }
137