How to repaint all JPanels upon resizing?
I currently have 2 JPanels being added to a JFrame. Upon resizing the window, only the most recently added JPanel gets resized. How can I ensure that all JPanels resize? In my program, I launch a firework and have gridlines. I want each to hug the bottom of the screen.
With this code, the launched firework resizes to hug, but the grid lines dont:
JFrame frame = new JFrame();
frame.setSize(950,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuiTest test = new GuiTest();
frame.add(test, BorderLayout.PAGE_END);
frame.setVisible(true);
Grid grid = new Grid();
frame.add(grid);
frame.setVisible(true);
FireworkP canvas = new FireworkP(150, 45, 7, 2);
frame.add(canvas);
frame.setVisible(true);
frame.getContentPane().setBackground(Color.BLACK);
frame.setVisible(true);
With this code, the opposite occurs:
JFrame frame = new JFrame();
frame.setSize(950,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuiTest test = new GuiTest();
frame.add(test, BorderLayout.PAGE_END);
frame.setVisible(true);
FireworkP canvas = new FireworkP(150, 45, 7, 2);
frame.add(canvas);
frame.setVisible(true);
frame.getContentPane().setBackground(Color.BLACK);
frame.setVisible(true);
Grid grid = new Grid();
frame.add(grid);
frame.setVisible(true);
Default state: https://i.imgur.com/cpzpBha.png
Code 1 resized: https://i.imgur.com/SRdh2oA.png
Code 2 resized: https://i.imgur.com/VHY4Ff1.png
java swing graphics jframe jpanel
add a comment |
I currently have 2 JPanels being added to a JFrame. Upon resizing the window, only the most recently added JPanel gets resized. How can I ensure that all JPanels resize? In my program, I launch a firework and have gridlines. I want each to hug the bottom of the screen.
With this code, the launched firework resizes to hug, but the grid lines dont:
JFrame frame = new JFrame();
frame.setSize(950,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuiTest test = new GuiTest();
frame.add(test, BorderLayout.PAGE_END);
frame.setVisible(true);
Grid grid = new Grid();
frame.add(grid);
frame.setVisible(true);
FireworkP canvas = new FireworkP(150, 45, 7, 2);
frame.add(canvas);
frame.setVisible(true);
frame.getContentPane().setBackground(Color.BLACK);
frame.setVisible(true);
With this code, the opposite occurs:
JFrame frame = new JFrame();
frame.setSize(950,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuiTest test = new GuiTest();
frame.add(test, BorderLayout.PAGE_END);
frame.setVisible(true);
FireworkP canvas = new FireworkP(150, 45, 7, 2);
frame.add(canvas);
frame.setVisible(true);
frame.getContentPane().setBackground(Color.BLACK);
frame.setVisible(true);
Grid grid = new Grid();
frame.add(grid);
frame.setVisible(true);
Default state: https://i.imgur.com/cpzpBha.png
Code 1 resized: https://i.imgur.com/SRdh2oA.png
Code 2 resized: https://i.imgur.com/VHY4Ff1.png
java swing graphics jframe jpanel
All Swing components should repaint themselves if resized. You should probably assume that one JPanel wasn't resized or was for some other reason not updated. Are you using any custom (not Swing, but your own work) components?
– markspace
Nov 14 '18 at 21:33
You callframe.add(canvas);
twice with different parameters. Only the last one will be active. You can only have one main content at a time.
– markspace
Nov 14 '18 at 21:37
So is there any way to have a separate grid line class or should I just combine them? I dont understand why they arent both active.
– mander39
Nov 14 '18 at 21:38
For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.
– Andrew Thompson
Nov 14 '18 at 21:46
add a comment |
I currently have 2 JPanels being added to a JFrame. Upon resizing the window, only the most recently added JPanel gets resized. How can I ensure that all JPanels resize? In my program, I launch a firework and have gridlines. I want each to hug the bottom of the screen.
With this code, the launched firework resizes to hug, but the grid lines dont:
JFrame frame = new JFrame();
frame.setSize(950,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuiTest test = new GuiTest();
frame.add(test, BorderLayout.PAGE_END);
frame.setVisible(true);
Grid grid = new Grid();
frame.add(grid);
frame.setVisible(true);
FireworkP canvas = new FireworkP(150, 45, 7, 2);
frame.add(canvas);
frame.setVisible(true);
frame.getContentPane().setBackground(Color.BLACK);
frame.setVisible(true);
With this code, the opposite occurs:
JFrame frame = new JFrame();
frame.setSize(950,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuiTest test = new GuiTest();
frame.add(test, BorderLayout.PAGE_END);
frame.setVisible(true);
FireworkP canvas = new FireworkP(150, 45, 7, 2);
frame.add(canvas);
frame.setVisible(true);
frame.getContentPane().setBackground(Color.BLACK);
frame.setVisible(true);
Grid grid = new Grid();
frame.add(grid);
frame.setVisible(true);
Default state: https://i.imgur.com/cpzpBha.png
Code 1 resized: https://i.imgur.com/SRdh2oA.png
Code 2 resized: https://i.imgur.com/VHY4Ff1.png
java swing graphics jframe jpanel
I currently have 2 JPanels being added to a JFrame. Upon resizing the window, only the most recently added JPanel gets resized. How can I ensure that all JPanels resize? In my program, I launch a firework and have gridlines. I want each to hug the bottom of the screen.
With this code, the launched firework resizes to hug, but the grid lines dont:
JFrame frame = new JFrame();
frame.setSize(950,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuiTest test = new GuiTest();
frame.add(test, BorderLayout.PAGE_END);
frame.setVisible(true);
Grid grid = new Grid();
frame.add(grid);
frame.setVisible(true);
FireworkP canvas = new FireworkP(150, 45, 7, 2);
frame.add(canvas);
frame.setVisible(true);
frame.getContentPane().setBackground(Color.BLACK);
frame.setVisible(true);
With this code, the opposite occurs:
JFrame frame = new JFrame();
frame.setSize(950,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuiTest test = new GuiTest();
frame.add(test, BorderLayout.PAGE_END);
frame.setVisible(true);
FireworkP canvas = new FireworkP(150, 45, 7, 2);
frame.add(canvas);
frame.setVisible(true);
frame.getContentPane().setBackground(Color.BLACK);
frame.setVisible(true);
Grid grid = new Grid();
frame.add(grid);
frame.setVisible(true);
Default state: https://i.imgur.com/cpzpBha.png
Code 1 resized: https://i.imgur.com/SRdh2oA.png
Code 2 resized: https://i.imgur.com/VHY4Ff1.png
java swing graphics jframe jpanel
java swing graphics jframe jpanel
asked Nov 14 '18 at 21:30
mander39mander39
142
142
All Swing components should repaint themselves if resized. You should probably assume that one JPanel wasn't resized or was for some other reason not updated. Are you using any custom (not Swing, but your own work) components?
– markspace
Nov 14 '18 at 21:33
You callframe.add(canvas);
twice with different parameters. Only the last one will be active. You can only have one main content at a time.
– markspace
Nov 14 '18 at 21:37
So is there any way to have a separate grid line class or should I just combine them? I dont understand why they arent both active.
– mander39
Nov 14 '18 at 21:38
For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.
– Andrew Thompson
Nov 14 '18 at 21:46
add a comment |
All Swing components should repaint themselves if resized. You should probably assume that one JPanel wasn't resized or was for some other reason not updated. Are you using any custom (not Swing, but your own work) components?
– markspace
Nov 14 '18 at 21:33
You callframe.add(canvas);
twice with different parameters. Only the last one will be active. You can only have one main content at a time.
– markspace
Nov 14 '18 at 21:37
So is there any way to have a separate grid line class or should I just combine them? I dont understand why they arent both active.
– mander39
Nov 14 '18 at 21:38
For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.
– Andrew Thompson
Nov 14 '18 at 21:46
All Swing components should repaint themselves if resized. You should probably assume that one JPanel wasn't resized or was for some other reason not updated. Are you using any custom (not Swing, but your own work) components?
– markspace
Nov 14 '18 at 21:33
All Swing components should repaint themselves if resized. You should probably assume that one JPanel wasn't resized or was for some other reason not updated. Are you using any custom (not Swing, but your own work) components?
– markspace
Nov 14 '18 at 21:33
You call
frame.add(canvas);
twice with different parameters. Only the last one will be active. You can only have one main content at a time.– markspace
Nov 14 '18 at 21:37
You call
frame.add(canvas);
twice with different parameters. Only the last one will be active. You can only have one main content at a time.– markspace
Nov 14 '18 at 21:37
So is there any way to have a separate grid line class or should I just combine them? I dont understand why they arent both active.
– mander39
Nov 14 '18 at 21:38
So is there any way to have a separate grid line class or should I just combine them? I dont understand why they arent both active.
– mander39
Nov 14 '18 at 21:38
For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.
– Andrew Thompson
Nov 14 '18 at 21:46
For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.
– Andrew Thompson
Nov 14 '18 at 21:46
add a comment |
1 Answer
1
active
oldest
votes
So is there any way to have a separate grid line class or should I just combine them?
You build a parent / child relationship.
So you logic would be something like:
FireworkP foreground = new FireworkP(150, 45, 7, 2);
foreground.setOpaque( false );
Grid background = new Grid();
background.add(foreground);
frame.add(background);
frame.setVisible(true);
The above assumes:
- your FireworkP and Grid classes are JPanels where you do custom painting.
- your classes use an appropriate layout manager
So now you GUI has a structure like:
- frame
- background
- foreground
So you have components that can paint themselves on top of one another on the Z axis.
If you add more than one component to the same panel then the components are painted on the X/Y axis
Trying this, the firework is not showing up
– mander39
Nov 14 '18 at 21:50
@mander39 Read the section from the Swing tutorial on Custom Painting. I would guess your fireworks have a preferred size of (0, 0) so there is nothing to paint. You need to override thegetPreferredSize()
method to return the size of your panel so the layout manager can do its job.
– camickr
Nov 14 '18 at 21:55
Alright, using the tutorial everything shows up, but its still onjly updating the position of 1/2 panels. I think its just time to scrap the code, having 2 panels just does not work. Ill put the gird in the firework paintComponent somehow.
– mander39
Nov 14 '18 at 22:13
Took 5 mins to just combine them, anyone reading this for help just combine your paintComponents and add gui elements that alter and repaint the graphics accordingly
– mander39
Nov 14 '18 at 22:21
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%2f53309020%2fhow-to-repaint-all-jpanels-upon-resizing%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
So is there any way to have a separate grid line class or should I just combine them?
You build a parent / child relationship.
So you logic would be something like:
FireworkP foreground = new FireworkP(150, 45, 7, 2);
foreground.setOpaque( false );
Grid background = new Grid();
background.add(foreground);
frame.add(background);
frame.setVisible(true);
The above assumes:
- your FireworkP and Grid classes are JPanels where you do custom painting.
- your classes use an appropriate layout manager
So now you GUI has a structure like:
- frame
- background
- foreground
So you have components that can paint themselves on top of one another on the Z axis.
If you add more than one component to the same panel then the components are painted on the X/Y axis
Trying this, the firework is not showing up
– mander39
Nov 14 '18 at 21:50
@mander39 Read the section from the Swing tutorial on Custom Painting. I would guess your fireworks have a preferred size of (0, 0) so there is nothing to paint. You need to override thegetPreferredSize()
method to return the size of your panel so the layout manager can do its job.
– camickr
Nov 14 '18 at 21:55
Alright, using the tutorial everything shows up, but its still onjly updating the position of 1/2 panels. I think its just time to scrap the code, having 2 panels just does not work. Ill put the gird in the firework paintComponent somehow.
– mander39
Nov 14 '18 at 22:13
Took 5 mins to just combine them, anyone reading this for help just combine your paintComponents and add gui elements that alter and repaint the graphics accordingly
– mander39
Nov 14 '18 at 22:21
add a comment |
So is there any way to have a separate grid line class or should I just combine them?
You build a parent / child relationship.
So you logic would be something like:
FireworkP foreground = new FireworkP(150, 45, 7, 2);
foreground.setOpaque( false );
Grid background = new Grid();
background.add(foreground);
frame.add(background);
frame.setVisible(true);
The above assumes:
- your FireworkP and Grid classes are JPanels where you do custom painting.
- your classes use an appropriate layout manager
So now you GUI has a structure like:
- frame
- background
- foreground
So you have components that can paint themselves on top of one another on the Z axis.
If you add more than one component to the same panel then the components are painted on the X/Y axis
Trying this, the firework is not showing up
– mander39
Nov 14 '18 at 21:50
@mander39 Read the section from the Swing tutorial on Custom Painting. I would guess your fireworks have a preferred size of (0, 0) so there is nothing to paint. You need to override thegetPreferredSize()
method to return the size of your panel so the layout manager can do its job.
– camickr
Nov 14 '18 at 21:55
Alright, using the tutorial everything shows up, but its still onjly updating the position of 1/2 panels. I think its just time to scrap the code, having 2 panels just does not work. Ill put the gird in the firework paintComponent somehow.
– mander39
Nov 14 '18 at 22:13
Took 5 mins to just combine them, anyone reading this for help just combine your paintComponents and add gui elements that alter and repaint the graphics accordingly
– mander39
Nov 14 '18 at 22:21
add a comment |
So is there any way to have a separate grid line class or should I just combine them?
You build a parent / child relationship.
So you logic would be something like:
FireworkP foreground = new FireworkP(150, 45, 7, 2);
foreground.setOpaque( false );
Grid background = new Grid();
background.add(foreground);
frame.add(background);
frame.setVisible(true);
The above assumes:
- your FireworkP and Grid classes are JPanels where you do custom painting.
- your classes use an appropriate layout manager
So now you GUI has a structure like:
- frame
- background
- foreground
So you have components that can paint themselves on top of one another on the Z axis.
If you add more than one component to the same panel then the components are painted on the X/Y axis
So is there any way to have a separate grid line class or should I just combine them?
You build a parent / child relationship.
So you logic would be something like:
FireworkP foreground = new FireworkP(150, 45, 7, 2);
foreground.setOpaque( false );
Grid background = new Grid();
background.add(foreground);
frame.add(background);
frame.setVisible(true);
The above assumes:
- your FireworkP and Grid classes are JPanels where you do custom painting.
- your classes use an appropriate layout manager
So now you GUI has a structure like:
- frame
- background
- foreground
So you have components that can paint themselves on top of one another on the Z axis.
If you add more than one component to the same panel then the components are painted on the X/Y axis
answered Nov 14 '18 at 21:42
camickrcamickr
275k15127239
275k15127239
Trying this, the firework is not showing up
– mander39
Nov 14 '18 at 21:50
@mander39 Read the section from the Swing tutorial on Custom Painting. I would guess your fireworks have a preferred size of (0, 0) so there is nothing to paint. You need to override thegetPreferredSize()
method to return the size of your panel so the layout manager can do its job.
– camickr
Nov 14 '18 at 21:55
Alright, using the tutorial everything shows up, but its still onjly updating the position of 1/2 panels. I think its just time to scrap the code, having 2 panels just does not work. Ill put the gird in the firework paintComponent somehow.
– mander39
Nov 14 '18 at 22:13
Took 5 mins to just combine them, anyone reading this for help just combine your paintComponents and add gui elements that alter and repaint the graphics accordingly
– mander39
Nov 14 '18 at 22:21
add a comment |
Trying this, the firework is not showing up
– mander39
Nov 14 '18 at 21:50
@mander39 Read the section from the Swing tutorial on Custom Painting. I would guess your fireworks have a preferred size of (0, 0) so there is nothing to paint. You need to override thegetPreferredSize()
method to return the size of your panel so the layout manager can do its job.
– camickr
Nov 14 '18 at 21:55
Alright, using the tutorial everything shows up, but its still onjly updating the position of 1/2 panels. I think its just time to scrap the code, having 2 panels just does not work. Ill put the gird in the firework paintComponent somehow.
– mander39
Nov 14 '18 at 22:13
Took 5 mins to just combine them, anyone reading this for help just combine your paintComponents and add gui elements that alter and repaint the graphics accordingly
– mander39
Nov 14 '18 at 22:21
Trying this, the firework is not showing up
– mander39
Nov 14 '18 at 21:50
Trying this, the firework is not showing up
– mander39
Nov 14 '18 at 21:50
@mander39 Read the section from the Swing tutorial on Custom Painting. I would guess your fireworks have a preferred size of (0, 0) so there is nothing to paint. You need to override the
getPreferredSize()
method to return the size of your panel so the layout manager can do its job.– camickr
Nov 14 '18 at 21:55
@mander39 Read the section from the Swing tutorial on Custom Painting. I would guess your fireworks have a preferred size of (0, 0) so there is nothing to paint. You need to override the
getPreferredSize()
method to return the size of your panel so the layout manager can do its job.– camickr
Nov 14 '18 at 21:55
Alright, using the tutorial everything shows up, but its still onjly updating the position of 1/2 panels. I think its just time to scrap the code, having 2 panels just does not work. Ill put the gird in the firework paintComponent somehow.
– mander39
Nov 14 '18 at 22:13
Alright, using the tutorial everything shows up, but its still onjly updating the position of 1/2 panels. I think its just time to scrap the code, having 2 panels just does not work. Ill put the gird in the firework paintComponent somehow.
– mander39
Nov 14 '18 at 22:13
Took 5 mins to just combine them, anyone reading this for help just combine your paintComponents and add gui elements that alter and repaint the graphics accordingly
– mander39
Nov 14 '18 at 22:21
Took 5 mins to just combine them, anyone reading this for help just combine your paintComponents and add gui elements that alter and repaint the graphics accordingly
– mander39
Nov 14 '18 at 22:21
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%2f53309020%2fhow-to-repaint-all-jpanels-upon-resizing%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
All Swing components should repaint themselves if resized. You should probably assume that one JPanel wasn't resized or was for some other reason not updated. Are you using any custom (not Swing, but your own work) components?
– markspace
Nov 14 '18 at 21:33
You call
frame.add(canvas);
twice with different parameters. Only the last one will be active. You can only have one main content at a time.– markspace
Nov 14 '18 at 21:37
So is there any way to have a separate grid line class or should I just combine them? I dont understand why they arent both active.
– mander39
Nov 14 '18 at 21:38
For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.
– Andrew Thompson
Nov 14 '18 at 21:46