JTextPane with scrollbar
Hi, Iam having an noob problem >_< . I wanna add an JScrollBar to my JTextPane.. But it just wont work.. Ive even tryed to cope the example code given from suns help page.. But it still wont work.. And I dont get it.. Can someone help me out?
This is the code:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
class TextPanel{
private JTabbedPanetabbPane;
privateint tabbLimit = 20;
private JTextPane[] jtp =new JTextPane[tabbLimit];
privateint tabbCounter = 0;
private String tabbNamn ="Unknown";
public TextPanel(){
JFrameframe =new JFrame("TextEditor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
for(int i = 0; i < jtp.length; i++){
jtp[i] =new JTextPane();
JScrollPane paneScrollPane =new JScrollPane(jtp[i]);
paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(paneScrollPane);
}
tabbPane =new JTabbedPane();
tabbPane.addTab(tabbNamn, null, jtp[tabbCounter], tabbNamn);
tabbCounter++;
frame.setLayout(new BorderLayout());
frame.add(tabbPane,"Center");
frame.setVisible(true);
}
}
publicclass TextEditor{
publicstaticvoid main (String[] args){
new TextPanel();
}
}
I think it has something to do with the: getContentPane() metod in JFrame.. But I dont get how its supposed to be implemented in this code. please help.
[2953 byte] By [
Prigasa] at [2007-11-26 18:12:52]

# 1
Why are you creating an array of text panes.
1) Create a single text pane
2) Create a JScrollPane using the text pane as a parameter
3) add the scroll pane to the GUI.
The scrollbars will only appear when you add data to the text pane. So keep hitting the enter key to add blank line to the text pane and you will see the scrollbars eventually appear.
# 2
>1) Create a single text pane
>2) Create a JScrollPane using the text pane as a parameter
>3) add the scroll pane to the GUI.
1. I need more cause the program is actually bigger than this.. But to big to add all unneded here. As you see am I using tabbs, in the bigger version I can add more tabbs through an menu which Ive added..
2. Aint that what Im doing by writing JScrollPane paneScrollPane = new JScrollPane(jtp[i]);
?
3. Please tell me how with code example by how to do that.. Otherwise I would have done it witout asking if its as simple as you make is sounds.. I men what is the GUI? if not the frame.. :S?
# 3
Yes, it has something to do with getContentPane().
In your for-statement you add the scrollPane to the frame's contentPane. And later you add your JTabbedPane to the contentPane, that's why you cannot see the scrollPane and the TextPane. So you should first decide what you want to add to your frame. Is it the JTabbedPane or the JScrollPane?
I guess you want to create a JTabbedPane with JTextPane in a JScrollPanes on every tab. So take a look at this:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
class TextPanel{
private JTabbedPanetabbPane;
private int tabbLimit = 20;
private JTextPane[] jtp = new JTextPane[tabbLimit];
public TextPanel(){
JFrameframe = new JFrame("TextEditor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
tabbPane = new JTabbedPane();
for(int i = 0; i < jtp.length; i++){
jtp[i] = new JTextPane();
JScrollPane paneScrollPane = new JScrollPane(jtp[i]);
paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
tabbPane.addTab("Tab " + i, null, jtp[i], "Tab " + i);
}
frame.setLayout(new BorderLayout());
frame.add(tabbPane, "Center");
frame.setVisible(true);
}
public static void main (String[] args){
new TextPanel();
}
}
# 4
I have overlooked something:
Do not do this:
frame.setLayout(new BorderLayout());
frame.add(tabbPane, "Center");
use
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(tabbPane, "Center");
instead.
# 5
>I guess you want to create a JTabbedPane with JTextPane in a JScrollPanes
>on every tab
That is as true as it is said.. But what you made didnt help :/ you only added all the tabs which aint needed.. In my other version of this I add tabbs when needed through menus..
But your example didnt help with the scrollpane with was the real problem.. Atleast it doesnt seems so when I tryed out the code.
# 6
>frame.getContentPane().setLayout(new BorderLayout());>frame.getContentPane().add(tabbPane, "Center");What exactly does that mean?BRB in 20min tops..
# 7
> frame.getContentPane().setLayout(new BorderLayout());
The default LayoutManager for a frame is a BorderLayout, so that line of code is not required.
> frame.getContentPane().add(tabbPane, "Center");
The default position for adding a component is the center, so the "Center" constraint is not required.
If you do specify the constraint then you should use the defined variable:
BorderLayout.CENTER
# 8
Yes, I have seen it you add the TextPane, not the scrollpane. You should add the scrollPanes.
tabbPane.addTab("Tab " + i, null, paneScrollPane, "Tab " + i);
When I ran your sample code there was nothing to see, no scrollpane not textpane so I thought that was your problem.
Hope this was helpful.....
# 9
> >frame.getContentPane().setLayout(new
> BorderLayout());
> >frame.getContentPane().add(tabbPane, "Center");
>
>
> What exactly does that mean?
>
Yes, camickr, what does that mean? I was wondering why you have to add components to the contentPane of the JFrame. And I found in the API:
As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:
frame.add(child);
And the child will be added to the contentPane.
since when can you do so? I had always in mind to NEVER add some components to a JFrame, but to its contentPane. Am I living in the stone age? I could have saved a lot of getContentPane()-typing in my life, if I knew this before!
# 10
>tabbPane.addTab("Tab " + i, null, paneScrollPane, "Tab " + i);Thank you so much, that is the magical code :/
# 11
i have been observing your conversation and intend to tell my trouble. I am also having a problem of some sort while retriving the JTextPane associated to the tabbedpane with changelistener
ChangeListener changed=new ChangeListener(){
public void stateChanged(ChangeEvent chevent){
JTabbedPane source=(JTabbedPane)chevent.getSource();
int index=source.getSelectedIndex();
JTextPane area=(JTextPane)textPanecontainer.get(source.getTitleAt(index));//problem here
//textPanecontainer is a hashmap where key is tabname and value the associted textpane
area.addMouseListener(pop);
scPan=creatarea(area);//scPan is a scrollpane
frame.setJMenuBar(createMenuBar(area));
frame.add(popup);
frame.setContentPane(createContentPane(area) );
frame.addWindowListener(new AppCloser());
frame.setSize(600, 500);
frame.setVisible(true);
System.out.println("tab changed to"+source.getTitleAt(index));
System.out.println(textPanecontainer.get(source.getTitleAt(index)));
ArrayList keys=new ArrayList(textPanecontainer.keySet());
Iterator i=keys.iterator();
int j=0,k=0;
k=textPanecontainer.size();
System.out.println("size=:"+k);
while(i.hasNext()){
j++;
System.out.println("keys:");
System.out.println(i.next()+" ");
System.out.println("values:");
System.out.println(textPanecontainer.get(i.next()));
System.out.println('\n');
}
System.out.println("no of keys:"+j);
}
};
the problem is that when i retrieve the jtextpane associated with the hashmap textpancontainer i cannot associate other functionalities like actions to the jtextpane.
following is the code for creating the new tabs and how it is associated to the textpane throgh the hashmap textpancontainer.
class NewTabAction extends AbstractAction{
NewTabAction(){
super("NewTab");
}
public void actionPerformed(ActionEvent e) {
count=count+1;
JTextPane area=creatPane();
scPan=creatarea(area);
jt.addTab("tab"+count,scPan);
textPanecontainer.put("tab"+count, area);
}
}
I am running out of ideas . please help
