diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-07-29 23:18:54 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-07-29 23:18:54 -0400 |
| commit | 4845a4d96944369e8350f15b25384bcc53c35526 (patch) | |
| tree | 5909ef9001b53564b496edcaa339e594ce1145a9 /BJC-Utils2/src/main/java/bjc/utils/gui/SimpleListPanel.java | |
| parent | dca8e9f586fd595a7995f07788318fb92b8cce79 (diff) | |
Added some new GUI utilities
* One is an adaptation of the simple dialogs to work on JDesktopPanes
* The other is a simple control for lists of strings
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gui/SimpleListPanel.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/gui/SimpleListPanel.java | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleListPanel.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleListPanel.java new file mode 100644 index 0000000..b203a61 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleListPanel.java @@ -0,0 +1,65 @@ +package bjc.utils.gui; + +import java.awt.BorderLayout; +import java.util.function.Consumer; +import java.util.function.Predicate; + +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.ListSelectionModel; + +import bjc.utils.gui.layout.AutosizeLayout; +import bjc.utils.gui.layout.HLayout; + +public class SimpleListPanel extends JPanel { + private static final long serialVersionUID = 2719963952350133541L; + + public SimpleListPanel(String itemType, + DefaultListModel<String> listModel, + Predicate<String> itemVerifier, + Consumer<String> onVerificationFailure) { + setLayout(new AutosizeLayout()); + + JPanel itemInputPanel = new JPanel(); + itemInputPanel.setLayout(new BorderLayout()); + + JPanel addItemPanel = new JPanel(); + addItemPanel.setLayout(new HLayout(2)); + + JTextField addItemField = new JTextField(255); + JButton addItemButton = new JButton("Add " + itemType); + + addItemPanel.add(addItemField); + addItemPanel.add(addItemButton); + + JList<String> itemList = new JList<>(listModel); + itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + + JButton removeItemButton = new JButton("Remove " + itemType); + + addItemButton.addActionListener((ev) -> { + String potentialItem = addItemField.getText(); + + if (itemVerifier == null || itemVerifier.test(potentialItem)) { + listModel.addElement(potentialItem); + } else { + onVerificationFailure.accept(potentialItem); + } + + addItemField.setText(""); + }); + + 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); + } +} |
