Create a Scrollable Label in Java
Description
The following code shows how to create a Scrollable Label.
Example
// ww w . ja v a2 s. c o m
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.Scrollable;
public class Main {
public static void main(String[] args) {
JFrame f = new JFrame("JScrollPane Demo");
ImageIcon ii = new ImageIcon("a.gif");
JScrollPane jsp = new JScrollPane(new ScrollableLabel(ii));
f.getContentPane().add(jsp);
f.setSize(300, 250);
f.setVisible(true);
}
}
class ScrollableLabel extends JLabel implements Scrollable {
public ScrollableLabel(ImageIcon i) {
super(i);
}
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
public int getScrollableBlockIncrement(Rectangle r, int orietation,
int direction) {
return 10;
}
public boolean getScrollableTracksViewportHeight() {
return false;
}
public boolean getScrollableTracksViewportWidth() {
return false;
}
public int getScrollableUnitIncrement(Rectangle r, int orientation,
int direction) {
return 10;
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »