II. Máseleniń qoyiliwi:
III.Máseleniń sheshiliwi
1.1 Programma kodi
1.
package main;
2.
import java.awt.BorderLayout;
3.
import java.awt.Color;
4.
import java.awt.GridBagConstraints;
5.
import java.awt.GridBagLayout;
6.
import java.awt.Insets;
7.
import java.awt.event.ActionEvent;
8.
import java.awt.event.ActionListener;
9.
import java.util.Arrays;
10.
import java.util.Collection;
11.
import java.util.Iterator;
12.
import java.util.SortedSet;
13.
import java.util.TreeSet;
14.
import javax.swing.AbstractListModel;
15.
import javax.swing.BorderFactory;
16.
import javax.swing.JButton;
17.
import javax.swing.JFrame;
18.
import javax.swing.JLabel;
19.
import javax.swing.JList;
20.
import javax.swing.JPanel;
21.
import javax.swing.JScrollPane;
22.
import javax.swing.ListCellRenderer;
23.
import javax.swing.ListModel;
24.
public class Main{
25.
public static void main(String args[]) {
26.
JFrame f = new JFrame("5-laborotoriya");
27.
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
28.
DualListBox dual = new DualListBox();
29.
dual.addSourceElements(new String[]{"bir", "eki", "ush"});
30.
dual.addSourceElements(new String[]{"tort", "bes", "alti"});
31.
dual.addSourceElements(new String[]{"jeti", "segiz", "tog'iz"});
32.
dual.addSourceElements(new String[]{"on", "on bir", "on eki"});
33.
dual.addSourceElements(new String[]{"on ush", "on tort", "on bes"});
34.
dual.addSourceElements(new String[]{"on alti", "on jeti", "on segiz"});
35.
dual.addSourceElements(new String[]{"on tog'iz", "jigirma", "jigirma bir"});
36.
f.getContentPane().add(dual, BorderLayout.CENTER);
37.
f.setBounds(450, 200,450,300);
38.
f.setVisible(true);
39.
}
40.
}
41.
class DualListBox extends JPanel {
42.
private static final Insets EMPTY_INSETS = new Insets(0, 0, 0, 0);
43.
private static final String ADD_BUTTON_LABEL = "Qosiw>>";
44.
private static final String REMOVE_BUTTON_LABEL = "<< O'shiriw";
45.
private static final String DEFAULT_SOURCE_CHOICE_LABEL = "berilgen sanlar";
46.
private static final String DEFAULT_DEST_CHOICE_LABEL = "sizin' sanlarin'iz";
47.
private JLabel sourceLabel;
48.
private JList sourceList;
49.
private SortedListModel sourceListModel;
50.
private JList destList;
51.
private SortedListModel destListModel;
52.
private JLabel destLabel;
53.
private JButton addButton;
54.
private JButton removeButton;
55.
public DualListBox() {
56.
initScreen();
57.
}
58.
public String getSourceChoicesTitle() {
59.
return sourceLabel.getText();
60.
}
61.
public void setSourceChoicesTitle(String newValue) {
62.
sourceLabel.setText(newValue);
63.
}
64.
public String getDestinationChoicesTitle() {
65.
return destLabel.getText();
66.
}
67.
public void setDestinationChoicesTitle(String newValue) {
68.
destLabel.setText(newValue);
69.
}
70.
public void clearSourceListModel() {
71.
sourceListModel.clear();
72.
}
73.
public void clearDestinationListModel() {
74.
destListModel.clear();
75.
}
76.
public void addSourceElements(ListModel newValue) {
77.
fillListModel(sourceListModel, newValue);
78.
}
79.
public void setSourceElements(ListModel newValue) {
80.
clearSourceListModel();
81.
addSourceElements(newValue);
82.
}
83.
public void addDestinationElements(ListModel newValue) {
84.
fillListModel(destListModel, newValue);
85.
}
86.
private void fillListModel(SortedListModel model, ListModel newValues) {
87.
int size = newValues.getSize();
88.
for (int i = 0; i < size; i++) {
89.
model.add(newValues.getElementAt(i));
90.
}
91.
}
92.
public void addSourceElements(Object newValue[]) {
93.
fillListModel(sourceListModel, newValue);
94.
}
95.
public void setSourceElements(Object newValue[]) {
96.
clearSourceListModel();
97.
addSourceElements(newValue);
98.
}
99.
public void addDestinationElements(Object newValue[]) {
100.
fillListModel(destListModel, newValue);
101.
}
102.
private void fillListModel(SortedListModel model, Object newValues[]) {
103.
model.addAll(newValues);
104.
}
105.
public Iterator sourceIterator() {
106.
return sourceListModel.iterator();
107.
}
108.
public Iterator destinationIterator() {
109.
return destListModel.iterator();
110.
}
111.
public void setSourceCellRenderer(ListCellRenderer newValue) {
112.
sourceList.setCellRenderer(newValue);
113.
}
114.
public ListCellRenderer getSourceCellRenderer() {
115.
return sourceList.getCellRenderer();
116.
}
117.
public void setDestinationCellRenderer(ListCellRenderer newValue) {
118.
destList.setCellRenderer(newValue);
119.
}
120.
public ListCellRenderer getDestinationCellRenderer() {
121.
return destList.getCellRenderer();
122.
}
123.
public void setVisibleRowCount(int newValue) {
124.
sourceList.setVisibleRowCount(newValue);
125.
destList.setVisibleRowCount(newValue);
126.
}
127.
public int getVisibleRowCount() {
128.
return sourceList.getVisibleRowCount();
129.
}
130.
public void setSelectionBackground(Color newValue) {
131.
sourceList.setSelectionBackground(newValue);
132.
destList.setSelectionBackground(newValue);
133.
}
134.
public Color getSelectionBackground() {
135.
return sourceList.getSelectionBackground();
136.
}
137.
138.
public void setSelectionForeground(Color newValue) {
139.
sourceList.setSelectionForeground(newValue);
140.
destList.setSelectionForeground(newValue);
141.
}
142.
public Color getSelectionForeground() {
143.
return sourceList.getSelectionForeground();
144.
}
145.
private void clearSourceSelected() {
146.
Object selected[] = sourceList.getSelectedValues();
147.
for (int i = selected.length - 1; i >= 0; --i) {
148.
sourceListModel.removeElement(selected[i]);
149.
}
150.
sourceList.getSelectionModel().clearSelection();
151.
}
152.
private void clearDestinationSelected() {
153.
Object selected[] = destList.getSelectedValues();
154.
for (int i = selected.length - 1; i >= 0; --i) {
155.
destListModel.removeElement(selected[i]);
156.
}
157.
destList.getSelectionModel().clearSelection();
158.
}
159.
private void initScreen() {
160.
setBorder(BorderFactory.createEtchedBorder());
161.
setLayout(new GridBagLayout());
162.
sourceLabel = new JLabel(DEFAULT_SOURCE_CHOICE_LABEL);
163.
sourceListModel = new SortedListModel();
164.
sourceList = new JList(sourceListModel);
165.
add(sourceLabel, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, EMPTY_INSETS, 0, 0));
166.
add(new JScrollPane(sourceList), new GridBagConstraints(0, 1, 1, 5, .5, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, EMPTY_INSETS, 0, 0));
167.
addButton = new JButton(ADD_BUTTON_LABEL);
168.
add(addButton, new GridBagConstraints(1, 2, 1, 2, 0, .25, GridBagConstraints.CENTER,
GridBagConstraints.NONE, EMPTY_INSETS, 0, 0));
169.
addButton.addActionListener(new AddListener());
170.
removeButton = new JButton(REMOVE_BUTTON_LABEL);
171.
add(removeButton, new GridBagConstraints(1, 4, 1, 2, 0, .25, GridBagConstraints.CENTER,
GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
172.
removeButton.addActionListener(new RemoveListener());
173.
destLabel = new JLabel(DEFAULT_DEST_CHOICE_LABEL);
174.
destListModel = new SortedListModel();
175.
destList = new JList(destListModel);
176.
add(destLabel, new GridBagConstraints(2, 0, 1, 1, 0, 0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, EMPTY_INSETS, 0, 0));
177.
add(new JScrollPane(destList), new GridBagConstraints(2, 1, 1, 5, .5, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, EMPTY_INSETS, 0, 0));
178.
}
179.
private class AddListener implements ActionListener {
180.
181.
public void actionPerformed(ActionEvent e) {
182.
Object selected[] = sourceList.getSelectedValues();
183.
addDestinationElements(selected);
184.
clearSourceSelected();
185.
}
186.
}
187.
private class RemoveListener implements ActionListener {
188.
189.
public void actionPerformed(ActionEvent e) {
190.
Object selected[] = destList.getSelectedValues();
191.
addSourceElements(selected);
192.
clearDestinationSelected();
193.
}
194.
}
195.
}
196.
class SortedListModel extends AbstractListModel {
197.
198.
SortedSet model;
199.
200.
public SortedListModel() {
201.
model = new TreeSet();
202.
}
203.
204.
public int getSize() {
205.
return model.size();
206.
}
207.
public Object getElementAt(int index) {
208.
return model.toArray()[index];
209.
}
210.
public void add(Object element) {
211.
if (model.add(element)) {
212.
fireContentsChanged(this, 0, getSize());
213.
}
214.
}
215.
public void addAll(Object elements[]) {
216.
Collection c = Arrays.asList(elements);
217.
model.addAll(c);
218.
fireContentsChanged(this, 0, getSize());
219.
}
220.
public void clear() {
221.
model.clear();
222.
fireContentsChanged(this, 0, getSize());
223.
}
224.
public boolean contains(Object element) {
225.
return model.contains(element);
226.
}
227.
public Object firstElement() {
228.
return model.first();
229.
}
230.
public Iterator iterator() {
231.
return model.iterator();
232.
}
233.
public Object lastElement() {
234.
return model.last();
235.
}
236.
public boolean removeElement(Object element) {
237.
boolean removed = model.remove(element);
238.
if (removed) {
239.
fireContentsChanged(this, 0, getSize());
240.
}
241.
return removed;
242.
}
243.
}
Do'stlaringiz bilan baham: |