Reputation: 2311
basically this is what i want to do:
I have two seperate JTextAreas in two seperate JScrollPanes and i want to Combine them, so when the text is to long for the first TextArea, it should be continued on the second one. But it also should work with scrolling: If i scroll the first TextArea, the second should scroll with it (and vice versa). I tried to calculate the maximum possible Lines showable, so that i know how many lines are visible. I found this:
public void paint(Graphics g) {
super.paint(g);
final FontMetrics fontMetrics = g.getFontMetrics();
// get width of any char (font is monospaced)
final int charWidth = fontMetrics.charWidth('M');
final int charHeight = fontMetrics.getHeight();
final int prefWidth = this.getPreferredSize().width;
// get height of Parent (JScrollPane)
final int prefHeight = this.getParent().getHeight();
int maxChars = prefWidth/charWidth;
int maxLines = prefHeight/charHeight;
}
but the getParent().getHeight()
method is not working, because i set the size of the components with my LayoutManager (SpringLayout, and i simply define constraints to set the size of the Components, which i think is the best method to have fixed proportions of the components, even after resizing the Window. Correct me if i'm wrong here, after hours of trying various LayoutManagers (which are all kind of bad and ugly), i dont have any Motivation to think about this any further). So the getHeight()
Method returns 0
all the time because i never set the size.
Are there any other Methods to get the visible Rows of a TextArea nested in a ScrollPane? Or am i completely wrong here and should it some other way?
Thanks for the Help.
Upvotes: 1
Views: 182
Reputation: 324098
Your requirement makes no sense to me:
so when the text is to long for the first TextArea, it should be continued on the second one
Ok, so lets say create each text area to display 10 lines. In the first you enter 20 lines at which time you overflow to the secon and you now have 1 line.
If i scroll the first TextArea, the second should scroll with it (and vice versa).
How do you scroll the text area when it only has 1 line?
Upvotes: 1
Reputation: 23950
I've never done anything like it, but I think the way to look at the problem is to redefine the question.
You actually want 2 views on the same document. View number 1 would show all chars up to some max length. View number 2 would show all the rest.
I think this could be accomplished by creating a custom Document class which share the underlying data (perhaps a master Document so you'd get update events for your document) and have a different offset.
Each of the JTextAreas would have it's own Document instance.
You'd still have to manually jump to the second window when the first is full (though you could of course program your TextArea to do that automatically when you'd get at MAX_LENGTH).
I don't see advantages of connecting the scrollers as they show different data. Why would scrolling to line 100 at area 1 mean that you'd want to scroll to line 1100 at area 2?
And I think you should not touch the paint
method. It's business logic you need, not graphics :)
Upvotes: 1