List of usage examples for javax.swing JMenuItem addItemListener
public void addItemListener(ItemListener l)
ItemListener
to the checkbox
. From source file:net.sourceforge.pmd.cpd.GUI.java
public GUI() { frame = new JFrame("PMD Duplicate Code Detector (v " + PMDVersion.VERSION + ')'); timeField.setEditable(false);/*w w w. j a v a 2 s . co m*/ JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic('f'); addSaveOptionsTo(fileMenu); JMenuItem exitItem = new JMenuItem("Exit"); exitItem.setMnemonic('x'); exitItem.addActionListener(new CancelListener()); fileMenu.add(exitItem); JMenu viewMenu = new JMenu("View"); fileMenu.setMnemonic('v'); JMenuItem trimItem = new JCheckBoxMenuItem("Trim leading whitespace"); trimItem.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { AbstractButton button = (AbstractButton) e.getItem(); GUI.this.trimLeadingWhitespace = button.isSelected(); } }); viewMenu.add(trimItem); JMenuBar menuBar = new JMenuBar(); menuBar.add(fileMenu); menuBar.add(viewMenu); frame.setJMenuBar(menuBar); // first make all the buttons JButton browseButton = new JButton("Browse"); browseButton.setMnemonic('b'); browseButton.addActionListener(new BrowseListener()); goButton = new JButton("Go"); goButton.setMnemonic('g'); goButton.addActionListener(new GoListener()); cancelButton = new JButton("Cancel"); cancelButton.addActionListener(new CancelListener()); JPanel settingsPanel = makeSettingsPanel(browseButton, goButton, cancelButton); progressPanel = makeProgressPanel(); JPanel resultsPanel = makeResultsPanel(); adjustLanguageControlsFor((LanguageConfig) LANGUAGE_SETS[0][1]); frame.getContentPane().setLayout(new BorderLayout()); JPanel topPanel = new JPanel(); topPanel.setLayout(new BorderLayout()); topPanel.add(settingsPanel, BorderLayout.NORTH); topPanel.add(progressPanel, BorderLayout.CENTER); setProgressControls(false); // not running now frame.getContentPane().add(topPanel, BorderLayout.NORTH); frame.getContentPane().add(resultsPanel, BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); }
From source file:org.nuclos.client.ui.collect.component.AbstractCollectableComponent.java
/** * @return/*from ww w. j a v a 2s . c om*/ * @precondition hasComparisonOperator() */ public JPopupMenu newComparisonOperatorPopupMenu() { if (!hasComparisonOperator()) { throw new IllegalStateException(); } final JPopupMenu result = new JPopupMenu( getSpringLocaleDelegate().getMessage("AbstractCollectableComponent.16", "Vergleichsoperator")); // 1. comparison operators: final ButtonGroup btngrpComparisonOperators = new ButtonGroup(); final ItemListener itemlistener = new ItemListener() { @Override public void itemStateChanged(ItemEvent ev) { if (ev.getStateChange() == ItemEvent.SELECTED) { final String sOperatorName = ((AbstractButton) ev.getItem()).getActionCommand(); setComparisonOperator(ComparisonOperator.getInstance(sOperatorName)); runLocked(new Runnable() { @Override public void run() { updateSearchConditionInModel(); } }); } } }; ComparisonOperator[] supportedComparisonOperators = getSupportedComparisonOperators(); if (supportedComparisonOperators == null) return null; for (ComparisonOperator compop : supportedComparisonOperators) { JMenuItem mi = new JRadioButtonMenuItem( getSpringLocaleDelegate().getMessage(compop.getResourceIdForLabel(), null)); mi.setActionCommand(compop.name()); mi.setToolTipText(getSpringLocaleDelegate().getMessage(compop.getResourceIdForDescription(), null)); result.add(mi); btngrpComparisonOperators.add(mi); mi.addItemListener(itemlistener); } result.addPopupMenuListener(new PopupMenuListener() { @Override public void popupMenuWillBecomeVisible(PopupMenuEvent ev) { for (int i = 0, n = result.getComponentCount(); i < n; i++) { Component c = result.getComponent(i); if (c instanceof JMenuItem && StringUtils.emptyIfNull(((JMenuItem) c).getActionCommand()) .equals(getComparisonOperator().name())) ((JMenuItem) c).setSelected(true); } } @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent ev) { } @Override public void popupMenuCanceled(PopupMenuEvent ev) { } }); // 2. right operand (value or other field): if (canDisplayComparisonWithOtherField()) { addRightOperandToPopupMenu(result, this); } return result; }