Results 1 to 10 of 10
  1. #1
    captain alge is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default Getting text from inside a jeditorpane inside a jtabbedpane

    greetings,

    I have a small program that contains a jTabbedPane. Then, when the user does an action (like click a button) a new jScrollPane is created, and a new jEditorPane is placed inside it. The jScrollPane is then added to the jTabbedPane.

    The jEditorPane then fills itself with information from a different file, depending on which button was clicked.

    All that works fine, but the problem arrises when I want to save the information to the file later. I can't get the text from inside the jEditorPane.

    Here is the method which adds a new tab:
    Java Code:
    public void addTab()
    {
        currentFile = "The File";
        //create new editor pane
        DefaultSyntaxKit.initKit(); //I'm using a custom kit
        JEditorPane editPane = new JEditorPane();
        JScrollPane editScroll = new JScrollPane(editPane);
        editPane.setContentType("text/java");
        editPane.setText("");
        editScroll.setViewportView(editPane);
        editPane.setName("the editor");
        editScroll.setName("the scroll");
        try
        {
            FileReader input = new FileReader(new File("The directory" + currentFile));
            myTabbedPane.addTab(currentFile, editScroll);
            editPane.read(input, null);
            input.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    I can't show you the save code because I really haven't got any. I know I can get the current selected component/tab from the jTabbedPane:
    Java Code:
    myTabbedPane.getSelectedComponent();
    But that has two problems:
    1) It actually returns the scrollPane, not the editor
    2) It returns a component , component has no getText() method

    Any one got any ideas? I'm all out

    cheers!

  2. #2
    captain alge is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    really? no one?

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I'm amazed by your monk-like patience. 1 Hour and 11 Minutes is almost a lifetime to wait.

    No, no its alright. I'll take time out of my busy schedule to help you. Its the least I can do for you your majesty.

  4. #4
    captain alge is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    and that was helpful how?

    Sorry if I do seem impatient, but after three days straight of working on the same problem I think It's probably forgiveable

    And if your schedule is so busy, I fail to understand how you manage to have time to post comments like that

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by captain alge View Post
    But that has two problems:
    1) It actually returns the scrollPane, not the editor
    2) It returns a component , component has no getText() method
    1) True, because that was the component that was added to your JTabbedPane; note that your editor is a child component of that JScrollPane.

    2) casts? A JTabbedPane is only interested in JComponents being added to its tabs, it doesn't care what component it actually is.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Rudeness is hardly forgiveable, if you had the same problem for three straight days why didn't you ask for help sooner?

    Either way JosAH is a much nicer individual than I, so you lucked out.

  7. #7
    captain alge is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    Because I was trying to sort it out myself, normally that's easier than coming on here

    Fortunately you're right, so cheers JosAH :)

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    lesson(hopefully) learned: Don't bump your thread so hastily. People that are currently on may not have a good enough answer for you. Bumping the thread in such a short amount of time only makes people less likely to respond. Wait a few hours, see if anyone posts anything, then perhaps you can bump.

    Who knows when someone with the answer will check the forums? I understand how frustrating it is to be stuck on something and desperate for some help, unfortunately you have to be patient.

  9. #9
    captain alge is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    Right, got it!

    Cheers JosAH for working this one out, here is the full code:

    Code which adds editor to tabbedpane
    Java Code:
    String currentFile = new File("the directory" + "the file");
    //create new syntax pane
    DefaultSyntaxKit.initKit(); //I'm using a custom kit
    JEditorPane editPane = new JEditorPane();
    JScrollPane editScroll = new JScrollPane(editPane);
    editPane.setContentType("text/java");
    editScroll.setViewportView(editPane);
    editPane.setName(currentFile);
    try
    {    
        FileReader input = new FileReader(currentFile);
        myTabbedPane.addTab(currentFile, editScroll);
        editPane.read(input, this);
        input.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    Code which saves to file:
    Java Code:
    JScrollPane scroll= (JScrollPane)myTabbedPane.getComponentAt(index);
    JViewport view = (JViewport)scroll.getViewport();
    JEditorPane editor = (JEditorPane)view.getComponent(0);
    try
    {
        FileWriter fw = new FileWriter(currentFile);
        editor.write(fw);
        fw.close();
    }
    catch (IOException e)
    {
        e.printStackTrace;
    }
    Hope this can help someone else

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by captain alge View Post
    Hope this can help someone else
    Glad you've solved it; I like it when someone comes back here to show the solution to the problem; and, as always, monetary tokens of gratitude should be sent directly to me; ignore that Darryl Burke phisher ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 12-30-2010, 09:12 AM
  2. Rotate Text Inside Brackets
    By corbokhan in forum New To Java
    Replies: 1
    Last Post: 10-16-2010, 01:01 AM
  3. Using regex to retrieve all text inside parentheses
    By adhoc334 in forum Advanced Java
    Replies: 5
    Last Post: 08-18-2010, 08:05 PM
  4. [SOLVED] Adding JTree, JTable inside a JTabbedPane
    By javanewbie in forum AWT / Swing
    Replies: 6
    Last Post: 05-28-2009, 05:32 AM
  5. Adding contents inside the JTabbedPane
    By javanewbie in forum New To Java
    Replies: 1
    Last Post: 05-27-2009, 12:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •