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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With