29 May 2011

JScrollPane Tutorial



JScrollPane Tutorial
Hello Friends,

Nice to see you again.
In Previous Sessions  we have seen lots of  Java Swing Components Demo. And learn how they work.
Today, we will see the example of JScrollPane component of Java Swing.

Threre are some situation where we can not adjust the space of our JFrame or JPanel to hold the
items in it. Because the items are too many to adjust in a small JFrame.
In this type of situation, JScrollPane is there to help you.

It is very easy to use JScrollPane.
We only have to create object of JScrollPane and in its constructor pass the component which you want to put in JScrollPane. And thats all. Now that component will shown in JScrollPane. Now no-matter how
big that component in size, JScrollPane will take care of it.

So be ready to learn this simple exmaple of JScrollPane.
After apply this code in action, you will be capable of using  JScrollPane in your Swing Applications.

Output will be like this:








OK For this you have to create one class called JScrollPaneDemo.java and paste the below code in it.
And run it....Simple right..?






import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;

public class JScrollPaneDemo extends JFrame {

public JScrollPaneDemo() {

super("JScrollPane Demo");

ImageIcon icon=new ImageIcon("Sunset.jpg");
JLabel label = new JLabel(icon);
JScrollPane scrollPane = new JScrollPane(label);

getContentPane().add(scrollPane);
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new JScrollPaneDemo();
}
}
--------------------------------------------------------------------

So I hope this code is helpful in understanding the working of JScrollPane.


Feel free to ask me if you have any doughts regarding this.
You can also give me suggessions to improve this tutorial.


Leave comments if you like this post...!


Thank you.
Nirav Raval


----------------------------------------------------------------------------------
Follow Other Java Swing Tricks....

No comments:

Post a Comment

Leave Comment If You Like This...Thank You..!