File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes putting screen output inside scroll bar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "putting screen output inside scroll bar" Watch "putting screen output inside scroll bar" New topic

putting screen output inside scroll bar

Pip Vo

Joined: Jul 18, 2001
Posts: 12
Hi Ranchers,
I need advice on how to place my graphics
output into a scrollable window. Right now,
it's displayed in the content pane of my
JFrame and getting cut off b/c there's no scroll
I got Sun's tutorial demo program:
but it display a JPEG, and I want to display
with my graphics output. Any advice on how to
do this or where to read up on it? Sun's page
doesn't have the answer and neither does my
Core Java book. Thanks!
jacq carballo
Ranch Hand

Joined: Feb 10, 2002
Posts: 42
try putting your graphics in a Canvas. Put the canvas in the JScrollPane. and then add the scroll pane into the contentpane.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Canvas is a heavyweight AWT component and JScrollPane is a lightweight Swing component... a big no no!

You'll have to either extend JPanel to paint your image on it, or just load your image in an ImageIcon and add it to a JLabel.

I'm not sure how you are creating your graphics output... if you were more specific perhaps I could help you more with this...

Are you using the Graphics context to draw onto a Component like JPanel? Are you generating Images?


Write once, run anywhere, because there's nowhere to hide! - /. A.C.
jacq carballo
Ranch Hand

Joined: Feb 10, 2002
Posts: 42
when do you consider a component to be lightweight or heavyweight? (just want to know and learn)
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
light weight and heavy weight are just fancy names given to to components to distinguish between SWING componenets (which are reffred to lightweight) and AWT componenets (which are reffred to as heavy weight).
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Not to make this too complicated, why not simply place your graphic in a JLabel that is in a ScrollPane?

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Pip Vo

Joined: Jul 18, 2001
Posts: 12
Thanks for all the advice. I should've included
my code for generating graphics. Still puzzled on how to proceed. From my book, I think
I should draw on the JPanel and then add it to the
content pane of my JFrame, but not sure *how* to do this. Thanks !
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.StringTokenizer;
// This is like the FontDemo applet in volume 1, except that it
// uses Java 2D APIS to define and render the graphics and text.
public class RectangleV3 extends JApplet
final static int maxCharHeight = 14;
final static int minFontSize = 12;
final static Color bg = Color.yellow;
final static Color fg =;
final static Color red =;
final static Color white = Color.white;
final static BasicStroke stroke = new BasicStroke(2.0f);
final static BasicStroke wideStroke = new BasicStroke(2.0f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);
final static float dash1[] = {10.0f};
final static BasicStroke dashed = new BasicStroke
static double data1[] = new double[6000]; // MAX INDEX NEEDS AUTOMATION.
static String name1[] = new String[6000]; // MAX INDEX NEEDS AUTOMATION.
static String temp1[] = new String[6000]; // MAX INDEX NEEDS AUTOMATION.
static char array1[] = new char[25];
static int h = 0;
static int count = 0;
static int count2 = 0;
static int i = 0;
static int j = 0;
Dimension totalSize;
FontMetrics fontMetrics;
public void init()
// Initialize drawing colors.
public void drawChars(char[] data, int offset, int length, int x, int y)
System.out.println (data);
FontMetrics pickFont(Graphics2D g2, String longString, int xSpace)
boolean fontFits = false;
Font font = g2.getFont();
FontMetrics fontMetrics = g2.getFontMetrics();
int size = font.getSize();
String name = font.getName();
int style = font.getStyle();
while (!fontFits)
if ((fontMetrics.getHeight() <= maxCharHeight)
&&(fontMetrics.stringWidth(longString) <= xSpace))
fontFits = true;
if (size <= minFontSize)
fontFits = true;
g2.setFont(font = new Font(name,style,--size));
fontMetrics = g2.getFontMetrics();
return fontMetrics;
public void paint(Graphics g)
// g.drawChars (array1, 10, 10, 10, 10);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Dimension d = getSize();
int gridWidth = d.width/6;
int gridHeight = d.height/2;
fontMetrics = pickFont(g2, "Filled and Stroked GeneralPath", gridWidth);
Color fg3D = Color.lightGray;
g2.draw3DRect(0, 0, d.width-1, d.height-1, true);
g2.draw3DRect(3, 3, d.width-7, d.height-7, false);
int x = 5;
int y = 7;
int rectWidth = gridWidth-2*x;
int stringY = gridHeight-3-fontMetrics.getDescent();
int rectHeight = stringY-fontMetrics.getMaxAscent()-y-2;
int i = 0;
// int line = 1; // NEEDS AUTOMATION
// int line = 2; // NEEDS AUTOMATION
// int line = 3; // NEEDS AUTOMATION
int line = 1921; // NEEDS AUTOMATION

for (i = 0; i <= line; i++)
// System.out.println("(x,y,w,h): "+data1[0] +" " +data1[1] +" " +100 +" "+data1[2]);
// System.out.println("(x,y,w,h): "+data1[3] +" " +data1[4] +" " +100 +" "+data1[5]);
// System.out.println("(x,y,w,h): "+data1[6] +" " +data1[7] +" " +100 +" "+data1[8]);
// System.out.println("(x,y,w,h): "+data1[9] +" " +data1[10] +" " +100 +" "+data1[11]);
g2.fill(new Rectangle2D.Double(data1[3*i],data1[3*i+1],100,data1[3*i+2]));
g2.drawString((String)name1[2*i], (int)data1[3*i], (int)data1[3*i+1]);
g2.drawString((String)name1[2*i+1], (int)data1[3*i], (int)data1[3*i+1]+8);

System.out.println ("X: " +data1[3*i]);
System.out.println ("Y: " +data1[3*i+1]);
System.out.println ("HEIGHT: " +data1[3*i+2]);
System.out.println ("NAME1: " +name1[2*i]);
System.out.println ("NAME2: " +name1[2*i+1]);

// g2.drawString("FROM TEST DATA", 150, 200);
// g2.draw(new Rectangle2D.Double(0,150,50,50));
// g2.draw(new Rectangle2D.Double(50,200,50,50));
// g2.draw(new Rectangle2D.Double(0,250,50,50));
// g2.draw(new Rectangle2D.Double(50,275,50,50));
// fill and stroke GeneralPath
int x3Points[] = {x, x+rectWidth, x, x+rectWidth};
int y3Points[] = {y, y+rectHeight, y+rectHeight, y};
GeneralPath filledPolygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,x3Points.length);
filledPolygon.moveTo(x3Points[0], y3Points[0]);
for (int index = 1; index < x3Points.length; index++)
filledPolygon.lineTo(x3Points[index], y3Points[index]);
public static void get_data1()
int count1 = 0;
int count2 = 0;
int j = 0;
String line;
BufferedReader br = new BufferedReader(new FileReader("tile1.txt"));

while ((line = br.readLine()) != null)
System.out.println("CURRENT LINE: " +line);
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens())
// System.out.println("J: " +j);
temp1[j] = st.nextToken();
// System.out.println("TEMP: " +temp1[j]);
data1[count1] = Double.parseDouble(temp1[j]);
// System.out.println("DATA: " +data1[count1]);
catch(NumberFormatException e)
name1[count2] = String.valueOf(temp1[j]);
System.out.println("FILL NAME: " +name1[count2]);count2++;
name1[count2] = st.nextToken(); count2++;}
} // end while
} // end try
catch(IOException e) {System.out.println("ERROR");}
public static void main(String s[])
JFrame f = new JFrame("RectangleV3");
// JScrollBar j = new JScrollBar();
// JScrollPane scrollPane = new JScrollPane(f);
// contentPane.add(scrollPane, BorderLayout.CENTER);
f.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e) {System.exit(0);}
JApplet applet = new RectangleV3();
f.getContentPane().add("Center", applet);
f.setSize(new Dimension(800,800));;
get_data1(); // This gets rectangle coordinates.
Pip Vo

Joined: Jul 18, 2001
Posts: 12
Hi Ranchers
I'm still stuck on this. Got the following code
and think it's pretty close to what I need,
but how do I integrate it into my existing JFrame
(if at all?). I've read about scroll panes in
Core Java and know how to add JPEGS and GIFS to the window ... but need to put my own program output inside (please see previous post for code ... sorry it isn't formatted. I need to learn about the UBB(?) code). Thank you!
class scrollpaneimage extends JApplet
Container contentPane = getContentPane();
JLabel jlabel = new JLabel(new ImageIcon("RectangleV3"));
JScrollPane jscrollpane = new JScrollPane(jlabel);
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

To add your applet to a JScrollPane in a JFrame you would do something like this...

Of course, for everything to work out all right, you'll need to implement a getPreferredSize() method on your applet to return the size (as a Dimension object) that it should be... since you are already doing some calculations to do the drawing this shouldn't be too hard...

I agree. Here's the link:
subject: putting screen output inside scroll bar
jQuery in Action, 3rd edition