Results 1 to 10 of 10
- 04-12-2011, 11:10 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 25
- Rep Power
- 0
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:
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: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(); } }
But that has two problems:Java Code:myTabbedPane.getSelectedComponent();
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!
- 04-12-2011, 12:21 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 25
- Rep Power
- 0
really? no one?
- 04-12-2011, 12:29 PM #3
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.
- 04-12-2011, 12:34 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 25
- Rep Power
- 0
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
- 04-12-2011, 12:35 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-12-2011, 12:41 PM #6
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.
- 04-12-2011, 12:43 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 25
- Rep Power
- 0
Because I was trying to sort it out myself, normally that's easier than coming on here
Fortunately you're right, so cheers JosAH :)
- 04-12-2011, 01:02 PM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-12-2011, 07:01 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 25
- Rep Power
- 0
Right, got it!
Cheers JosAH for working this one out, here is the full code:
Code which adds editor to tabbedpane
Code which saves to file: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(); }
Hope this can help someone elseJava 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; }
- 04-12-2011, 07:26 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Manipulate Text Properties within Specified Rectangular Inside PDF
By sherazam in forum Java SoftwareReplies: 0Last Post: 12-30-2010, 09:12 AM -
Rotate Text Inside Brackets
By corbokhan in forum New To JavaReplies: 1Last Post: 10-16-2010, 01:01 AM -
Using regex to retrieve all text inside parentheses
By adhoc334 in forum Advanced JavaReplies: 5Last Post: 08-18-2010, 08:05 PM -
[SOLVED] Adding JTree, JTable inside a JTabbedPane
By javanewbie in forum AWT / SwingReplies: 6Last Post: 05-28-2009, 05:32 AM -
Adding contents inside the JTabbedPane
By javanewbie in forum New To JavaReplies: 1Last Post: 05-27-2009, 12:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks