Solved

Get number of columns in a JTextPane?

Posted on 2004-09-03
11
407 Views
Last Modified: 2012-06-27
Hi there!
I'm not all that Swing savvy, so bear with me. :)
I have a ControllablePanel containing a JScrollPane containing a JTextPane with a StyledDocument inside. Since the user can resize the window, I'd like to be able to figure out how many columns are being displayed in the JTextPane.
This is most likely pretty simple - any ideas? Or am I not giving enough information? Thanks...
epaga
0
Comment
Question by:epaga
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 11971017
Are you using a fixed width font?  If not, it's variable depending on what characters are in the line...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11971018
X is wider than i
0
 

Author Comment

by:epaga
ID: 11971024
nope, it's fixed width
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 12

Expert Comment

by:Giant2
ID: 11971036
It depends on the size/style of the character setted for Panel, so you can calculate it.
Like the following:
  panelHeight/charHeight
0
 
LVL 6

Expert Comment

by:expertmb
ID: 11971041
hmmmm ... textpane is not having anymethod like setColumns() getColumns() as textfield and textarea have.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11971090
does:

   int cols = (int)( Math.floor( pane.getWidth() / pane.getGraphics().getFontMetrics( pane.getFont() ).charWidth( 'm' ) ) ) ;

give anything like the correct answer?
0
 

Author Comment

by:epaga
ID: 11971151
It would except pane.getGraphics() gives me null - so it's a nullpointerexception.
All I have in my pane is the text, so all the text functions work fine. Am I missing a line somewhere?
But I can tell we're getting close... :)
0
 
LVL 92

Expert Comment

by:objects
ID: 11971207
think you need to make the pane visible before you can get a Graphics context for it.
0
 
LVL 35

Accepted Solution

by:
TimYates earned 350 total points
ID: 11971449
try getGraphics() off another random visible component...

it shouldn't matter which graphics object you use, as it's the fontmetrics you want...

     int cols = (int)( Math.floor( pane.getWidth() / this.getGraphics().getFontMetrics( pane.getFont() ).charWidth( 'm' ) ) ) ;

or something..
0
 

Author Comment

by:epaga
ID: 11971612
that worked like a charm, I just found a different component to use. Thanks Tim!
epaga
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11971639
:-)  Good luck!

Tim
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question