PlayMa256
PlayMa256

Reputation: 6841

Graphstream small inside jframe

i'm trying to change the size of a graph that is inside a graphstream but its failing and getting even small.

This is my code. At first, i couldnt set a window title so my only option was to create a JFRAMe and embedded the view inside of it. So, if there is an option to set the title of the graph, i would appreciate it.

Here's my code.

public class Clicks implements ViewerListener {
    protected boolean loop = true;

    public static void main(String args[]) throws InterruptedException {
        new Clicks();
    }
    public Clicks() throws InterruptedException {
             System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
        // We do as usual to display a graph. This
        // connect the graph outputs to the viewer.
        // The viewer is a sink of the graph.
        Graph graph = new MultiGraph("Clicks");
                graph.addAttribute("ui.stylesheet", "node#D{ fill-color:red; }node{ stroke-mode: plain;size:20;text-size:30;stroke-color:#333333;text-background-color:#FFF;text-background-mode:plain;text-alignment:above;}");




                Node D = graph.addNode("D");
                Node J = graph.addNode("J");
        Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
                View view = viewer.addDefaultView(false);


                JFrame frame = new JFrame("title");
                frame.setSize(1000, 1000);
                frame.add((Component) view);
                frame.pack();
                frame.setVisible(true);

                ViewerPipe fromViewer = viewer.newViewerPipe();
        fromViewer.addViewerListener(this);
        fromViewer.addSink(graph);



        while(loop) {
            fromViewer.blockingPump();// or fromViewer.blockingPump(); in the nightly builds
        }
    }

    public void viewClosed(String id) {
        loop = false;
    }

    public void buttonPushed(String id) {

    }

    public void buttonReleased(String id) {
        System.out.println("Button released on node "+id);
    }
}

@edit

Even applying the changes that were proposed, it didnt work.

public Clicks() throws InterruptedException { System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer"); // We do as usual to display a graph. This // connect the graph outputs to the viewer. // The viewer is a sink of the graph. Graph graph = new MultiGraph("Clicks"); graph.addAttribute("ui.stylesheet", "node#D{ fill-color:red; }node{ stroke-mode: plain;size:20;text-size:30;stroke-color:#333333;text-background-color:#FFF;text-background-mode:plain;text-alignment:above;}");

        Node D = graph.addNode("D");
        Node J = graph.addNode("J");
Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
        View view = viewer.addDefaultView(false);


        JFrame frame = new JFrame("title");
        frame.add((Component) view);


        ViewerPipe fromViewer = viewer.newViewerPipe();
fromViewer.addViewerListener(this);
fromViewer.addSink(graph);

        frame.pack();
        frame.setVisible(true);


while(loop) {
    fromViewer.blockingPump();// or fromViewer.blockingPump(); in the nightly builds
}

}

When i run it, the window size is small, i have to manually resize it.

Whats the problem?

Upvotes: 0

Views: 1205

Answers (1)

camickr
camickr

Reputation: 324147

When i run it, the window size is small, i have to manually resize it.

frame.pack();
frame.setVisible(true);

ViewerPipe fromViewer = viewer.newViewerPipe();
fromViewer.addViewerListener(this);
fromViewer.addSink(graph);

You need to add all the components to the frame BEFORE making the frame visible.

So the code should be:

ViewerPipe fromViewer = viewer.newViewerPipe();
fromViewer.addViewerListener(this);
fromViewer.addSink(graph);

frame.pack();
frame.setVisible(true);

Also there is no need for the following line:

//frame.setSize(1000, 1000);

the pack() method will reset the size based on the preferred size of all the components added.

Upvotes: 1

Related Questions