Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to java Swing. I am trying to develop a tool, in which I used a Jtextpane as an editor. Jtextpane is enclosed in a Jtabbedpane to meet my requirement. But I have inserted the JTextpane in a Jscrollpane and then to the Jtabbedpane. Now, when I select a particular tab, I should get the corresponding Jtextpane. I have used the

jTabbedPane.getSelectedComponent();

But it returns the Jscrollpane. So, I am unable to save the contents in corresponding Jtextpane...

Can anybody plz help me to resolve this....

Thanks in Advance...
Posted

Can you use the JScrollPane's getComponent(int) method to get the JTextPane? I'd presume that if the JTextPane is the only component in the scroll pane, passing 0 to getComponent(int) would give you the JTextPane.
 
Share this answer
 
Comments
rohit2 12-Jan-11 4:30am    
Dear Marc A. Brown,

I had 3 components in my ScrollPane. As you mentioned, I have used jScrollPane.getComponent(0);, it returns a JViewPort, since I have used setViewPortView(JtextPane);

I used this code snippet and my problem get solved:


JScrollPane jscrollpane =(JScrollPane)jTabbedPane1.getSelectedComponent();
JViewport viewPort = (JViewport) jscrollpane.getComponent(0);
JTextPane jTextPane = (JTextPane)viewPort.getComponent(0);

Thanks a lot.....
Marc A. Brown 12-Jan-11 9:20am    
Glad I could help.
How about first getting the selected component of the tab (which is the scroll panel):
SQL
JScrollPane jscrollpane =(JScrollPane)jTabbedPane.getSelectedComponent();


And then get the selected component of that (which would be the text panel):
SQL
JTextPane jtextpanel=(JTextPane)jscrollpane.getSelectedComponent();


You should do some type checking though to be safe. Also, you could create a recursive method using getComponents to find the desired text panel on the tab or scroll panel.

Good luck!
 
Share this answer
 
Comments
rohit2 12-Jan-11 0:02am    
There is no jscrollpane.getSelectedComponent();
method. Thanks for your support and reply...
Expecting the same...
E.F. Nijboer 12-Jan-11 4:26am    
I see Marc A. Brown helped you out further. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900