JGraphX is ruining the bounds of Swing-components












0















I am trying to implement an interaction between some self-written GUI-Elements (like Java-swing Buttons) and JGraphX. In order to do that, I first just want to display a Button next to a JGraph-Element - and I'm stuck.



My code to display the Button itself works fine:



import javax.swing.*;    

public class HelloWorld extends JFrame {

public HelloWorld()
{
super("Everything works");
}

public static void main(String args)
{
hello_world frame = new hello_world();
frame.setTitle("bub");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);

button.setBounds(10, 10, 100, 50);

frame.setVisible(true);

frame.add(button);
}
}


This is the Button as it is supposed to be shown



But as soon as I am adding the JGraph-Component, the Button ist displayed fullscreen. I have no Idea why and how to prevent that. My Code:



import javax.swing.*;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class HelloWorld extends JFrame {

private mxGraph graph;
private Object window;

public HelloWorld()
{
super("Nothing works");

graph = new mxGraph();
window = graph.getDefaultParent();

graph.getModel().beginUpdate();

try
{
Object v2 = graph.insertVertex(window, null, "Hello World!", 200, 150, 80, 30);

}
finally
{
graph.getModel().endUpdate();
}

mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}

public static void main(String args)
{
hello_world frame = new hello_world();


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);

JButton button = new JButton("left");
button.setBounds(10, 10, 100, 50);

frame.setVisible(true);

frame.add(button);
}

}


The result looks like this (I manually resized the window to show you the button. Naturally, the button would just fill the space behind the JGraph-Component and therefor would be invisible):



This is the Button, filling up the whole screen.










share|improve this question

























  • Each work in a class name should start with an upper case character. "hello_world" should be HelloWorld. When in doubt follow the format of the class names found in the Java API.

    – camickr
    Nov 14 '18 at 16:24













  • You're right, but I'm pretty sure thats not the main problem there ;)

    – Jere
    Nov 14 '18 at 16:27













  • I'm pretty sure thats not the main problem there That's why I added it as a comment. The forum will highlight class names. It makes it easier for us (or anybody else) to read your code when you follow standards. There is no reason to not follow standards. Everybody benefits.

    – camickr
    Nov 14 '18 at 17:40


















0















I am trying to implement an interaction between some self-written GUI-Elements (like Java-swing Buttons) and JGraphX. In order to do that, I first just want to display a Button next to a JGraph-Element - and I'm stuck.



My code to display the Button itself works fine:



import javax.swing.*;    

public class HelloWorld extends JFrame {

public HelloWorld()
{
super("Everything works");
}

public static void main(String args)
{
hello_world frame = new hello_world();
frame.setTitle("bub");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);

button.setBounds(10, 10, 100, 50);

frame.setVisible(true);

frame.add(button);
}
}


This is the Button as it is supposed to be shown



But as soon as I am adding the JGraph-Component, the Button ist displayed fullscreen. I have no Idea why and how to prevent that. My Code:



import javax.swing.*;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class HelloWorld extends JFrame {

private mxGraph graph;
private Object window;

public HelloWorld()
{
super("Nothing works");

graph = new mxGraph();
window = graph.getDefaultParent();

graph.getModel().beginUpdate();

try
{
Object v2 = graph.insertVertex(window, null, "Hello World!", 200, 150, 80, 30);

}
finally
{
graph.getModel().endUpdate();
}

mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}

public static void main(String args)
{
hello_world frame = new hello_world();


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);

JButton button = new JButton("left");
button.setBounds(10, 10, 100, 50);

frame.setVisible(true);

frame.add(button);
}

}


The result looks like this (I manually resized the window to show you the button. Naturally, the button would just fill the space behind the JGraph-Component and therefor would be invisible):



This is the Button, filling up the whole screen.










share|improve this question

























  • Each work in a class name should start with an upper case character. "hello_world" should be HelloWorld. When in doubt follow the format of the class names found in the Java API.

    – camickr
    Nov 14 '18 at 16:24













  • You're right, but I'm pretty sure thats not the main problem there ;)

    – Jere
    Nov 14 '18 at 16:27













  • I'm pretty sure thats not the main problem there That's why I added it as a comment. The forum will highlight class names. It makes it easier for us (or anybody else) to read your code when you follow standards. There is no reason to not follow standards. Everybody benefits.

    – camickr
    Nov 14 '18 at 17:40
















0












0








0








I am trying to implement an interaction between some self-written GUI-Elements (like Java-swing Buttons) and JGraphX. In order to do that, I first just want to display a Button next to a JGraph-Element - and I'm stuck.



My code to display the Button itself works fine:



import javax.swing.*;    

public class HelloWorld extends JFrame {

public HelloWorld()
{
super("Everything works");
}

public static void main(String args)
{
hello_world frame = new hello_world();
frame.setTitle("bub");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);

button.setBounds(10, 10, 100, 50);

frame.setVisible(true);

frame.add(button);
}
}


This is the Button as it is supposed to be shown



But as soon as I am adding the JGraph-Component, the Button ist displayed fullscreen. I have no Idea why and how to prevent that. My Code:



import javax.swing.*;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class HelloWorld extends JFrame {

private mxGraph graph;
private Object window;

public HelloWorld()
{
super("Nothing works");

graph = new mxGraph();
window = graph.getDefaultParent();

graph.getModel().beginUpdate();

try
{
Object v2 = graph.insertVertex(window, null, "Hello World!", 200, 150, 80, 30);

}
finally
{
graph.getModel().endUpdate();
}

mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}

public static void main(String args)
{
hello_world frame = new hello_world();


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);

JButton button = new JButton("left");
button.setBounds(10, 10, 100, 50);

frame.setVisible(true);

frame.add(button);
}

}


The result looks like this (I manually resized the window to show you the button. Naturally, the button would just fill the space behind the JGraph-Component and therefor would be invisible):



This is the Button, filling up the whole screen.










share|improve this question
















I am trying to implement an interaction between some self-written GUI-Elements (like Java-swing Buttons) and JGraphX. In order to do that, I first just want to display a Button next to a JGraph-Element - and I'm stuck.



My code to display the Button itself works fine:



import javax.swing.*;    

public class HelloWorld extends JFrame {

public HelloWorld()
{
super("Everything works");
}

public static void main(String args)
{
hello_world frame = new hello_world();
frame.setTitle("bub");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);

button.setBounds(10, 10, 100, 50);

frame.setVisible(true);

frame.add(button);
}
}


This is the Button as it is supposed to be shown



But as soon as I am adding the JGraph-Component, the Button ist displayed fullscreen. I have no Idea why and how to prevent that. My Code:



import javax.swing.*;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class HelloWorld extends JFrame {

private mxGraph graph;
private Object window;

public HelloWorld()
{
super("Nothing works");

graph = new mxGraph();
window = graph.getDefaultParent();

graph.getModel().beginUpdate();

try
{
Object v2 = graph.insertVertex(window, null, "Hello World!", 200, 150, 80, 30);

}
finally
{
graph.getModel().endUpdate();
}

mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}

public static void main(String args)
{
hello_world frame = new hello_world();


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);

JButton button = new JButton("left");
button.setBounds(10, 10, 100, 50);

frame.setVisible(true);

frame.add(button);
}

}


The result looks like this (I manually resized the window to show you the button. Naturally, the button would just fill the space behind the JGraph-Component and therefor would be invisible):



This is the Button, filling up the whole screen.







java swing jbutton jgraphx jgraph






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 12:32







Jere

















asked Nov 14 '18 at 16:09









JereJere

521219




521219













  • Each work in a class name should start with an upper case character. "hello_world" should be HelloWorld. When in doubt follow the format of the class names found in the Java API.

    – camickr
    Nov 14 '18 at 16:24













  • You're right, but I'm pretty sure thats not the main problem there ;)

    – Jere
    Nov 14 '18 at 16:27













  • I'm pretty sure thats not the main problem there That's why I added it as a comment. The forum will highlight class names. It makes it easier for us (or anybody else) to read your code when you follow standards. There is no reason to not follow standards. Everybody benefits.

    – camickr
    Nov 14 '18 at 17:40





















  • Each work in a class name should start with an upper case character. "hello_world" should be HelloWorld. When in doubt follow the format of the class names found in the Java API.

    – camickr
    Nov 14 '18 at 16:24













  • You're right, but I'm pretty sure thats not the main problem there ;)

    – Jere
    Nov 14 '18 at 16:27













  • I'm pretty sure thats not the main problem there That's why I added it as a comment. The forum will highlight class names. It makes it easier for us (or anybody else) to read your code when you follow standards. There is no reason to not follow standards. Everybody benefits.

    – camickr
    Nov 14 '18 at 17:40



















Each work in a class name should start with an upper case character. "hello_world" should be HelloWorld. When in doubt follow the format of the class names found in the Java API.

– camickr
Nov 14 '18 at 16:24







Each work in a class name should start with an upper case character. "hello_world" should be HelloWorld. When in doubt follow the format of the class names found in the Java API.

– camickr
Nov 14 '18 at 16:24















You're right, but I'm pretty sure thats not the main problem there ;)

– Jere
Nov 14 '18 at 16:27







You're right, but I'm pretty sure thats not the main problem there ;)

– Jere
Nov 14 '18 at 16:27















I'm pretty sure thats not the main problem there That's why I added it as a comment. The forum will highlight class names. It makes it easier for us (or anybody else) to read your code when you follow standards. There is no reason to not follow standards. Everybody benefits.

– camickr
Nov 14 '18 at 17:40







I'm pretty sure thats not the main problem there That's why I added it as a comment. The forum will highlight class names. It makes it easier for us (or anybody else) to read your code when you follow standards. There is no reason to not follow standards. Everybody benefits.

– camickr
Nov 14 '18 at 17:40














1 Answer
1






active

oldest

votes


















1














Swing uses layout managers. The default layout manager for a JFrame is the BorderLayout. When you add a component to the frame the default constraint used is BorderLayout.CENTER. Only a single component can be added to the CENTER of the BorderLayout.



The setBounds(...) will only work temporarily. As soon as the frame is resized the layout manager is invoked and the button will be given the new size/location based on the rules of the layout manager.



The solution is to use layout managers properly. I'm not sure what layout you are trying to achieve so all I can suggest is that you read the Swing tutorial on Layout Managers for working examples to get you started. Then you can nest panels with different layout managers to achieve your desired effect.



Start with something simple like:



frame.add(button, BorderLayout.PAGE_END);
frame.setVisible(true);
//frame.add(button);


The above will display your graph in the center of the frame and the button at the bottom.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304408%2fjgraphx-is-ruining-the-bounds-of-swing-components%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Swing uses layout managers. The default layout manager for a JFrame is the BorderLayout. When you add a component to the frame the default constraint used is BorderLayout.CENTER. Only a single component can be added to the CENTER of the BorderLayout.



    The setBounds(...) will only work temporarily. As soon as the frame is resized the layout manager is invoked and the button will be given the new size/location based on the rules of the layout manager.



    The solution is to use layout managers properly. I'm not sure what layout you are trying to achieve so all I can suggest is that you read the Swing tutorial on Layout Managers for working examples to get you started. Then you can nest panels with different layout managers to achieve your desired effect.



    Start with something simple like:



    frame.add(button, BorderLayout.PAGE_END);
    frame.setVisible(true);
    //frame.add(button);


    The above will display your graph in the center of the frame and the button at the bottom.






    share|improve this answer






























      1














      Swing uses layout managers. The default layout manager for a JFrame is the BorderLayout. When you add a component to the frame the default constraint used is BorderLayout.CENTER. Only a single component can be added to the CENTER of the BorderLayout.



      The setBounds(...) will only work temporarily. As soon as the frame is resized the layout manager is invoked and the button will be given the new size/location based on the rules of the layout manager.



      The solution is to use layout managers properly. I'm not sure what layout you are trying to achieve so all I can suggest is that you read the Swing tutorial on Layout Managers for working examples to get you started. Then you can nest panels with different layout managers to achieve your desired effect.



      Start with something simple like:



      frame.add(button, BorderLayout.PAGE_END);
      frame.setVisible(true);
      //frame.add(button);


      The above will display your graph in the center of the frame and the button at the bottom.






      share|improve this answer




























        1












        1








        1







        Swing uses layout managers. The default layout manager for a JFrame is the BorderLayout. When you add a component to the frame the default constraint used is BorderLayout.CENTER. Only a single component can be added to the CENTER of the BorderLayout.



        The setBounds(...) will only work temporarily. As soon as the frame is resized the layout manager is invoked and the button will be given the new size/location based on the rules of the layout manager.



        The solution is to use layout managers properly. I'm not sure what layout you are trying to achieve so all I can suggest is that you read the Swing tutorial on Layout Managers for working examples to get you started. Then you can nest panels with different layout managers to achieve your desired effect.



        Start with something simple like:



        frame.add(button, BorderLayout.PAGE_END);
        frame.setVisible(true);
        //frame.add(button);


        The above will display your graph in the center of the frame and the button at the bottom.






        share|improve this answer















        Swing uses layout managers. The default layout manager for a JFrame is the BorderLayout. When you add a component to the frame the default constraint used is BorderLayout.CENTER. Only a single component can be added to the CENTER of the BorderLayout.



        The setBounds(...) will only work temporarily. As soon as the frame is resized the layout manager is invoked and the button will be given the new size/location based on the rules of the layout manager.



        The solution is to use layout managers properly. I'm not sure what layout you are trying to achieve so all I can suggest is that you read the Swing tutorial on Layout Managers for working examples to get you started. Then you can nest panels with different layout managers to achieve your desired effect.



        Start with something simple like:



        frame.add(button, BorderLayout.PAGE_END);
        frame.setVisible(true);
        //frame.add(button);


        The above will display your graph in the center of the frame and the button at the bottom.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 16:23

























        answered Nov 14 '18 at 16:18









        camickrcamickr

        275k15127239




        275k15127239
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304408%2fjgraphx-is-ruining-the-bounds-of-swing-components%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python