JGraphX is ruining the bounds of Swing-components
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);
}
}
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):
java swing jbutton jgraphx jgraph
add a comment |
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);
}
}
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):
java swing jbutton jgraphx jgraph
Each work in a class name should start with an upper case character. "hello_world" should beHelloWorld
. 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
add a comment |
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);
}
}
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):
java swing jbutton jgraphx jgraph
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);
}
}
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):
java swing jbutton jgraphx jgraph
java swing jbutton jgraphx jgraph
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 beHelloWorld
. 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
add a comment |
Each work in a class name should start with an upper case character. "hello_world" should beHelloWorld
. 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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 14 '18 at 16:23
answered Nov 14 '18 at 16:18
camickrcamickr
275k15127239
275k15127239
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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