List of usage examples for javax.swing.event PopupMenuEvent getSource
public Object getSource()
From source file:Main.java
public static void main(String[] args) { JComboBox c = new JComboBox(); c.addPopupMenuListener(new PopupMenuListener() { @Override/*from ww w .ja v a 2 s . c o m*/ public void popupMenuCanceled(PopupMenuEvent e) { System.out.println(e.getSource()); } @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { System.out.println(e.getSource()); } @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) { System.out.println(e.getSource()); } }); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().setLayout(new FlowLayout()); f.getContentPane().add(c); f.pack(); f.setVisible(true); }
From source file:PopupTest.java
public static void main(String args[]) { JFrame frame = new JFrame("Popup Menu Listener"); Container contentPane = frame.getContentPane(); final String flavors[] = { "Item 1", "Item 2", "Item 3" }; PopupMenuListener listener = new PopupMenuListener() { boolean initialized = false; public void popupMenuCanceled(PopupMenuEvent e) { }//from ww w. j av a 2 s . c om public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { } public void popupMenuWillBecomeVisible(PopupMenuEvent e) { if (!initialized) { JComboBox combo = (JComboBox) e.getSource(); ComboBoxModel model = new DefaultComboBoxModel(flavors); combo.setModel(model); initialized = true; } } }; JComboBox jc = new JComboBox(); jc.addPopupMenuListener(listener); jc.setMaximumRowCount(4); jc.setEditable(true); contentPane.add(jc, BorderLayout.NORTH); frame.pack(); frame.show(); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame("Popup Menu Listener"); Container contentPane = frame.getContentPane(); final String flavors[] = { "Item 1", "Item 2", "Item 3" }; PopupMenuListener listener = new PopupMenuListener() { boolean initialized = false; public void popupMenuCanceled(PopupMenuEvent e) { }// w ww.ja va2 s. c o m public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { } public void popupMenuWillBecomeVisible(PopupMenuEvent e) { if (!initialized) { JComboBox combo = (JComboBox) e.getSource(); ComboBoxModel model = new DefaultComboBoxModel(flavors); combo.setModel(model); initialized = true; } } }; JComboBox jc = new JComboBox(); jc.addPopupMenuListener(listener); jc.setMaximumRowCount(4); jc.setEditable(true); contentPane.add(jc, BorderLayout.NORTH); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public void popupMenuWillBecomeVisible(PopupMenuEvent evt) { JComboBox cb = (JComboBox) evt.getSource(); }
From source file:Main.java
public void popupMenuWillBecomeInvisible(PopupMenuEvent evt) { JComboBox cb = (JComboBox) evt.getSource(); }
From source file:Main.java
public void popupMenuCanceled(PopupMenuEvent evt) { JComboBox cb = (JComboBox) evt.getSource(); }
From source file:Main.java
public Main() { String[] items = { "Item1", "Item2", "Item3", "Item4", "Item5" }; JComboBox<String> comboBox = new JComboBox<>(items); add(comboBox);//from w w w.j a va 2 s .c om comboBox.addPopupMenuListener(new PopupMenuListener() { public void popupMenuWillBecomeVisible(PopupMenuEvent e) { JComboBox<String> comboBox = (JComboBox<String>) e.getSource(); BasicComboPopup popup = (BasicComboPopup) comboBox.getAccessibleContext().getAccessibleChild(0); JList list = popup.getList(); list.setSelectedIndex(2); } public void popupMenuCanceled(PopupMenuEvent e) { } public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { } }); }
From source file:Main.java
public Main() { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(400, 300);/* ww w . ja v a 2 s.c o m*/ jPopupMenu1.add(jMenuItem1); jTabbedPane1.addTab(null, jLabel1); jTabbedPane1.addTab(null, jLabel2); getContentPane().add(jTabbedPane1, BorderLayout.CENTER); int tabCount = jTabbedPane1.getTabCount(); for (int i = 0; i < tabCount; i++) { JLabel jLabel = new JLabel("Testing the tab" + (i + 1)); jTabbedPane1.setTabComponentAt(i, jLabel); jLabel.setName(String.valueOf(i)); jLabel.setComponentPopupMenu(jPopupMenu1); } jPopupMenu1.addPopupMenuListener(new PopupMenuListener() { @Override public void popupMenuCanceled(final PopupMenuEvent evt) { } @Override public void popupMenuWillBecomeInvisible(final PopupMenuEvent evt) { } @Override public void popupMenuWillBecomeVisible(final PopupMenuEvent evt) { JPopupMenu source = (JPopupMenu) evt.getSource(); JLabel invoker = (JLabel) source.getInvoker(); JLabel component = (JLabel) jTabbedPane1.getComponentAt(Integer.parseInt(invoker.getName())); jMenuItem1.setText(invoker.getText() + ": " + component.getText()); } }); }