Summary: A simple Java JScrollPane source code example.
When you're first learning how to use Java Swing components, like a JScrollPane it can be a little hard to figure out how to get started. To that end I am trying to share several complete Java source code examples that demonstrate how to work with components like these.
Here is the source code for a complete Java class that demonstrates how to use a JScrollPane
. This example shows how to use a scroll pane with a JTextArea and a JFrame:
import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingUtilities; import javax.swing.text.Document; import javax.swing.text.html.HTMLEditorKit; import javax.swing.text.html.StyleSheet; /** * A complete Java class to demonstrate the use of a JScrollPane. * * @author alvin alexander, devdaily.com. * */ public class JScrollPaneDemo { public static void main(String[] args) { new JScrollPaneDemo(); } public JScrollPaneDemo() { SwingUtilities.invokeLater(new Runnable() { public void run() { // create a jtextarea JTextArea textArea = new JTextArea(); // add text to it; we want to make it scroll textArea.setText("xx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\n"); // create a scrollpane, givin it the textarea as a constructor argument JScrollPane scrollPane = new JScrollPane(textArea); // now add the scrollpane to the jframe's content pane, specifically // placing it in the center of the jframe's borderlayout JFrame frame = new JFrame("JScrollPane Test"); frame.getContentPane().add(scrollPane, BorderLayout.CENTER); // make it easy to close the application frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set the frame size (you'll usually want to call frame.pack()) frame.setSize(new Dimension(240, 180)); // center the frame frame.setLocationRelativeTo(null); // make it visible to the user frame.setVisible(true); } }); } }
Discussion
I've tried to provide plenty of documentation in the code, so I'll add just a few other comments here:
- I use the SwingUtilities
invokeLater
method as a wrapper around most of myJFrame
code. Unfortunately this is the most complicated line of code, and it comes first, but in general you use this technique to make sure you execute Swing code like this on the Event Dispatch Thread, or EDT. As I'll try to show in many examples on this blog, you only want to modify Swing (GUI) elements from the EDT (otherwise bad things will happen). - The
frame.setLocationRelativeTo(null)
is a slightly-special way of centering theJFrame
on screen. It's actually discussed in theJFrame
Javadoc, but when you first start working with Swing andJFrame
's this isn't obvious. - The
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
method sets up your application so that when the user presses the close button on the window they see, your entire application will shut down. This technique is fine for simple applications like this, but with more complicated applications you'll want to control that shutdown process more carefully.
Hopefully this source code makes sense. I've tried to boil it down to just the basics. I find that it's easier to learn a new programming language (especially an environment with GUI components) when you can see complete source code examples, so I'm trying to share more examples like this throughout my blog.