JTabbedPane and JScrollPane problem
I am trying to create a plain text editor that opens each new file in a new JTabbedPane tab. The contents of the file are displayed using an object that extends JTextArea. If I add my object to a JTabbedPane I can easily get the text using the getComponentAt(int) method. However, if I add the object to a JScrollPane and then add the JScrollPane to the JTabbedPane, I can't get the text from my object. Make sense?
JTabbedPane tabs = new JTabbedPane();
JTextFileAreamyarea= new JTextFileArea(); //my extension of JTextArea
JScrollPane sp = new JScrollPane(myarea);
tabs.addTab("test",sp);
The above pseudo code doesn't allow me to get text frommyareabecause it returns the JScrollPane as the selected component. If I remove the JScrollPane part, everything work fine - but I can't scroll my text file.
All of the solutions I've read in the forum don't include the part with the JScrollPane.
Thanks in advance.
[983 byte] By [
Jstatsa] at [2007-11-26 16:29:20]

> Seems like it works well. Have You tried to ask
> JScrollPane where is Your text? ;)
> May I see how You access text in those both cases?
I thought I was asking the JScrollPane very nicely, but perhaps
there's a method that might work better. Is there a getText() method
for JScrollPane? For more details my code is below.
This first class extends JTextArea. It also has methods for opening files
and displaying them. The save file functions are the ones
I'm trying to get acess to from the JTabbedPane.
public class TextFileArea extends JTextArea{
/**
*
*/
private static final long serialVersionUID = 1;
String fileName = "";
int fontSize = 12;
Font defaultFont = new Font("Courier",Font.PLAIN, fontSize);
BufferedReader bRead = null;
BufferedWriter bWrite = null;
boolean textChanged = false;
protected File f = null;
Document doc;
/*
* aFileName - a String indicating the complete path and file name
*/
public TextFileArea(String aFileName){
setPreferredSize(new Dimension(730,550));
setBorder(new EmptyBorder(5,5,5,5));
setBackground(Color.WHITE);
setFont(defaultFont);
getDocument().addDocumentListener(new UpdateListener());
fileName = aFileName;
openFile(fileName);
}
private void openFile(String aFileName){
String s="";
f = new File(aFileName);
if(f == null || !f.isFile() || !f.exists()) return;
try{
setText("");
bRead = new BufferedReader(new FileReader(f));
while((s = bRead.readLine()) != null){
append(s);
append("\n");
}
bRead.close();
}catch(IOException ex){
//throw statisticsException with stack trace
}
}
public void setFile(String aFileName){
f = new File(aFileName);
}
/*
* Method for saving the existing file.
*/
public void fileSave(){
try{
bWrite=new BufferedWriter(new FileWriter(f));
bWrite.write(getText());
bWrite.close();
}catch(IOException ex){
//throw statisticsException with stack trace
}
textChanged=false;
}
/*
* Method for saving file with a new name.
*
* aNewFile - a File that is based on the new name.
*/
public void fileSaveAs(File aNewFile){
try{
f.createNewFile();
bWrite=new BufferedWriter(new FileWriter(f));
bWrite.write(getText());
bWrite.close();
}catch(IOException ex){
//throw statisticsException with stack trace
}
textChanged=false;
}
public Document getTextFileDocument(){
doc = getDocument();
return doc;
}
public boolean textChanged(){
return textChanged;
}
/*
* Method for adding a String object to the TextFile. User's will
* be able to edit the JTextArea directly. This method is for, say,
* adding the contents of one file or the output of a method
* to this file.
*
* aStringToAdd - String to be added to TextFile
*/
public void addText(String aStringToAdd){
append(aStringToAdd);
textChanged = true;
}
/*
* Method for adding a Formatter object to the TextFile. User's will
* be able to edit the JTextArea directly. This method is for, say,
* adding the contents of one file or the output of a method
* to this file.
*
* aFormatterToAdd - a Formatter object to be added to TextFile
*/
public void addText(Formatter aFormatterToAdd){
append(aFormatterToAdd.toString());
textChanged = true;
}
/*
* Returns the file name if teh file has been created.
* Otherwise a blank file name is returned.
*/
public String getFileName(){
if(f!=null){
return f.getName();
}
return "";
}
}
This class creates a panel that I can Add to a JTabbedPane.
When I add this panel directly I have no problems. When I add
this panel to a JScrollPane first, I can't access the TextFileArea object
public class JTextPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1;
TextFileArea tfa;
JScrollPane sp;
public JTextPanel(String aFileName){
tfa = new TextFileArea(aFileName);
sp =new JScrollPane(tfa);
add(sp,BorderLayout.CENTER);
}
public TextFileArea getTextFileArea(){
return tfa;
}
}
Now the class that does the work include this part:
textPanel = new JTextPanel(f.getAbsolutePath());
...
tabs.addTab(textPanel.getTextFileArea().getFileName(), textPanel);
...
Finally, the following line tells me that the component is a JScrollPane.
System.out.println((tabs.getSelectedComponent()).toString());