File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Adding graphics to JPanel using PopupMenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding graphics to JPanel using PopupMenu" Watch "Adding graphics to JPanel using PopupMenu" New topic
Author

Adding graphics to JPanel using PopupMenu

Deirdre Hackett
Greenhorn

Joined: Feb 13, 2008
Posts: 5
Adding graphics to JPanel using PopupMenu

Right. I really need help at this stage so I'll try and be as clear as I can.

I essentially want to be able to create a flowchart dynamically by adding different statements. Now it doesn't look like I have much done but this is probably my 4th attempt.
I basically want to draw the "Assignment", "While", "IF", blocks onto a JPanel by right clicking on nodes where I would like to insert these blocks.

One of my problems is: Can I add a MouseListener to my node in order to generate a popupmenu. At the moment I have a mouse listener for the whole JPanel and the popupmenu is generated where ever you right-click the mouse. I would like this to happen just over nodes. I know that mouselisteners can only be applied to JComponents so I extended the FCNode class with JComponent. I tried to add the mouse listener at this stage but it didn't work but I thought that maybe that was because of where I did it. Is what I want possible? If so how and where should I do it.

The code below isn't what it should be yet but I am just testing some of my theories. Initially I did have alot of the nested inner classes in seperate files but then put them all in one class based on something I read on another thread. Is this the right thing to do?

In the code below I simply created the default window that I want to see when the application is started. So basically the next thing I want to be able to do is right click on the node present, see a popup window appear, click "Assignment" and then see the "Assignment" graphic appear between the "start" and end "graphic".

Is this possible and if so how?

Sorry for rambling and thanks in advance for the help.

Regards,

Deirdre

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
One of my problems is: Can I add a MouseListener to my node in order to generate a popupmenu. At the moment I have a mouse listener for the whole JPanel and the popupmenu is generated where ever you right-click the mouse. I would like this to happen just over nodes. I know that mouselisteners can only be applied to JComponents so I extended the FCNode class with JComponent. I tried to add the mouse listener at this stage but it didn't work but I thought that maybe that was because of where I did it. Is what I want possible?
Yes.

If so how and where should I do it.
You would have to add the JComponent/Nodes to the FlowPanel where they can generate MouseEvents in response to user actions. And each of these added JComponents would have to have a Mouselistener added to it so the component could listen for mouse events.
Right now you do not add the components but use the classes to hold information and to draw graphics with the graphics context, g, passed in from the FlowPanel paintComkponent method. So, as is, the JComponent extension is not needed or not being used, but okay.

Initially I did have alot of the nested inner classes in seperate files but then put them all in one class based on something I read on another thread. Is this the right thing to do?
It's up to you. In the beginning many put everything in one class. Later it seems more elegant to break things up into smaller classes according to function. The challenge with this is how to get the classes to communicate with one another. Everyone seems to have to find their own way in this.

...the next thing I want to be able to do is right click on the node present, see a popup window appear, click "Assignment" and then see the "Assignment" graphic appear between the "start" and end "graphic".

Is this possible and if so how?

I think so.
You are testing some ... theories which I would say include:
1 — Using a component approach such as adding JComponents to the FlowPanel and arranging for each component to know/learn when the user wants to select it with the mouse. You could try adding a MouseListener to each of these added components. Another option is to use a non-opaque overlay panel with a MouseListener (nodes don't have their own listeners with this) and search for/identify target components below to find/determine node selections. You can also arrange to select and/or drag these components with the mouse if you like. This lets the JComponent class do some of the work.
2 — A graphic approach in which you draw everything. Here you use a single MouseListener added to FlowPanel (the way you have it now) and search all of the Nodes (in you Collections) to see if any contain the mouse point on the Flowpanel. You can also select and drag these nodes with the mouse.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Adding graphics to JPanel using PopupMenu