3

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.

blank
  • 17,852
  • 20
  • 105
  • 159
Farhan Nasim
  • 773
  • 4
  • 13

1 Answers1

3

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();
gcooney
  • 1,689
  • 10
  • 14