Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6153567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:06:56+00:00 2026-05-23T20:06:56+00:00

I want to draw some graphs including vertices and edges in my application. I

  • 0

I want to draw some graphs including vertices and edges in my application. I found that JGraph is a good library for plotting graphs. I went through some online sources about it but couldn’t find any relevant articles about how to embed a JGraph in a Swing application. (Showing a JGraph in a JFrame etc). Can Any one help me with that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:06:57+00:00Added an answer on May 23, 2026 at 8:06 pm

    This code worked for me:

    // Insert the cells via the cache, so they get selected
    graph.getGraphLayoutCache().insert(cells);
    
    // Show in Frame
    JFrame frame = new JFrame();
    frame.getContentPane().add(new JScrollPane(graph));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    

    If you want the whole sample i can upload it too.

    I think the code must be a modified sample from the site. Here you go:

    import java.awt.Color;
    import java.awt.geom.Rectangle2D;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import org.jgraph.JGraph;
    import org.jgraph.graph.DefaultEdge;
    import org.jgraph.graph.DefaultGraphCell;
    import org.jgraph.graph.DefaultGraphModel;
    import org.jgraph.graph.GraphConstants;
    import org.jgraph.graph.GraphModel;
    
    public class Foo {
    
        public static void main(String[] args) {
    
            // Construct Model and Graph
            GraphModel model = new DefaultGraphModel();
            JGraph graph = new JGraph(model);
            // Control-drag should clone selection
            graph.setCloneable(true);
    
            // Enable edit without final RETURN keystroke
            graph.setInvokesStopCellEditing(true);
    
            // When over a cell, jump to its default port (we only have one, anyway)
            graph.setJumpToDefaultPort(true);
    
            // Insert all three cells in one call, so we need an array to store them
            DefaultGraphCell[] cells = new DefaultGraphCell[3];
    
            // Create Hello Vertex
            cells[0] = createVertex("Hello", 20, 20, 40, 20, null, false );
    
            // Create World Vertex
            cells[1] = createVertex("World", 140, 140, 40, 20,
                    Color.ORANGE, true);
    
            // Create Edge
            DefaultEdge edge = new DefaultEdge("foo");
            // Fetch the ports from the new vertices, and connect them with the edge
            edge.setSource(cells[0].getChildAt(0));
            edge.setTarget(cells[0].getChildAt(0));
            cells[2] = edge;
    
            // Create Edge
            DefaultEdge edge1 = new DefaultEdge();
            // Fetch the ports from the new vertices, and connect them with the edge
    //        cells[0].addPort();
    //        cells[1].addPort();
    //        edge1.setSource(cells[1]);
    //        edge1.setTarget(cells[0]);
    //        cells[3] = edge1;
    
            // Set Arrow Style for edge
            int arrow = GraphConstants.ARROW_CLASSIC;
            GraphConstants.setLineEnd(edge.getAttributes(), arrow);
            GraphConstants.setEndFill(edge.getAttributes(), true);
    
            // Insert the cells via the cache, so they get selected
            graph.getGraphLayoutCache().insert(cells);
    
            // Show in Frame
            JFrame frame = new JFrame();
            frame.getContentPane().add(new JScrollPane(graph));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static DefaultGraphCell createVertex(String name, double x,
                double y, double w, double h, Color bg, boolean raised) {
    
            // Create vertex with the given name
            DefaultGraphCell cell = new DefaultGraphCell(name);
    
            // Set bounds
            GraphConstants.setBounds(cell.getAttributes(),
                    new Rectangle2D.Double(x, y, w, h));
    
            // Set fill color
            if (bg != null) {
                GraphConstants.setGradientColor(cell.getAttributes(), bg);
                GraphConstants.setOpaque(cell.getAttributes(), true);
            }
    
            // Set raised border
            if (raised) {
                GraphConstants.setBorder(cell.getAttributes(),
                        BorderFactory.createRaisedBevelBorder());
            } else // Set black border
            {
                GraphConstants.setBorderColor(cell.getAttributes(),
                        Color.black);
            }
            // Add a Floating Port
            cell.addPort();
    
            return cell;
        }
    }
    

    This is a very simple example. I have done a few complicated graphs of database data, but ended up working with Jung2 which fits my needs better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ImageView and draw some things on that view. Now I want
I want to draw graphs (nodes and edges) in Java. However, since I don't
I want to draw some Rectangle over a single Image . For example I
I want to draw some lines and rectangles on a panel. Sometimes it does
I want to draw some listview items disabled and would like to mimic the
I've got problem with DirectX in C#. I want to draw some lines. Firstly
In my program, I draw some quads. I want to add the functionality for
i want to draw line, i have two textfiled in which i enter some
I have a JPanel and I want do draw to this JPanel some very
I want to draw rectangle that is only specified percent high of panel im

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.