tags:

views:

288

answers:

5
+1  Q: 

Java GUI Problems

I'm designing a really simple GUI but without any luck. Basically the outer layout has two columns which is a splitter pane (this is for an IRC client I'm writing). On the left-hand side is the server/channel/user tree and on the right is the chat pane. Now I break this down so on the right-hand side there's the output pane at the top, and a single-lined input box at the bottom.

This is the code for creating the GUI on a whole (this is just a snippet)

ChatModel cm = new ChatModel();
ChatView cv = new ChatView(cm);

treeViewChatSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, serverTreeView, cv);
treeViewChatSplitPane.setSize(500, 500);
treeViewChatSplitPane.setBorder(null);
treeViewChatSplitPane.setDividerSize(3);
this.getContentPane().add(treeViewChatSplitPane);

this.setSize(500, 500);

The ChatView constructor has this in it (it extends java.awt.Container)

this.model = model;
this.setLayout(new BorderLayout());

mainTextArea = new JTextArea();
mainTextArea.setLineWrap(true);
mainTextArea.setBackground(new Color(255, 255, 255));

inputField = new JTextField();

this.add(mainTextArea, BorderLayout.CENTER);
this.add(inputField, BorderLayout.PAGE_END);

Now it looks fine initially, but the problem I'm having with it is that you can move the splitter to the left, but not to the right. In fact, if you move it left, you can no longer move it right. I did have it working earlier, but the implementation wasn't as good and I've forgotten what I had done.

Any ideas? I can't see why this wouldn't work.

+3  A: 

If I remember correctly, JSplitPanes always respect the minimum size of their components. Try explicitly setting the minimum size of the ChatView to (0,0) and see if that helps. If so, you can then look at each of the components inside it to see which is causing the problem.

Michael Myers
I think I just fell in love with you. You star.
Kezzer
Umm... excuse me while I move just a little further away. ;)
Michael Myers
+1  A: 

In any place do you set minimum size for the ChatView? If so, isn't this minimum equals to the frame size ( 500 x 500 ) ?

It is kind of hard to troubleshot with the information provided, but the following advice have ALWAYS worked for me ( and for the people who use it )

  • Create the simplest frame that could possible work and add a SplitPane with two buttons.
  • Resize in there.
  • In everything goes ok, process replacing one of your components.
  • Then the other.
  • Detect which one is causing the strange behavior.
  • On that one, start doing the same, replace it for the minimum component you can think of ( replace it with a JPanel for instance )

BTW, probably it would be better to replace java.awt.Container with javax.swing.JComponent

The idea is to start from the simplest thing that could possible work and evolve from there. Most of the times the problem is in some silly property we set and by cleaning up everything it came up.

It should take less than 1 hr. for this.

OscarRyz
A: 

I'm not sure what is exactly causing your problem, but from vague memory I can tell you that JSplitPane goes bonkers if you don't set preferred sizes and minimum sizes for each of your panes.

I would suggest doing that just to see if it works better.

Here is some stuff from the Java tutorial on dividers.

Uri
A: 

It may be related with minimunSize of the right component. In the following example you can just move the separator to the left.

public static void main(String[] args) {
 JLabel barLabel = new JLabel("bar");
 JLabel fooLabel = new JLabel("foo");
 barLabel.setMinimumSize(new Dimension(150,150));
 fooLabel.setPreferredSize(new Dimension(50,50));



 JSplitPane treeViewChatSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, fooLabel, barLabel);
 treeViewChatSplitPane.setSize(500, 500);
 treeViewChatSplitPane.setBorder(null);
 treeViewChatSplitPane.setDividerSize(3);

 JFrame frame = new JFrame();
 frame.add(treeViewChatSplitPane, BorderLayout.CENTER);

 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(200,200);
 frame.setVisible(true);


}
Kalecser
+1  A: 

If both components have sensible preferred and minimum sizes, and you're still seeing strange behavior, you may need to call setResizeWeight() on the JSplitPane.

Note, BTW, that programatically setting the divider location (setDividerLocation()) pretty much never works.

David Moles