Solved

Get number of columns in a JTextPane?

Posted on 2004-09-03
11
402 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
printf performancy 11 84
Java syntax, or is it Selenium 6 39
Fast way to search item into Java Array (Rhino compatible) 2 38
DTD and JAVA versions 1 29
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

756 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