Solved

Get number of columns in a JTextPane?

Posted on 2004-09-03
11
385 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
advertisement module in core php 4 159
check java version using powershell 13 86
How do I remove an object from a 3 23
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

776 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