Results 1 to 1 of 1
Thread: Creating popup menus with Swing
-
Creating popup menus with Swing
Java Code:import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import javax.swing.JTextField; public class Popup extends JApplet { private JPopupMenu popup = new JPopupMenu(); private JTextField t = new JTextField(10); public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(t); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { t.setText(((JMenuItem) e.getSource()).getText()); } }; JMenuItem m = new JMenuItem("Hither"); m.addActionListener(al); popup.add(m); m = new JMenuItem("Yon"); m.addActionListener(al); popup.add(m); m = new JMenuItem("Afar"); m.addActionListener(al); popup.add(m); popup.addSeparator(); m = new JMenuItem("Stay Here"); m.addActionListener(al); popup.add(m); PopupListener pl = new PopupListener(); addMouseListener(pl); t.addMouseListener(pl); } class PopupListener extends MouseAdapter { public void mousePressed(MouseEvent e) { maybeShowPopup(e); } public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) popup.show(((JApplet) e.getComponent()).getContentPane(), e .getX(), e.getY()); } } public static void main(String[] args) { run(new Popup(), 300, 200); } public static void run(JApplet applet, int width, int height) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(applet); frame.setSize(width, height); applet.init(); applet.start(); frame.setVisible(true); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
nebie popup script, need to add code.
By lilith2014 in forum New To JavaReplies: 2Last Post: 12-30-2010, 11:09 AM -
Is there popup search dialog for this?
By lmsook10 in forum AWT / SwingReplies: 5Last Post: 06-27-2008, 05:08 PM -
how we get popup window in java
By baserohit in forum Advanced JavaReplies: 1Last Post: 03-22-2008, 04:39 AM -
Communicating with JSP and popup
By nilz in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-20-2007, 04:29 PM -
Popup in Java
By fernando in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks