|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.*; |
| 3 | +import javax.swing.*; |
| 4 | +import javax.swing.border.*; |
| 5 | +import javax.swing.event.*; |
| 6 | +import javax.swing.UIManager.*; |
| 7 | + |
| 8 | +public class BoundsPopupMenuListenerDemo extends JPanel |
| 9 | + implements ChangeListener, ItemListener |
| 10 | +{ |
| 11 | + private BoundsPopupMenuListener listener; |
| 12 | + private JCheckBox scrollBarRequired; |
| 13 | + private JCheckBox popupWider; |
| 14 | + private JSpinner maximumWidth; |
| 15 | + private JCheckBox popupAbove; |
| 16 | + private JComboBox<String> comboBox; |
| 17 | + |
| 18 | + BoundsPopupMenuListenerDemo() |
| 19 | + { |
| 20 | + setLayout( new BorderLayout(10, 10) ); |
| 21 | + setBorder( new EmptyBorder(20, 20, 20, 20) ); |
| 22 | + |
| 23 | + JComponent left = createLeftPanel(); |
| 24 | + JComponent right = createRightPanel(); |
| 25 | + |
| 26 | + add(left, BorderLayout.CENTER); |
| 27 | + add(right, BorderLayout.EAST); |
| 28 | + } |
| 29 | + |
| 30 | + private JComponent createLeftPanel() |
| 31 | + { |
| 32 | + listener = new BoundsPopupMenuListener(false, false, -1, false); |
| 33 | + |
| 34 | + String[] items = |
| 35 | + { |
| 36 | + "Item1", |
| 37 | + "Item2", |
| 38 | + "Item3", |
| 39 | + "Item4 contains longer text to display", |
| 40 | + "Item5", |
| 41 | + "Item6", |
| 42 | + "Item7", |
| 43 | + "Item8" |
| 44 | + }; |
| 45 | + |
| 46 | + comboBox = new JComboBox<>( items ); |
| 47 | + comboBox.setPrototypeDisplayValue("ItemWWW"); |
| 48 | + comboBox.setMaximumRowCount(5); |
| 49 | + comboBox.addPopupMenuListener( listener ); |
| 50 | + Dimension d = comboBox.getPreferredSize(); |
| 51 | + d.width = 1024; |
| 52 | + comboBox.setMaximumSize( d ); |
| 53 | + |
| 54 | + Box content = new Box( BoxLayout.Y_AXIS ); |
| 55 | + content.add( comboBox ); |
| 56 | + content.add( Box.createVerticalGlue() ); |
| 57 | + |
| 58 | + return content; |
| 59 | + } |
| 60 | + |
| 61 | + public JPanel createRightPanel() |
| 62 | + { |
| 63 | + RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS); |
| 64 | + rl.setAlignment(RelativeLayout.LEADING); |
| 65 | + rl.setFill( true ); |
| 66 | + JPanel main = new JPanel( rl ); |
| 67 | + main.setBorder( new TitledBorder("Change Properties:") ); |
| 68 | + |
| 69 | + scrollBarRequired = new JCheckBox("ScrollBar Required"); |
| 70 | + scrollBarRequired.addItemListener( this ); |
| 71 | + main.add( scrollBarRequired ); |
| 72 | + |
| 73 | + popupWider = new JCheckBox("Popup Wider"); |
| 74 | + popupWider.addItemListener( this ); |
| 75 | + main.add( popupWider ); |
| 76 | + |
| 77 | + JPanel width = new JPanel(); |
| 78 | + maximumWidth = new JSpinner(new SpinnerNumberModel(-1, -1, 599 ,50)); |
| 79 | + maximumWidth.addChangeListener( this ); |
| 80 | + width.add( maximumWidth ); |
| 81 | + width.add( new JLabel("Maximum Width") ); |
| 82 | + main.add( width ); |
| 83 | + |
| 84 | + popupAbove = new JCheckBox("Popup Above"); |
| 85 | + popupAbove.addItemListener( this ); |
| 86 | + main.add( popupAbove ); |
| 87 | + |
| 88 | + return main; |
| 89 | + } |
| 90 | + |
| 91 | + public void itemStateChanged(ItemEvent e) |
| 92 | + { |
| 93 | + JCheckBox checkBox = (JCheckBox)e.getSource(); |
| 94 | + |
| 95 | + if (checkBox == scrollBarRequired) |
| 96 | + listener.setScrollBarRequired( checkBox.isSelected() ); |
| 97 | + else if (checkBox == popupWider) |
| 98 | + listener.setPopupWider( checkBox.isSelected() ); |
| 99 | + else if (checkBox == popupAbove) |
| 100 | + listener.setPopupAbove( checkBox.isSelected() ); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + public void stateChanged(ChangeEvent e) |
| 105 | + { |
| 106 | + listener.setMaximumWidth( (Integer)maximumWidth.getValue() ); |
| 107 | + } |
| 108 | + |
| 109 | + public static void main(String[] args) |
| 110 | + { |
| 111 | + SwingUtilities.invokeLater(new Runnable() { |
| 112 | + public void run() { |
| 113 | + createAndShowGUI(); |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + public static void createAndShowGUI() |
| 119 | + { |
| 120 | + JFrame frame = new JFrame("Bounds Popup Menu Listener"); |
| 121 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 122 | + frame.add( new BoundsPopupMenuListenerDemo() ); |
| 123 | + frame.pack(); |
| 124 | + frame.setLocationRelativeTo(null); |
| 125 | + frame.setVisible(true); |
| 126 | + |
| 127 | + } |
| 128 | +} |
0 commit comments