Advertisement



< Prev
Next >



JScrollPane class




JScrollPane provides a scrollable view of a component, where a component maybe an image, table, tree etc. A JScrollPane can be added to a top-level container like JFrame or a component like JPanel. JScrollPane is another lightweight component which extends JComponent class.





Constructors of JScrollPane


Constructor Description
public JScrollPane() Creates an empty JScrollPane with no viewport, where both the horizontal and vertical scrollbars appearing when required.
public JScrollPane(Component view) Creates a JScrollPane that displays a component within it. This JScrollPane also shows the horizontal and vertical bar, only when its component's contents are larger than the viewing area.





Methods of JScrollPane class


Methods Description
public setPreferredSize(Dimension d) Sets the preferred size of JScrollPane.
public int setLayout(LayoytManager managaer) Sets the layout manager for JScrollPane.





An example of JScrollPane


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

public class ScrollPane1
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A //implements ActionListener
{

JFrame jf;
JPanel jp; 
JLabel label1;

A()
{
jf = new JFrame("JScrollPane");
label1 = new JLabel("Displaying a picture ",JLabel.CENTER);
	
//Creating an ImageIcon object to create a JLabel with image
ImageIcon image = new ImageIcon("nature.jpg");
JLabel label = new JLabel(image, JLabel.CENTER);

//Creating a JPanel and adding JLabel that contains the image
jp = new JPanel(new BorderLayout());
jp.add( label, BorderLayout.CENTER );

//Adding JPanel to JScrollPane
JScrollPane scrollP = new JScrollPane(jp);

//Adding JLabel and JScrollPane to JFrame
jf.add(label1,BorderLayout.NORTH);
jf.add(scrollP,BorderLayout.CENTER);


jf.setSize(350,270);
jf.setVisible(true);
}

}

When you run the code, you are presented a window that shows an image with a horizontal and a vertical scroll bar.

Figure 1



Advertisement




Displaying two images with their individuals JScrollPane.


In the next code, we are going to display two images within a JFrame and each of these image has a JScrollPane attached to it.

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

public class ScrollPane3
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A 
{
Object [] index;
JFrame jf;
JPanel jp1, jp2;
JLabel label;

A()
{

 
jf = new JFrame("JScrollPane");
jp1 = new JPanel();

label = new JLabel("Displaying two images ",JLabel.CENTER);
	


ImageIcon image1 = new ImageIcon("nature.jpg");
JLabel label1= new JLabel(image1, JLabel.CENTER);
jp1 = new JPanel(new BorderLayout());
jp1.add( label1, BorderLayout.CENTER );
JScrollPane scrollP1 = new JScrollPane(jp1);
scrollP1.setPreferredSize(new Dimension(300, 190));

ImageIcon image2 = new ImageIcon("nature2.jpg");
JLabel label2 = new JLabel(image2, JLabel.CENTER);
jp2 = new JPanel(new BorderLayout());
jp2.add( label2, BorderLayout.CENTER);



JScrollPane scrollP2 = new JScrollPane(jp2);
scrollP2.setPreferredSize(new Dimension(300, 190));

jf.add(label,BorderLayout.NORTH);
jf.add(scrollP1,BorderLayout.CENTER);
jf.add(scrollP2,BorderLayout.EAST);


jf.setSize(600,270);
jf.setVisible(true);
}

}
When you run the code, you are presented a window that shows two images, with their own JScrollPane giving them horizontal and vertical bars.

Figure 2





JScrollPane with JTable


import javax.swing.*;
import java.awt.*;
import javax.swing.table.TableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.DefaultTableCellRenderer;


public class ScrollPane5
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A 
{
String [] index;
JFrame jf;
DefaultTableModel dTableModel;
JTable table1; 
JLabel label1;
JPanel jp;

A()
{
index= new String[]{"Rank", "Country", "Forested Area(km sq)", "% of land area"};

jf= new JFrame("JScrollPane with JTable");
label1 = new JLabel("Top 10 countries with most forested area",JLabel.CENTER);

Object[][] rawData = new Object[] [] {
		{"1", "Russia", "8,149,300", "49.40%"},
		{"2", "Canada", "4.196,438", "49.24%"},
		{"3", "Brazil", "4,777,980", "56.10%"},
		{"4", "United States", "2,083,210", "33%"},
		{"5", "China", "2,083,210", "21.83"},
		{"6", "Congo", "1,819,326", "50%"},
		{"7", "Australia", "1,147,832", "19.90%"},
		{"8", "Argentina", "945,336", "34%"},
		{"9", "Indonesia", "884,950", "46.46%"},
		{"10", "India", "778,424", "24.68%"},
		};

//creating a DeFaultTableModel object, which is subclass of TableModel
dTableModel = new DefaultTableModel(rawData, index);

//Initializing a JTable from DefaultTableModel.
table1 = new JTable(dTableModel);


//Adjusting the contents of each cell of JTable in CENTER
DefaultTableCellRenderer tableRenderer = new DefaultTableCellRenderer();
tableRenderer.setHorizontalAlignment(JLabel.CENTER); //Aligning the table data centrally.
table1.setDefaultRenderer(Object.class, tableRenderer);

//Creating a JPanel, setting it layout to BorderLayout and adding JTable to it.
jp= new JPanel(new BorderLayout());
jp.add(table1, BorderLayout.CENTER);

//Creating a JScrollPane and adding its functionalities to JPanel
JScrollPane scrollP = new JScrollPane(jp);

//Adding a JLabel and JScrollPane to JFrame.
jf.add(label1, BorderLayout.NORTH);
jf.add(scrollP,BorderLayout.CENTER);

jf.setSize(320,200);
jf.setVisible(true);
}
}
When you run the code, you are presented a window shows us a table displaying the top 10 countries with the maximum forested area. We have achieved it by creating a JTable with a JScrollPane attached to it, giving it the horizontal and vertical bars(when required).

Figure 3




Please share this article -





< Prev
Next >
< JTree
JScrollBar >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement