# [java] How can I set a component size inside of JTextPane.

This topic is 3124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to add one of my own classes derived from JComponent into a JTextPane Object. Whenever I do this, the size of my component changes to 1 pixel in height and it looks like a straight line. Is there any way I can set the size of a component inside of JTextPane?

##### Share on other sites
Why are you trying to add a component directly to a text pane? That would make the component appear on top of some of your text, thereby defeating the purpose of having the text there in the first place. Try it this way instead:

JPanel panel = new JPanel(new BorderLayout());JComponent customComponent = new CustomComponent(...);customComponent.setPreferredSize(new Dimension(...));JTextPane textPane = new JTextPane(...);panel.add(textPane, BorderLayout.CENTER);panel.add(customComponent, BorderLayout.XYZ); // replace XYZ with a valid constant

The above technique would place your custom component next to the text pane, leaving both components fully visible.

##### Share on other sites
Quote:
 Original post by DathgaleWhy are you trying to add a component directly to a text pane?

I am trying to create an Esperanto Tutorial. It is formatted so that there is a header panel at the top where lessons can be selected. There is a submenu panel on the left where the user can select a subsection of the lesson. On the main section there is a JTextPane object in which each lesson part is loaded onto. I want the Text Pane to be static and wide as possible.

##### Share on other sites
In that case I'm sure you don't need to add anything directly to the text pane. Use another panel (or the root component) with the border layout.

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
13
5. 5

• 9
• 13
• 10
• 9
• 17
• ### Forum Statistics

• Total Topics
632601
• Total Posts
3007358

×