Skip to content

Documentation is wrong about integrating the viewer into a JPanel + proposed Fix #377

@simdeistud

Description

@simdeistud

In the "Graph Visualization" tutorial of the documentation it is said that in order to include the viewer into a user-defined JPanel you have to use more or less the following code:

import org.graphstream.ui.swingViewer.View;
import org.graphstream.ui.swingViewer.Viewer;
// ....
Graph graph = new MultiGraph("embedded");
Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
// ...
View view = viewer.addDefaultView(false);   // false indicates "no JFrame".
// ...
myJFrame.add(view);

This unfortunately doesn't work since the View class does not extend JPanel.
The solution I found is instead to use the SwingViewer and the ViewPanel classes, the latter which in fact extends JPanel.

import org.graphstream.ui.swing_viewer.SwingViewer;
import org.graphstream.ui.swing_viewer.ViewPanel;
// ....
Graph graph = new MultiGraph("embedded");
SwingViewer viewer = new SwingViewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
// ...
ViewPanel view = (ViewPanel) viewer.addDefaultView(false);   // false indicates "no JFrame".
// ...
myJFrame.add(view);

This code actually works and correctly embeds a viewer into an external JPanel.

I don't know why the tutorial is wrong since it's supposed to be updated to the 2.0 version of this library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions