1

I am developing an application that transforms an XML file to a dynamic graph so I made a class that extended JPanel to arrange the node (circularnode.java) and I created a class that extended JPanel to add arrows (arrow.java) but when displaying the arrows, they always come behind nodes. The background of circularnode.java hides them.

How can I display a JPanel over another, or control the order of appearance?

naugler
  • 1,060
  • 10
  • 31
Rad1
  • 185
  • 1
  • 4
  • 17
  • 2
    Also consider an existing [library](http://stackoverflow.com/q/51574/230513), such as `JFreeChart`, `JGraph` or `JUNG`. – trashgod Aug 30 '12 at 16:07

1 Answers1

4

Consider extracting the code for directly drawing of the arrows and nodes out of JPanel. Perhaps you can create an Arrow and a Node class that know how to draw themselves, for instance that has a public void paint(Graphics g) method.

Then you could create an ArrayList<Arrow> and ArrayList<Node> fields for your JPanel class, and draw both in the JPanel's paintComponent(...) method by iterating through the Lists and calling each item's paint(...) method passing in the Graphics context obtained from the JVM.

Otherwise, if you absolutely need to draw these in separate JPanels, one on top of the other, be sure that the top JPanel is not opaque via setOpaque(false), so that its background won't be painted, and you can see through it.

For more specific help, consider telling us more about your problem, your current code, and even posting some of your code, preferably in the form of an SSCCE (please see the link that describes this very useful construct).

Welcome to stackoverflow.com by the way!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • thank you it has been very kind of you ! i did what you recommended by rearrange it in list element `public ElementMenu(String texte,String Ident, Color couleurNormal, Color couleurActif, int taille) { this.couleur=couleurNormal; this.couleurNormal=couleurNormal; this.couleurActif=couleurActif; this.texte=texte; this.Ident=Ident ; largeur=taille; this.setSize(new Dimension(largeur, largeur )); this.setOpaque(false); this.setLayout(new BorderLayout()); ajouterListener(); } ` – Rad1 Aug 30 '12 at 16:53
  • @Rad1: you're welcome! You'll want to post any code as an edit to your question though since comments don't display code formatting correctly. You'll also want the posted code indented 4 spaces so that the site will display the code formatting. – Hovercraft Full Of Eels Aug 30 '12 at 16:54
  • ok i am new and iam really having a bad time withe this project ! i will try to more clarify my post – Rad1 Aug 30 '12 at 16:58
  • this my main class : `Fenetre rad1 = new Fenetre() ; List items = new ArrayList(); items.add(new ElementMenu(elementAction.getText(),"ACTION", new Color(19, 73, 140), new Color(162, 198, 232), 150)); items.add(new ElementMenu(elementGoal.getText(),"GOAL",new Color(19, 73, 140), new Color(162, 198, 232), 150)); rad1.add(new MenuCirculaire(Color.blue, 1000, 600, 200, 150, items), BorderLayout.CENTER); rad1.add(new ArrowPanel(363.5,335,460.5,168,540,171,635.5,335));` – Rad1 Aug 30 '12 at 17:01
  • my arrows take for settings the coordinates of the 4 point to draw 2 arrows ! well i have organized my node (element menu) by the class Menucerculaire which is a Jpannel so it contains all the elements and then i draw the arrows to much the nodes ! but it always come behind the background of the pannel Menucerculaire ! – Rad1 Aug 30 '12 at 17:07