Resizing a control using Java JSplitPane

Please see my image below at the link, and then read below to find out more about my problem.

Sample image

Imagine that this is a basic frame, split into two using JSplitPane, by default, when the frame is resized, the gray part changes its size, but I would like the white part to change in accordance with the change in frame size.

Any help in the right direction would be appreciated as I am working on the project now and I will try all kinds of weird things that will be prepared for my biggest project set for the new year. :)

Relations Theron

+4
source share
1 answer

You need to use setResizeWeight to get left and right extra space or to reduce the size of the JFrame, the code example below:

  import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JSplitPane; public class TestJSplitPane { private void init(){ JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); splitPane.setRightComponent(new JButton("Here I Am")); splitPane.setLeftComponent(new JButton("Me Too")); splitPane.setResizeWeight(0.5); frame.add(splitPane, BorderLayout.CENTER); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new TestJSplitPane().init(); } } 

Java Doc for setResizeWeight .

Hope this helps.

+10
source

Source: https://habr.com/ru/post/1382944/


All Articles