Swing java, window split with content in both halves - java

I am trying to write a program with Java using Swing that will display a window as such:
+--------------+-----------------------------------+
| | |
| | |
| | |
| | |
| | |
| | |
| | |
+--------------+-----------------------------------+
With the left window having text files, and the right window loading the text files. The problem is two-fold. For one, Swing is giving me mental fits, as I cannot seem to understand how splitting a frame works.
The second half of the problem is that when I get text in the right side, its just randomly places and has white space around it. I want it to display like a windows explorer window, but the text formatted more like a text viewer, if that makes sense.
Here is what I have tried.
package myCB;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class MyCB extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new MyCB();
}
public MyCB() {
super("MyCB.0.0.0.1");
setResizable(true);
setSize(750, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Dimension size1 = new Dimension(200, 400);
// Dimension size2 = new Dimension(500, 400);
JPanel p = new JPanel();
JPanel b1 = new JPanel();
b1.setPreferredSize(new Dimension(200, 400));
JPanel b2 = new JPanel();
b2.setPreferredSize(new Dimension(100, 100));
b1.setVisible(true);
b2.setVisible(true);
JTextArea text = new JTextArea("This is a sample of a text area" + "in Java Swing for my program.");
text.setLineWrap(true);
text.setWrapStyleWord(true);
b2.add(text);
p.setLayout(new BorderLayout());
setVisible(true);
p.add(b1, BorderLayout.LINE_START);
p.add(b2, BorderLayout.CENTER);
p.setVisible(true);
repaint();
add(p);
revalidate();
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
Any help would be greatly appreciated. I have been struggling with Swing for weeks. Thanks!

I write a small example for you, try to inspire for it
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class SplitPane extends JFrame {
private JPanel p1 = new JPanel(), p2 = new JPanel();
private JSplitPane jsp;
public SplitPane() {
super("SplitPane example");
setDefaultCloseOperation(EXIT_ON_CLOSE);
p1.setBackground(Color.orange);
p2.setBackground(Color.green);
jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, p1, p2);
jsp.setDividerLocation(getWidth() / 2);
setContentPane(jsp);
setSize(300, 300);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new SplitPane();
}
}
Hope that helps, if so make up please =) Salam

You should start reading on the different swing layouts helping you organize the space without manually setting the sizes yourself. There are many examples on the different layouts on The Java Tutorial.
You can see examples to do exactly what you want to have as a result here: You set the FlowLayout and then set the preferred size for the left panel. The size of the left one is automatically set, based on the size of the window.

I have been struggling with Swing for weeks.
Start by reading the Swing tutorial. There are plenty of working examples there to get you started.
There are plenty of issues with your code:
Don't use setPreferredSize(). Swing was designed to be used with layout managers. Then the layout manager will determine the appropriate size for each component. Read the tutorial section on layout managers
A JTextArea should be displayed in a JScrollPane and then the scroll pane added to the panel. You may want to create you text area using new JTextArea(5, 30), to give a hint for the preferred size of the text area. Read the tutorial section on text component features
You should only invoke the setVisible() method of the frame AFTER all components have been added to the frame. When you do this there is no need to invoke repaint() or revalidate(). Also, you should invoke pack() just before the setVisible() so all component are displayed at their preferred sizes.
GUI components should be created on the Event Dispatch Thread. Read the section on Concurrency.
its just randomly places and has white space around it.
This is normal. When you add a component to the CENTER, it occupies all the remaining available space on the frame.
but the text formatted more like a text viewer, if that makes sense.
Doesn't make sense to me. A text area is just like the area you where you typed your question into on this forum.

It sounds like you're looking for a JSplitPane. It's very easy too use, you simple specify the orientation (Horizantal or Vertical) and you can either use two JPanels or other containers/components. Anyway here is a simple example:
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
JLabel lblRight = new JLabel("Right");
rightPanel.add(lblRight);
JLabel lblLeft = new JLabel("Left");
leftPanel.add(lblLeft);
And since you said you've been struggling with swing for such a long time have you tried the windowbuilder plugin?

Related

Can not display the features using JFrame [duplicate]

I'm fairly new to JFrame and I want to know why my items are not showing up on the window. I know i dont have a ActionHandler but I just want my textfield's to show up on my window. Here's my code:
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class FirstGUI extends JFrame{
public void GUI(){
setTitle("Welcome");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(600,600);
JLabel title = new JLabel();
title.setText("Apple Inc. Member Login Port");
title.setFont(new Font("Arial", Font.PLAIN, 24));
JTextField login = new JTextField("Login",10);
JPasswordField pass = new JPasswordField("Password");
add(title);
add(login);
add(pass);
}
public static void main(String[] args){
FirstGUI a = new FirstGUI();
a.GUI();
}
}
but when i run it i get this:
but when i run it i get this:
You get an empty screen because you add the components to the frame after the frame is visible.
As has already been suggested you need to use an appropriate layout manager. FlowLayout is the easiest to start with.
invoke setVisible(true) AFTER adding the components to the frame.
So the code should be more like:
panel.add(...);
panel.add(...);
add(panel);
pack();
setVisible(true);
I agree to MadProgrammer's suggestions (+1)
Well, lets take a look at your program though
You actually have created a JFrame with components in it. Its working fine as well, but your question of "why are my items not showing up in the JFrame" is not because you did something wrong but because missed out something i.e. revalidate()
Try:
public static void main(String[] args){
FirstGUI a = new FirstGUI();
a.GUI();
a.revalidate();
}
I'm not saying this will give you perfect UI.. what I'm trying to say is this will help you understand the Swing better. Learn about Swing Layout managers and then work on your UI to have better results
revalidate(): This component and all parents above it are marked as needing to be laid out. This means the Layout Manager will try to realign the components. Often used after removing components. It is possible that some really sharp swing people may miss this. I would think that you will only know this if you are actually using Swing.
The default layout manager for JFrame is BorderLayout.
This means that your components are essentially all been added ontop of each other.
Try changing the layout manager to something like FlowLayout (for example)...
Take a look at A Visual Guide to Layout Managers and Using Layout Managers for more details.
Also, avoid setSize where possible, use Window#pack instead
Update
I'd also like to introduce you to Initial Threads which should be used to launch your UI code...
The only one reason :
setVisible(True); method for the frame should be put on the end of the code.
if you give this line on the top of the code that is when you create a frame. This will cause that problem.
Don't add the components directly to your frame. Instead add to the content pane, which is where a JFrame stores all of the components that it draws. Usually this is a JPanel.
Here is an example:
public class GUI
{
private JPanel content;
public void GUI
{
/*Other code*/
content = new JPanel();
add(content); //make content the content pane
content.add(title);
content.add(login);
content.add(pass);
}
If that fails, call setVisible(true) and setEnabled(true) on all of your components.
On a side note you may want to make your GUI function a constructor.
import javax.swing.*;
import java.awt.*;
class Myframec extends JFrame
{
Myframec()
{
Container c = this.getContentPane();
c.setLayout(null);
this.setBounds(10,10,700,500);
this.setTitle("Welcome");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBounds(0,0,700,500);
panel.setBackground(Color.gray);
panel.setLayout(null);
c.add(panel);
Font f = new Font("Arial",Font.BOLD,25);
Font f1 = new Font("Arial",Font.BOLD,20);
JLabel lable = new JLabel();
lable.setBounds(130,10,400,100);
lable.setText("Apple Inc. Member Login Port");
lable.setFont(f);
panel.add(lable);
JTextField login = new JTextField("Login",10);
login.setBounds(120,150,400,30);
login.setFont(f1);
panel.add(login);
JPasswordField pass =new JPasswordField("Password");
pass.setBounds(120,200,400,30);
pass.setFont(f1);
lable.setFont(f);
panel.add(pass);
c.setVisible(true);
this.setVisible(true);
}
public static void main(String[] argm)
{
Myframec frame = new Myframec();
frame.setVisible(true);
}
}

JFrame Image wont appear, require refresh? [duplicate]

I'm fairly new to JFrame and I want to know why my items are not showing up on the window. I know i dont have a ActionHandler but I just want my textfield's to show up on my window. Here's my code:
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class FirstGUI extends JFrame{
public void GUI(){
setTitle("Welcome");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(600,600);
JLabel title = new JLabel();
title.setText("Apple Inc. Member Login Port");
title.setFont(new Font("Arial", Font.PLAIN, 24));
JTextField login = new JTextField("Login",10);
JPasswordField pass = new JPasswordField("Password");
add(title);
add(login);
add(pass);
}
public static void main(String[] args){
FirstGUI a = new FirstGUI();
a.GUI();
}
}
but when i run it i get this:
but when i run it i get this:
You get an empty screen because you add the components to the frame after the frame is visible.
As has already been suggested you need to use an appropriate layout manager. FlowLayout is the easiest to start with.
invoke setVisible(true) AFTER adding the components to the frame.
So the code should be more like:
panel.add(...);
panel.add(...);
add(panel);
pack();
setVisible(true);
I agree to MadProgrammer's suggestions (+1)
Well, lets take a look at your program though
You actually have created a JFrame with components in it. Its working fine as well, but your question of "why are my items not showing up in the JFrame" is not because you did something wrong but because missed out something i.e. revalidate()
Try:
public static void main(String[] args){
FirstGUI a = new FirstGUI();
a.GUI();
a.revalidate();
}
I'm not saying this will give you perfect UI.. what I'm trying to say is this will help you understand the Swing better. Learn about Swing Layout managers and then work on your UI to have better results
revalidate(): This component and all parents above it are marked as needing to be laid out. This means the Layout Manager will try to realign the components. Often used after removing components. It is possible that some really sharp swing people may miss this. I would think that you will only know this if you are actually using Swing.
The default layout manager for JFrame is BorderLayout.
This means that your components are essentially all been added ontop of each other.
Try changing the layout manager to something like FlowLayout (for example)...
Take a look at A Visual Guide to Layout Managers and Using Layout Managers for more details.
Also, avoid setSize where possible, use Window#pack instead
Update
I'd also like to introduce you to Initial Threads which should be used to launch your UI code...
The only one reason :
setVisible(True); method for the frame should be put on the end of the code.
if you give this line on the top of the code that is when you create a frame. This will cause that problem.
Don't add the components directly to your frame. Instead add to the content pane, which is where a JFrame stores all of the components that it draws. Usually this is a JPanel.
Here is an example:
public class GUI
{
private JPanel content;
public void GUI
{
/*Other code*/
content = new JPanel();
add(content); //make content the content pane
content.add(title);
content.add(login);
content.add(pass);
}
If that fails, call setVisible(true) and setEnabled(true) on all of your components.
On a side note you may want to make your GUI function a constructor.
import javax.swing.*;
import java.awt.*;
class Myframec extends JFrame
{
Myframec()
{
Container c = this.getContentPane();
c.setLayout(null);
this.setBounds(10,10,700,500);
this.setTitle("Welcome");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBounds(0,0,700,500);
panel.setBackground(Color.gray);
panel.setLayout(null);
c.add(panel);
Font f = new Font("Arial",Font.BOLD,25);
Font f1 = new Font("Arial",Font.BOLD,20);
JLabel lable = new JLabel();
lable.setBounds(130,10,400,100);
lable.setText("Apple Inc. Member Login Port");
lable.setFont(f);
panel.add(lable);
JTextField login = new JTextField("Login",10);
login.setBounds(120,150,400,30);
login.setFont(f1);
panel.add(login);
JPasswordField pass =new JPasswordField("Password");
pass.setBounds(120,200,400,30);
pass.setFont(f1);
lable.setFont(f);
panel.add(pass);
c.setVisible(true);
this.setVisible(true);
}
public static void main(String[] argm)
{
Myframec frame = new Myframec();
frame.setVisible(true);
}
}

Java Swing simple center of JPanel in other JPanel

I have this incredibly easy task of wanting a nice centered JPanel inside another JPanel. The parent is set to 900, 550, and the child should be approximately 200,400 or so.
To do this, I thought giving the parent a BorderLayout and then setting the setPreferredSize(200, 400) of the child. This child would be in the CENTER. Two empty JPanels would be on the EAST and WEST. Of course this did not work. Giving the two sidepanels a setPreferredSize() of course DID work. Problem with this is that narrowing the Frame causes the center pane to go away.
Here's some sample code that should give show the issue:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Temporary {
public static Temporary myObj = null;
private JFrame mainFrame;
public void go(){
mainFrame = new JFrame("Swing");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setPreferredSize(new Dimension(900,550));
JPanel mainCards = new JPanel(new CardLayout());
mainCards.add(loginLayer(), "Login");
mainFrame.setContentPane(mainCards);
mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
public JPanel loginLayer(){
JPanel masterPane = new JPanel(new BorderLayout());
JPanel centerPane = new JPanel();
centerPane.setLayout(new BoxLayout(centerPane, BoxLayout.Y_AXIS));
centerPane.setPreferredSize(new Dimension(100,200));
JLabel label = new JLabel("Swing is overly");
label.setAlignmentX(Component.CENTER_ALIGNMENT);
centerPane.add(label);
JButton button = new JButton("complicated");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
centerPane.add(button);
JTextField textField = new JTextField(10);
centerPane.add(textField);
JPanel filler = new JPanel();
JPanel filler2 = new JPanel();
masterPane.add(filler, BorderLayout.WEST);
masterPane.add(centerPane, BorderLayout.CENTER);
masterPane.add(filler2, BorderLayout.EAST);
return masterPane;
}
public static void main(String[] args){
myObj = new Temporary();
myObj.go();
}
}
BorderLayout will, by it's nature, give as much of the available space as it can to the CENTER component. This is how it's designed.
If you want the component to be centered within the parent container, BUT maintain it's preferred size, you should consider using a GridBagLayout instead. Without any additional constraints, this should achieve the result you're after
For example...
public JPanel loginLayer(){
JPanel masterPane = new JPanel(new GridBagLayout);
JPanel centerPane = new JPanel();
centerPane.setLayout(new BoxLayout(centerPane, BoxLayout.Y_AXIS));
JLabel label = new JLabel("Swing is overly");
label.setAlignmentX(Component.CENTER_ALIGNMENT);
centerPane.add(label);
JButton button = new JButton("complicated");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
centerPane.add(button);
JTextField textField = new JTextField(10);
centerPane.add(textField);
masterPane.add(centerPane);
// Add additional borders to providing padding around the center pane
// as you need
return masterPane;
}
I would also avoid actively setting the preferred size of component in this way, as it's possible that the components you're adding to it will exceed your expectations, instead, make use of things like EmptyBorder (for example) to add additional white space arouond the component and it's contents
In Java Swing, you generally want to avoid creating a bunch of statically positioned items with preferred sizes and absolute positions, because things get weird with resizing (as you've noticed). Instead you want to rely on the fluid LayoutManagers. There is an excellent tutorial here. Or, if you want to supply a mock-up of some sort to show the actual UI you are trying to create, I could provide some more feedback.

JDesktopPane and BorderLayout

I am writing a program that attempts to simulate the evolution of a species, and it has a window that looks like this:
Originally the empty area in the bottom right was a Panel, and it is intended to draw a visual representation of the specimens, locations, and travel paths(doesn't really matter). However, you will be able to open some sort of window that allows you to create/edit different items(like species, locations, and travel paths). Originally I planned for those to simply be popup windows. But, I was thinking I would perhaps use JInternal panes for the popups, and the visual representation screen.
So in my JFrames constructor:
JDesktopPane pane = new JDesktopPane();
this.setContentPane(pane);
setLayout(new BorderLayout());//To layout the menubar, and the items on the left
panel = new GraphicsPanel(manager);
panel.setVisible(true);
And in Graphics Panel constructor:super("Graphic Project View",true,false,true,true);
This locks the Panel to BorderLayout.CENTER, and it fills up the entire space, not allowing for anything else. My guess this is because JDesktopPanes use an OverlayLayout, and when I set the layout to BorderLayout that overrides the OverlayLayout, and so my InternalFrame just gets added to the center.
So the question is:
How do I layout the things like the JMenuBar, and the left ward Panel as they are now, whilst still maintaining the capability to have JInternalFrames?
For now I am going to add the JMenuBar via JFrame.setJMenuBar(JMenuBar) instead of JFrame.add(menuBar,BorderLayout.NORTH), and then change the panel on the left into a JInternal frame, but if possible I'd rather have it as is. I would like it if I could just have the DesktopPane be added to the JFrame at BorderLayout.CENTER, and then just add the frame to the Desktop pane. If the InternalFrame were limited to that region I wouldn't care, as long as it's still mobile, ect.
EDIT: How I add JInternalFrame(Sorry it still says panel, but it has been converted to a JInternalFrame):
panel = new GraphicsPanel(manager);
panel.setSize(desktop.getSize());
panel.setLocation(0,0);
panel.setVisible(true);
desktop.add(panel);
I would start with a single JPanel (lets all it the base pane), which will house the other containers.
Using a border layout, I would add a "controls" panel to the WEST position of the base pane. Onto the CENTER position I would add the JDesktopPane.
I would set the main windows layout to BorderLayout and add the base pane to it. This will allow you to use JFrame#setJMenuBar to manage the menu bar while maintaining the result of the layout.
This will allow you to contain to use the JInternalFrames on the desktop without effecting the rest of the layout...
Simple Example
This is an overly simplified example used to demonstrate the basic concept described above...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class SimpleLayout {
public static void main(String[] args) {
new SimpleLayout();
}
public SimpleLayout() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JMenuBar mb = new JMenuBar();
mb.add(new JMenu("File"));
mb.add(new JMenu("Add"));
mb.add(new JMenu("Edit"));
mb.add(new JMenu("Analize"));
mb.add(new JMenu("About"));
JFrame frame = new JFrame("Testing");
frame.setJMenuBar(mb);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new BasePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class BasePane extends JPanel {
private JTextArea species;
private JTextArea locations;
private JTextArea travelPaths;
private JDesktopPane desktopPane;
public BasePane() {
setLayout(new BorderLayout());
desktopPane = new JDesktopPane();
species = new JTextArea("Species");
locations = new JTextArea("Locations");
travelPaths = new JTextArea("TravelPaths");
JPanel controls = new JPanel(new GridLayout(3, 0));
controls.add(new JScrollPane(species));
controls.add(new JScrollPane(locations));
controls.add(new JScrollPane(travelPaths));
add(controls, BorderLayout.WEST);
add(desktopPane);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
Your requirements might be slightly difference, but the basic concept should get you moving.
Depending on the structure of your application, I might be tempted to separate the Controls pane into a separate class as well.

How to Correctly Set Minimum Sizes in Java?

For a GUI application I am creating in Java, I have the following:
A JFrame, set to have a minimum size of (300,200)
A JSplitPane, in which lies:
On the left, a JScrollPane (containing a JTree) with a minimum size of (100,0) (I only want to restrict the width to 200)
On the right, a JPanel with a minimum size of (200,0)
The sizing does not give me any issue under the following conditions:
Resizing the JSplitPane all the way to the left (to the JScrollPane's minimum size), and subsequently resize the window afterward
Just resizing the window, to a certain degree
The problem occurs when I move the JSplitPane too close to the right, whereupon resizing the window the JPanel in the right of the JSplitPane fails to adhere to the minimum width I set.
I attempted setting a maximum width on the JScrollPane, which did not seem to help at all.
Is there something involving maximum sizes I must do? Or perhaps there is a way to attach a Listener to one of the panels to force my desired behavior? Ultimately, I just want the right panel in the JSplitPane to never be less than 200px wide.
Here is an example with behavior I am experiencing:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
public class ResizeTest
{
private JFrame frame;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
ResizeTest window = new ResizeTest();
window.frame.setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
public ResizeTest()
{
initialize();
}
private void initialize()
{
frame = new JFrame();
frame.setMinimumSize(new Dimension(300, 200));
frame.setBounds(100,100,450,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(0, 1, 0, 0));
JSplitPane splitPane = new JSplitPane();
frame.getContentPane().add(splitPane);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setMinimumSize(new Dimension(100, 0));
splitPane.setLeftComponent(scrollPane);
JTree tree = new JTree();
tree.setMinimumSize(new Dimension(100, 0));
scrollPane.setViewportView(tree);
JPanel panel = new JPanel();
panel.setMinimumSize(new Dimension(200, 0));
splitPane.setRightComponent(panel);
}
}
Update:
I'm afraid I don't fully understand the point trying to be made in the proposed solutions, except for that setPreferred() and setMinimum/Maximum() are better to be avoided.
My question in response to learning this is, what are my options for restricting the JSplitPane divider outside of using these methods? MadProgrammer mentioned listening for the componentResized event, but I need just a little more clarification as to why. Am I calling setDividerLocation() in response to this event?
I apologize in advance if the appended question is meant as a separate StackOverflow question entirely, I can post another and link here if necessary.
Update 2:
Is simply not regulating how the user chooses to size the window and having the right panel in a JScrollPane a viable option? This looks to me like somewhat of a standard practice.
Firstly, the method setMinimumSize is a suggestion to the LayoutManager API. A suggestion that may be ignored.
In order to be able to even come close to supporting this, you will need to use something like a ComponentListener and monitor the componentResized event.
The best solution I can think of is to use a LayoutManager that actually uses the minimum and maximum size constraints, something like GridBagLayout.
Use this on a "content" pane and place you're JSplitPane onto this (setting it's minimum and maximum size accordingly) then add the "content" pane to frame.
UPDATE
Sorry, I'm probably missing something really obvious, but I put this little test together, I hope it has some ideas that help :P
public class TestFrameSize extends JFrame {
public TestFrameSize() throws HeadlessException {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 600);
setLocationRelativeTo(null);
setMinimumSize(new Dimension(250, 250));
JLabel left = new JLabel("Left");
JLabel right = new JLabel("Right");
Dimension pSize = new Dimension(100, 100);
Dimension mSize = new Dimension(25, 100);
left.setPreferredSize(pSize);
left.setMinimumSize(mSize);
right.setPreferredSize(pSize);
right.setMinimumSize(mSize);
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
JPanel content = new JPanel(new GridBagLayout());
content.add(pane);
setLayout(new BorderLayout());
add(content);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
new TestFrameSize().setVisible(true);
}
}
In this example, the preferred size of the content of a JSplitPane is initially set to a small multiple of the component's preferred size in getPreferredSize(). Note that the JSplitPane will become no smaller than the panel's minimum size, managed by the enclosed Jlabel.

Categories

Resources