can anyone tell me the code how to create a textpane with scrollbars inside a tabbed pane.....and i should save the text in a file on button click....

Recommended Answers

All 22 Replies

You need to use a JTextArea for this, you can specify the no. of rows and columns for the JTextArea by using the methods setRows() and setColumns() . When you set the JTextArea's rows and columns to more than the width and height assigned for the JTextArea horizontal and vertical scroll bars appear automatically when the text in the area runs beyond it's width (or height).

To save the text within the JTextArea into a file on the click of a button, you would need to write the code that is invoked on the button press, which writes the content of the text area to a file.
The content of the text area can be received by using the getText() method.

The button press can be detected by adding an ActionListener to the button.

I hope this helps, here are the Java docs for the JTextArea.

thank you very much it helped me....
if there r more than 1 tab for ex if there are 3 tabs then i need to get the selected tab title and the name of the textarea to write into a file...please tell me how to do this...

For this you would have to implement the ChangeListener Interface's stateChanged() method, which would be invoked whenever the tabbed panes state has been changed, which includes the changing of the active tab from one to another.
When this happens, you can use the getAccessibleContext() method to get the name of the currently active tab.

Check the JTabbedPane & ChangeListener docs for more.

i need to add tabs dynamically to the tabbedpane every tab will have jtextarea as component i tried this but everytime i click the button the first tab alone is changed..

tabbedpane=new JTabbedPane();
tabbedpane.addtab("new tab",null,textArea,null);

this is inside a button events....
and setting the rows and cols of the textArea does not have scrollbars how to create scrollbars for textArea...

Setting the rows and cloumns alone will not get the scroll bars for you, if you read my earlier post carefully, I have mentioned there that setting the Rows and Columns for the JTextArea will set the total size of the JTextArea, where Rows will specify how many lines of text the JTextArea will allow and Columns would specify the number of characters per line.
Now when your text runs beyond the viewable are of the JTextArea and then scroll bars would appear on their own for you to view that text.
Try this and see.

i need to add tabs dynamically to the tabbedpane every tab will have jtextarea as component i tried this but everytime i click the button the first tab alone is changed..

tabbedpane=new JTabbedPane();
tabbedpane.addtab("new tab",null,textArea,null);

That is because every time you press the button a new JTabbedPane is created replacing the old. You don't need to create a new JTabbedPane only adding.
So declare the JTabbedPane as class attribute outside the methods.
when the button is clicked you will only add to the existing one

i have the tabbedpane creating at out of the method the reason why its replaced is that everytime the same textarea instance is created so what i did is....

tabbedPane.addTab("tab",new ScrollPane(new JTextArea()),null);

i got multiple tabs and scrollbars but now the problem is i have to write the contents of textArea to a file on button click how do i get the textarea for the selected tab......

and hw to get the focussed or opened tabs(visible tab) title.....
can u please explain the getAccessibleContext() method ....i googled but i dint understand its use......

Rather then having tabbedPane.addTab("tab",new ScrollPane(new JTextArea()),null); you should go as

JTextArea jta = new JTextArea();
JScrollPane jsp = new JScrollPane(jta);
tabbedPane.addTab("TAB_NAME", jsp);

in doing so you can either call jta directly if from same class or provide appropriate setter and getter methods for access from outside class.

I'm not sure if there is way to get names of all visible tabs, but to get name of currently selected is very simple tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());

Java Syntax (Toggle Plain Text) 
JTextArea jta = new JTextArea();
JScrollPane jsp = new JScrollPane(jta);
tabbedPane.addTab("TAB_NAME", jsp);

it can be done but im creating a new tab dynamically....if these lines of code are included in the button event on first time it creates a new tab but for the next time it doesnot create new tab it replaces the first tab....
any ideas......

Problem is we do not have full image. You are just feeding us with bits of information and we try to help you to our best knowledge. If you give us whole image that will make difference...

what im trying to do is an ide for c,c++,java when i click file->new->c file a new tab must be created with text area component and on clicking save the contents of textarea must be written to a file..i used this code to create new tabs on clicking menu...

tabbedPane.addTab(filename,new ScrollPane(new JTextArea()),null);

but when i click save the contents must be written to a file if there are more tabs how can i get the contents of textarea bcoz there is no variables for the textarea....
any ideas....

Well, you could find it by walking down the container hierarchy from the selected tab with instanceof checks, but I'd consider that an awful hack. You're better off writing an EditorPane class that encapsulates the components of each editor pane. Each tab would contain a single instance of EditorPane and have the necessary references to obtain the text.

sorry i couldnt understand can u explain with examples...

Okay, here is a bare-bones example. I've included the EditorPanel here as an inner class just for convenience sake. You would actually want that to be a separate top-level class.
(For future reference, if you want people to take the time to help you out, the least you can do is expend the effort to type complete words. There is no "u" here. Punctuation wouldn't hurt either.)

import java.awt.BorderLayout;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.text.Document;


public class TabbedEditorExample extends javax.swing.JFrame {

    public TabbedEditorExample() {
        initComponents();
        
        // Add a couple of editor panels for example purposes
        addEditorPanel("C++");
        addEditorPanel("Java");
        
        setBounds(100, 100, 300, 300);
    }

    /** Add a new editor tab */
    protected void addEditorPanel(String name){
        tabbedPane.add(name, new EditorPanel(name));
    }

    /** Example of getting the currently selected editor */
    private EditorPanel getSelectedEditorPane(){
        return (EditorPanel)tabbedPane.getSelectedComponent();
    }

    /** Example of getting the currently selected document. */
    private Document getSelectedDocument(){
        if (tabbedPane.getComponentCount()>0){
            return getSelectedEditorPane().getDocument();
        } else {
            return null;
        }
    }

    /** This class just encapsulates the component details of the editor panel */
    class EditorPanel extends JPanel {
        private String name;
        private JEditorPane editorPane;

        public EditorPanel(String name){
            super();
            this.name = name;
            editorPane = new JEditorPane();
            JScrollPane scrollPane = new JScrollPane(editorPane);
            setLayout(new BorderLayout());
            add(scrollPane, BorderLayout.CENTER);
        }

        /** Provides access to the text document. */
        public Document getDocument(){
            return editorPane.getDocument();
        }

        public String getName(){
            return name;
        }
    }

    private void initComponents() {
        tabbedPane = new javax.swing.JTabbedPane();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().add(tabbedPane, java.awt.BorderLayout.CENTER);

        pack();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TabbedEditorExample().setVisible(true);
            }
        });
    }

    private javax.swing.JTabbedPane tabbedPane;

}

i need to set the text whn a tab is created.... need to get the text from file and set it to the current opened tab hw to do that...

What have you done in your code so far? I gave you a model to start with above. Did you read it? Did you understand it?

I am not going to write examples of how to do every single thing you request - especially when you are too lazy to even type all of the letters in a sentence.

no its not like that i have tried but i couldnt set the text i can get the text from file but setting this text is the problem...

Code, code, code is what we need. Reading from magic ball is out of date....

i dont know much about swings thats why i find it difficult...
till now u have helped me a lot thank u but this i couldnt find out how thats why i seeks expert help

You said you want to set text. However you did not said where do you get the text from, is it retrieved correctly and where you want to set it (obviously to text area, but did you try it?)
For these we need some code samples.

PS: To avoid any unnecessary haste have look at JTextArea.append(String) method and let us know if you got it working. In case it does not, please provide your latest code

no i need to set text to editorpanel ... this is for opening a file....
when a new tab is created using editorpanel i need to get the contents from file and must set that text to that editorpanel..

i found out now what i did is i used another constructor in editorpanel class whis gets the name and the text to be inserted in the main program i get the contents from file save it as a single line with newline characters placed inside the lines ....then new tab is created and the name and this text is send to the class constructor there using setText() method i set the text to editorpane....

Thanks a lot it helped me very much thank u.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.