Cast a JScrollPane

339 Views Asked by At

I want to know, how can I cast a: JScrollPane inside a JTextArea.

Basically, is a text area, with a: scroll pane, inside of this.

Here I put some of my code:

JTextArea txtarea = new JTextArea(); 
JScrollPane scroll = new JScrollPane(txtarea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

JTabbedPane tabbed = new JTabbedPane();
tabbed.addTab("Text", null, scroll, "Text");     


savebutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent A){
txtarea = (JtextArea) tabbed.getSelecetedComponent();
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(null);}});

With the last code, I create the: text area, and added a: scroll pane. All is fine here, but I made a JTabbedPane, where I add the text area, but how I put the scroll pane, I need to put the: scroll pane, in the tab...

How I said, all is fine here, but in the console I get a exception which says,

JScrollPane cannot be cast to javax.swing.JTextArea...

Can you help me, or give an idea how to fix this?

1

There are 1 best solutions below

2
On BEST ANSWER

Hope this will help:

txtarea = (JTextArea) ((JScrollPane)tabbed.getSelectedComponent()).getViewport().getView();