source: src/main/java/genius/gui/panels/ComboboxSelectionPanel.java@ 346

Last change on this file since 346 was 8, checked in by Wouter Pasman, 6 years ago

#3 fix labels to fixed width of 120 px to get nicer layout. Removed right alignment of some comboboxes.

File size: 2.5 KB
Line 
1package genius.gui.panels;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5
6import javax.swing.DefaultListCellRenderer;
7import javax.swing.JComboBox;
8import javax.swing.JLabel;
9import javax.swing.JPanel;
10import javax.swing.ListCellRenderer;
11import javax.swing.event.ListDataEvent;
12import javax.swing.event.ListDataListener;
13
14/**
15 * A GUI that shows panel with given title at the left and a combobox at the
16 * right. In the combo, the user can select an item from a list. This panel
17 * hides automatically if there are no selectable items.
18 *
19 * The text label is set fixed to 120 pixels to get some nice column layout
20 *
21 * @param <ItemType>
22 * the type of the elements in the item list.
23 */
24@SuppressWarnings("serial")
25public class ComboboxSelectionPanel<ItemType> extends JPanel {
26
27 private SingleSelectionModel<ItemType> model;
28 final JComboBox<ItemType> combo;
29
30 /**
31 *
32 * @param title
33 * the text to be placed left of the combo box
34 * @param itemsModel
35 * the {@link SingleSelectionModel} that contains the possible
36 * choices and can be listened for changes.
37 */
38 public ComboboxSelectionPanel(final String title,
39 final SingleSelectionModel<ItemType> itemsModel) {
40 this.model = itemsModel;
41 setLayout(new BorderLayout());
42 combo = new JComboBox<ItemType>(itemsModel);
43
44 JLabel label = new JLabel(title);
45 // sets a FIXED (not preferred?) size for these.
46 label.setPreferredSize(new Dimension(120, 10));
47 add(label, BorderLayout.WEST);
48 add(combo, BorderLayout.CENTER);
49 updateVisibility();
50
51 itemsModel.addListDataListener(new ListDataListener() {
52
53 @Override
54 public void intervalRemoved(ListDataEvent e) {
55 updateVisibility();
56 }
57
58 @Override
59 public void intervalAdded(ListDataEvent e) {
60 updateVisibility();
61 }
62
63 @Override
64 public void contentsChanged(ListDataEvent e) {
65 updateVisibility();
66 }
67
68 });
69 }
70
71 /**
72 * We are visible only if there is at least one possible selection. This
73 * enables us to hide eg the Mediator combobox by just emptying the mediator
74 * list in the model.
75 */
76 private void updateVisibility() {
77 // FIXME invokelater
78 setVisible(model.getSize() > 0);
79 }
80
81 /**
82 * Set the cell renderer.
83 *
84 * @param renderer
85 * a {@link DefaultListCellRenderer}. Note, for unknown reason,
86 * {@link DefaultListCellRenderer} implements
87 * {@link ListCellRenderer}&lt;object&gt; and ignores the proper
88 * typing
89 */
90 public void setCellRenderer(DefaultListCellRenderer renderer) {
91 combo.setRenderer(renderer);
92 }
93
94}
Note: See TracBrowser for help on using the repository browser.