0

I am coding this in Kotlin using Swing but I can work with any answers in Java as well.

I have a JFrame with a panel that uses the SpringLayout layout manager.

var layout = SpringLayout()
var frame = JFrame()
var mainPanel = JPanel(layout)

I want to split the screen in two. I know that there are other ways to do this, but I created two more JPanels. One will be on the top and one will be on the bottom.

var topPanel = JPanel()
var bottomPanel = JPanel()
//I've omitted ll of the objects inside these two panels.

I know how to get them on the top and bottom of the screen, but I want them to stay there when I change the screen's size. Currently I have this bit of code.

layout.putConstraint(SpringLayout.NORTH, bottomPanel, screenSize().height/2, SpringLayout.NORTH, mainPanel)

The screenSize method just returns the size of the JFrame. It only gets the size in that instance so it won't work for me anyway.

private fun screenSize(): Dimension = frame.size

I know that using Spring, I can set the objects to always be in the exact center of the screen using SpringLayout.VERTICAL_CENTER, for example. This means that Spring is able to update the object's position in real-time as the screen size changes. I want a more general version of this where I can, for example, set my bottomPanel's y position to be the screen height / 2. This would update the panel's position every time the screen changes, similar to how setting the position to ScreenLayout.VERTICAL_CENTER does but moved upwards. I also need to use this to set the panel's sizes, as I want them to each be the screen width / 2. I can get the screen width at any time, but once I set the bottomPanel's position to it, it only updates at that moment and then it just continues to use that value. Spring doesn't have a constant for the screen width or height as far as I know, so I can't plug one into the putConstraint method. I need some other alternative. I already considered a loop that just keeps updating it, but that would be extremely inefficient for this one task. I would like to solve this directly using SpringLayout functionality if possible.

KNOB Personal
  • 333
  • 4
  • 15
  • I'm not understanding your requirements. *I know how to get them on the top and bottom of the screen* - sounds like you should be using a BorderLayout. Or maybe a GridLayout. *set my bottomPanel's y position to be the screen width / 2.* - not sure why the "Y" position is controlled by the width? * I would like to solve this directly using SpringLayout functionality if possible* - use the best tool for the job. Don't force a single layout manager. Maybe nesting panels with different layout manager will accomplish the task easier. – camickr May 23 '20 at 14:14
  • I suggest you update the question with an ascii drawing showing what you are attempting to do which will help clarify the verbal description. – camickr May 23 '20 at 14:16
  • @camickr yeah, I made a mistake when I said the y position should be the width / 2. I meant the height / 2 and will correct it. Also, I know I can use other layout managers, but I want to know how to do it using Spring. I think I found the solution as well and am just running some tests to see if it works. If so I guess I will answer my own question so people can see that it's been resolved. – KNOB Personal May 23 '20 at 15:10

0 Answers0