File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTextPane viewable area Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextPane viewable area" Watch "JTextPane viewable area" New topic

JTextPane viewable area

Joe Crew
Ranch Hand

Joined: Apr 02, 2001
Posts: 49
I asked this question before but I may have made it too complex a question and may have confused some of you so I'll ask it a little differently.
Here is what I would like to do. Lets say I define a JTextPane to be 10 inches wide and 10 inches in height, but I would only like the viewable area to be only 5x5 inches wide at a time. I want to be able to view the entire JtextPane, but only in 5" by 5" viewable window. When I get to the far right side, or bottom of the viewable window, the window would then move with my next arrow key press. Can someone tell me if this is possible. I've tried some things with scrollbars and a JScrollpane with no luck. I have to have the ability to stop the user when they reach their 10 inch margin and once I introduce scroll bars, there is no way to stop them at 10 inches. This problem comes from the fact that I want to be able to define a Jtextpane to be larger than the screen size someone is limited too, but I still want to limit it to 10 inches. Any ideas at all???
Joe Crew
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

You should be able to yourJTextPane.setMaximumSize(new Deminsion(10, 10)) And so when that point is reached, the user cannot scroll any longer. Set your JScrollPane to be the 5 x 5 prefereed size.
I did something similar with a JPanel and a JScrollPane and it worked.
Hope that helps.
Happy Coding,
Gregg Bolinger

GenRocket - Experts at Building Test Data
Joe Crew
Ranch Hand

Joined: Apr 02, 2001
Posts: 49
I will give this a try and let you know how it comes out. I've been trying different combinations of things but just not the right combination, mostly trying to move the viewport around.
Joe Crew
Joe Crew
Ranch Hand

Joined: Apr 02, 2001
Posts: 49
I thought I would post this as a guide to anyone else having the same problem. Not shown below is where I created a BorderLayout and my own personal toolbar for text editing commands which I added to the "North" border. Here is the pseudocode for creating a JTextPane that you can create at a size larger than your PC screen size, have scrollbars, and still limit the width of the JTextPane area. The paradox was once I turn on scrollbars I could no longer limit the size of the JTextPane. Note that in main() not shown here I specified the application to have a setSize of 640x480.
private void setScrollBars() {
// These next few variables were actually declared in another
// part of the overall application, I just added them here
// for the sake of clarity. p1 was used for my toolbar,
// thus p2 is shown here.

JPanel p2 = new JPanel();
JTextPane jpane = new JTextPane();
Dimension myDim = new Dimension();

myDim.setSize(1000, 1000);
jscroll = new JScrollPane(p2);
add(jscroll, "Center" );
The mistake I made previously, which I am sure could still be fixed by someone, was I added the JTextPane to the JScrollbar, then added the JScrollbar to the JPanel. I could not control the actual size of the JTextPane width or height this way. By adding the JtextPane to the Panel and then adding the panel to the JScrollPane, the panel does all of the scrolling and I can specify the exact size of the JTextPane I want. You also don't have to do any monkey business with moving viewports around this way either.
One issue yet to be addressed by this method is even though you can't see the text beyond the area you create for the JTextPane, text still gets entered past the horizontal limit. I'm working on that issue as I type this, but I wanted to at least post the main solution before this original post got too outdated.
Joe Crew
I agree. Here's the link:
subject: JTextPane viewable area
It's not a secret anymore!