$darkmode
DENOPTIM
ScrollableJPupupMenu.java
Go to the documentation of this file.
1package denoptim.gui;
2
3import java.awt.BorderLayout;
4import java.awt.Component;
5import java.awt.Dimension;
6import java.awt.Point;
7import java.awt.event.ActionEvent;
8import java.awt.event.ActionListener;
9import java.util.ArrayList;
10import java.util.List;
11
12import javax.swing.BoxLayout;
13import javax.swing.JButton;
14import javax.swing.JCheckBox;
15import javax.swing.JPanel;
16import javax.swing.JPopupMenu;
17import javax.swing.JScrollPane;
18import javax.swing.SwingConstants;
19
25{
26 private JPopupMenu menu;
27 private JPanel menuPanel;
28 private List<JCheckBox> allBChekBoxes = new ArrayList<JCheckBox>();
29
30//------------------------------------------------------------------------------
31
33 {}
34
35//------------------------------------------------------------------------------
36
43 public void showMenu(Component invoker, int x, int y)
44 {
45 menu = new JPopupMenu();
46 menu.setLayout(new BorderLayout());
47 menuPanel = new JPanel();
48 menuPanel.setLayout(new BoxLayout(menuPanel, SwingConstants.VERTICAL));
49 for (JCheckBox cb : allBChekBoxes)
50 {
51 menuPanel.add(cb);
52 }
53
54 JScrollPane scrollablePane = new JScrollPane(menuPanel);
55 scrollablePane.setPreferredSize(new Dimension(450,400));
56 menu.add(scrollablePane, BorderLayout.CENTER);
57
58 JButton doneBtn = new JButton("Done");
59 doneBtn.addActionListener(new ActionListener(){
60 public void actionPerformed(ActionEvent e){
61 menu.setVisible(false);
62 }
63 });
64 menu.add(doneBtn, BorderLayout.SOUTH);
65
66 menu.pack();
67 menu.setInvoker(invoker);
68 Point invokerOrigin = invoker.getLocationOnScreen();
69 menu.setLocation((int) invokerOrigin.getX() + x,
70 (int) invokerOrigin.getY() + y);
71 menu.setVisible(true);
72 }
73
74//------------------------------------------------------------------------------
75
80 public void addCheckBox(JCheckBox item)
81 {
82 allBChekBoxes.add(item);
83 }
84
85//------------------------------------------------------------------------------
86
91 protected List<JCheckBox> getAllBChekBoxes()
92 {
93 return allBChekBoxes;
94 }
95
96//------------------------------------------------------------------------------
97
98}
A popup menu' that has a fixed size and can be scrolled to see menu items that do not fit into the fi...
void showMenu(Component invoker, int x, int y)
Displays the menu with the components that have been added so far.
void addCheckBox(JCheckBox item)
Add a check box to the list of check boxes to display in the menu.
List< JCheckBox > getAllBChekBoxes()
Returns the list of check boxes that is displayed in the menu.