• Post Reply Bookmark Topic Watch Topic
  • New Topic

2 JTextPane with fixed weight inside JScrollPane  RSS feed

 
Petra Cervinkova
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

my goal is make vertical scrollable panel with 2 JTextPane inside it. The first JTextPane with fixed width 55 % of parent panel, than gap with fixed width 5 % and the second JTextPane with fixed width 40 %. Because the both JTextPane(s) have fixed width, they expand with more text only vertically.



My current code:


Should you please advise me what I'm doing wrong? The size of JTextPanes is not fixed.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a horizontal Box with glue between the two text panes?
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm wondering why lines 22, 27, and 32 are not all the same?

Also, I scanned through your code and when I got to the bottom I was surprised to see code putting your panel into a SpringLayout. Why isn't the GridBagLayout all you need?

Anyway if the technique you are trying doesn't work out, you could try using JSplitPane objects instead.
 
Petra Cervinkova
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your's comments . The SpringLayout for parent panel is only for future time, maybe the parent panel will have specific position of main frame.

I try use suggested JSplitPane, so I have this code:

The gap I will simulate by margin for rightTextPane.

With this code, the initial view is OK, but the width is not fixed.


After insert some text:
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Petra Cervinkova wrote:my goal is make vertical scrollable panel with 2 JTextPane inside it. The first JTextPane with fixed width 55 % of parent panel, than gap with fixed width 5 % and the second JTextPane with fixed width 40 %. Because the both JTextPane(s) have fixed width, they expand with more text only vertically.


I must admit that I haven't looked at your code closely. (One reason is that I don't recommend GridBagLayout for anyone who isn't already familiar with that style of layout manager.)

But if I wanted to place two text panes side-by-side in a single scrollable panel, I think I would do something like this.

You could simply call frame.setContentPane(sideBySideTextPane()) if you wanted to try out this code.

edit: I started working on this when there were no replies, but was delayed. Now that I've posted I see that others have recommended JSplitPane too.
 
Petra Cervinkova
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Brian . I like it. I have only few more wishis, please. I do not want to allow the users to use divider and resize these halfs. So I use
, is that OK?

After insert some text, the leftTextPane correctly wrap the lines and expand vertically (the width is fixed to 200). But the rightTextPane expand horizontally and I want vertically expand too. Can you please help me fix it?
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Petra Cervinkova wrote:Thank you, Brian . I like it. I have only few more wishis, please. I do not want to allow the users to use divider and resize these halfs. So I use
, is that OK?

Did you try it? It's ok if it's what you want, but I think you may be confusing DividerSize with DividerLocation. Usually the user can not change DividerSize but can change DividerLocation.

You want to prevent the user from changing the DividerLocation. Unfortunately, JSplitPane does not provide a good way to do that. You can try sp.setEnabled(false) but, depending on the LookAndFeel, that can have bad side effects. You may have to override the JSplitPane's setDividerLocation() method to do what you want. It might look like this:

Petra Cervinkova wrote:After insert some text, the leftTextPane correctly wrap the lines and expand vertically (the width is fixed to 200). But the rightTextPane expand horizontally and I want vertically expand too. Can you please help me fix it?


The code snippet I provided above does not handle horizontal scroll bars very well. What you really want to do is something closer to this:
The problem here is explained in the comment on line 10. To do this correctly (when one pane may have more lines than the other, and the user may add or remove text and change which one is longer) is nontrivial.

Btw, is there a reason you're using JTextPane in favor of JTextArea?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!