Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 25th, 2003, 06:44 AM
Registered User
Join Date: Jul 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ransomhmc
Default about JPopupMenu

i wrote a JPanel capable of drawing lines on it,
now i want to pop up a JPopupMenu when clicking MouseButton3,

here is my code:

import javax.swing.*;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.*;

public class ScribblePane1 extends JPanel
        implements MouseListener, MouseMotionListener {

    private int last_x,last_y;
    private JPopupMenu pop = new JPopupMenu();

    public ScribblePane1() {

        setPreferredSize(new Dimension(300, 500));
        pop.add(new JMenuItem("test"));

    //mouse events
    public void mouseClicked(MouseEvent e) {

    public void mouseEntered(MouseEvent e) {

    public void mouseExited(MouseEvent e) {

    public void mousePressed(MouseEvent e) {
            last_x = e.getX();
            last_y = e.getY();

    public void mouseReleased(MouseEvent e) {

    public void maybePopup(MouseEvent e) {
       if (e.isPopupTrigger()) {
            pop.show((Component)e.getSource(), e.getX(),e.getY());
    //mouse motion events
    public void mouseDragged(MouseEvent e) {
            this.getGraphics().drawLine(last_x, last_y, e.getX(),e.getY());
            last_x = e.getX();
            last_y = e.getY();

    public void mouseMoved(MouseEvent e) {

but when pop.show() is invoked,
the lined drew already will be covered and disappear,
what's the problem? and what should i do to fix it?

sorry my english is not good,
hope you can understand what i want to say,
thanks all :)

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.