Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

JTextPane Visualization Problem!

I am trying to display a JtextPane embedded into a JScrollPane in a JFrame. But I can only see the JScrollPane (not the JTextPane) in the JFrame. Here is my code:

public class JTextPaneTest extends JFrame {

public static void main(String args[]) {
try {
JTextPaneTest frame = new JTextPaneTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

public JTextPaneTest() {
super();
getContentPane().setBackground(new Color(241, 243, 248));
//setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
//this.setPreferredSize(new Dimension(1020,730));
this.setSize(536, 300);
this.setLocationRelativeTo(null);

final JTextPane textPane_1 = new JTextPane();
textPane_1.setText("New JTextPane");

final JScrollPane statisticsScrollPane = new JScrollPane(textPane_1);
statisticsScrollPane.setBorder(new TitledBorder(new EtchedBorder(), "Statistics", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, new Color(0, 128, 255)));
statisticsScrollPane.setLayout(null);
statisticsScrollPane.setBounds(6, 14, 486, 140);
getContentPane().add(statisticsScrollPane);

}
}

Just wondering, what am I doing wrong? Please help.
[1407 byte] By [sabbir] at [2007-11-11 7:05:40]
# 1 Re: JTextPane Visualization Problem!
You must add the component to the scrollpane's viewport. I have
removed some unnecessary final declarations also, and used a GridLayout
for the frame's contentpane.

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

public class JTextPaneTest extends JFrame {

JTextPane textPane_1 = new JTextPane();
JScrollPane statisticsScrollPane = new JScrollPane();

public static void main(String args[]) {
try {
JTextPaneTest frame = new JTextPaneTest();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}

public JTextPaneTest() {
getContentPane().setLayout(new GridLayout());
getContentPane().setBackground(new Color(241, 243, 248));
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

textPane_1.setText("New JTextPane");

statisticsScrollPane.getViewport().add(textPane_1,null);

statisticsScrollPane.setBorder(new TitledBorder(new EtchedBorder(),
"Statistics", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, new Color(0, 128, 255)));

statisticsScrollPane.setBounds(6, 14, 486, 140);
getContentPane().add(statisticsScrollPane);

}
}
sjalle at 2007-11-11 22:39:19 >