Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get number of columns in a JTextPane?

Posted on 2004-09-03
11
Medium Priority
?
414 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

885 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