Solved

Get number of columns in a JTextPane?

Posted on 2004-09-03
11
361 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now