Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTabbedPane component accessing

I use JTabbedPane in one of my Java GUI code. I use the following portion of code to instantiate and maintain the tabpane.

JTabbedPane tabpane =  new JTabbedPane();  
PageViewer pv = new PageViewer();  
tabpane.addTab("tabttitle", new JScrollPane(pv));

PageViewer is an extended class of JEditorPane. I want to access and modify the currently selected tab's constituent PageViewer pv component. I tried the following lines of code with some values of ind.

JScrollPane jsp = (JScrollPane) tabpane.getComponentAt(tabpane.getSelectedIndex());  
PageViewer pv2 = (PageViewer) jsp.getComponent(ind);

But for ind==0 compiler says "java.lang.ClassCastException: javax.swing.JViewport cannot be cast to menu_window.PageViewer".

For ind==1 it says "java.lang.ClassCastException: javax.swing.JScrollPane$ScrollBar cannot be cast to menu_window.PageViewer".

For ind==2 output is "java.lang.ClassCastException: javax.swing.JScrollPane$ScrollBar cannot be cast to menu_window.PageViewer".

And for ind>=3 error is "java.lang.ArrayIndexOutOfBoundsException: No such child: 3".

Now how do I do the desired job, if anyone knows please help.

Note: I use NetBeans 6.8 with Java 6 Standard Edition.

like image 513
f.nasim Avatar asked Dec 08 '25 12:12

f.nasim


1 Answers

When you create a JScrollPane around a component, the scrollpane actually adds the component into an internal JViewPort. To get the original component from the scrollpane you can do this:

PageViewer pv2 = (PageViewer)jsp.getViewport().getView();
like image 198
gcooney Avatar answered Dec 11 '25 01:12

gcooney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!