From cd01fccd05f7a98d3e0f71a86e153d349f7284ac Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 31 Jul 2016 11:25:30 -0400 Subject: New GUI widget A list panel that offers selections from a drop down menu --- .../main/java/bjc/utils/gui/DropdownListPanel.java | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/gui/DropdownListPanel.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/gui/DropdownListPanel.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/DropdownListPanel.java b/BJC-Utils2/src/main/java/bjc/utils/gui/DropdownListPanel.java new file mode 100644 index 0000000..530fca2 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/DropdownListPanel.java @@ -0,0 +1,57 @@ +package bjc.utils.gui; + +import java.awt.BorderLayout; + +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.ListSelectionModel; + +import bjc.utils.funcdata.IList; +import bjc.utils.gui.layout.AutosizeLayout; +import bjc.utils.gui.layout.HLayout; + +public class DropdownListPanel extends JPanel { + private static final long serialVersionUID = 2719963952350133541L; + + @SuppressWarnings("unchecked") + public DropdownListPanel(String itemType, + DefaultListModel listModel, IList choices) { + setLayout(new AutosizeLayout()); + + JPanel itemInputPanel = new JPanel(); + itemInputPanel.setLayout(new BorderLayout()); + + JPanel addItemPanel = new JPanel(); + addItemPanel.setLayout(new HLayout(2)); + + JComboBox addItemBox = new JComboBox<>(); + choices.forEach(addItemBox::addItem); + + JButton addItemButton = new JButton("Add " + itemType); + + addItemPanel.add(addItemBox); + addItemPanel.add(addItemButton); + + JList itemList = new JList<>(listModel); + itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + + JButton removeItemButton = new JButton("Remove " + itemType); + + addItemButton.addActionListener((ev) -> { + listModel.addElement((T) addItemBox.getSelectedItem()); + }); + + removeItemButton.addActionListener((ev) -> { + listModel.remove(itemList.getSelectedIndex()); + }); + + itemInputPanel.add(addItemPanel, BorderLayout.PAGE_START); + itemInputPanel.add(itemList, BorderLayout.CENTER); + itemInputPanel.add(removeItemButton, BorderLayout.PAGE_END); + + add(itemInputPanel); + } +} -- cgit v1.2.3