Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't print empty lables

Status
Not open for further replies.

gera

Programmer
Jul 28, 1999
2
IL
As we try print a table content in Java, using the printable interface we get an exception on empty JLabel components. The print function seems to check if a label has no text in it and raise an exception. The exception stops the printing.<br>
<br>
Unfortunately we need to print empty labels. Could someone suggest an easy way to walk around this problem. We can remove all empty labels from the table but this is problematic since we use empty labels in many situations and we prefer to keep the code untouched. Maybe someone know of a fix to this bug.
 
I think, there is an usable walk around, and it may will help you:<br>
Try to use a space character in JLabel text rather than an empty text.<br>
Good luck. Bye, Otto.<br>

 
A better solution is to use the follwing class:<br>
<br>
import java.awt.*;<br>
import java.io.*;<br>
import java.lang.*;<br>
import java.util.*;<br>
import java.awt.image.ImageObserver;<br>
import java.text.AttributedCharacterIterator;<br>
<br>
public class GraphicsShell extends Graphics {<br>
<br>
Graphics g;<br>
<br>
public GraphicsShell(Graphics g) {<br>
this.g = g;<br>
}<br>
<br>
public Graphics create() {<br>
return new GraphicsShell(g.create());<br>
}<br>
<br>
public Graphics create(int x, int y, int width, int height) {<br>
return new GraphicsShell(g.create(x,y,width,height));<br>
}<br>
<br>
public void translate(int x, int y) {<br>
g.translate(x,y);<br>
}<br>
<br>
public Color getColor() {<br>
return g.getColor();<br>
}<br>
<br>
public void setColor(Color c) {<br>
g.setColor(c);<br>
}<br>
<br>
public void setPaintMode() {<br>
g.setPaintMode();<br>
}<br>
<br>
public void setXORMode(Color c1) {<br>
g.setXORMode(c1);<br>
}<br>
<br>
public Font getFont() {<br>
return g.getFont();<br>
}<br>
<br>
public void setFont(Font font) {<br>
g.setFont(font);<br>
}<br>
<br>
public FontMetrics getFontMetrics() {<br>
return g.getFontMetrics();<br>
}<br>
<br>
public FontMetrics getFontMetrics(Font f) {<br>
return g.getFontMetrics(f);<br>
}<br>
<br>
public Rectangle getClipBounds() {<br>
return g.getClipBounds();<br>
}<br>
<br>
public void clipRect(int x, int y, int width, int height) {<br>
g.clipRect(x, y, width,height);<br>
}<br>
<br>
public void setClip(int x, int y, int width, int height) {<br>
g.setClip(x, y, width, height);<br>
}<br>
<br>
public Shape getClip() {<br>
return g.getClip();<br>
}<br>
<br>
public void setClip(Shape clip) {<br>
g.setClip(clip);<br>
}<br>
<br>
public void copyArea(int x, int y, int width, int height,int dx, int dy) {<br>
g.copyArea(x, y, width, height,dx, dy);<br>
}<br>
<br>
public void drawLine(int x1, int y1, int x2, int y2) {<br>
g.drawLine(x1, y1, x2, y2);<br>
}<br>
<br>
public void fillRect(int x, int y, int width, int height) {<br>
g.fillRect(x, y, width, height);<br>
}<br>
<br>
public void drawRect(int x, int y, int width, int height) {<br>
g.drawRect(x, y, width, height);<br>
}<br>
<br>
public void clearRect(int x, int y, int width, int height) {<br>
g.clearRect(x, y, width, height);<br>
}<br>
<br>
public void drawRoundRect(int x, int y, int width, int height,int arcWidth, int arcHeight) {<br>
g.drawRoundRect(x, y, width, height,arcWidth, arcHeight);<br>
}<br>
<br>
public void fillRoundRect(int x, int y, int width, int height,int arcWidth, int arcHeight) {<br>
g.fillRoundRect(x, y, width, height,arcWidth, arcHeight);<br>
}<br>
<br>
public void draw3DRect(int x, int y, int width, int height,boolean raised) {<br>
g.draw3DRect(x, y, width, height,raised);<br>
}<br>
<br>
public void fill3DRect(int x, int y, int width, int height, boolean raised) {<br>
g.fill3DRect(x, y, width, height, raised);<br>
}<br>
<br>
public void drawOval(int x, int y, int width, int height) {<br>
g.drawOval(x, y, width, height);<br>
}<br>
<br>
public void fillOval(int x, int y, int width, int height) {<br>
g.fillOval(x, y, width, height);<br>
}<br>
<br>
public void drawArc(int x, int y, int width, int height,int startAngle, int arcAngle) {<br>
g.drawArc(x, y, width, height,startAngle, arcAngle);<br>
}<br>
<br>
public void fillArc(int x, int y, int width, int height,int startAngle, int arcAngle) {<br>
g.fillArc(x, y, width, height,startAngle, arcAngle);<br>
}<br>
<br>
public void drawPolyline(int xPoints[], int yPoints[],int nPoints) {<br>
g.drawPolyline(xPoints, yPoints,nPoints);<br>
}<br>
<br>
public void drawPolygon(int xPoints[], int yPoints[],int nPoints) {<br>
g.drawPolygon(xPoints, yPoints,nPoints);<br>
}<br>
<br>
public void drawPolygon(Polygon p) {<br>
g.drawPolygon(p);<br>
}<br>
<br>
public void fillPolygon(int xPoints[], int yPoints[],int nPoints) {<br>
g.fillPolygon(xPoints, yPoints,nPoints);<br>
}<br>
<br>
public void fillPolygon(Polygon p) {<br>
g.fillPolygon(p);<br>
}<br>
<br>
public void drawString(String str, int x, int y) {<br>
if( (str != null) && (str.length() != 0) )<br>
g.drawString(str, x, y);<br>
}<br>
<br>
public void drawString(AttributedCharacterIterator iterator,int x, int y) {<br>
g.drawString(iterator,x, y);<br>
}<br>
<br>
public void drawChars(char data[], int offset, int length, int x, int y) {<br>
g.drawChars(data, offset, length, x, y);<br>
}<br>
<br>
public void drawBytes(byte data[], int offset, int length, int x, int y) {<br>
g.drawBytes(data, offset, length, x, y);<br>
}<br>
<br>
public boolean drawImage(Image img, int x, int y,ImageObserver observer) {<br>
return g.drawImage(img, x, y,observer);<br>
}<br>
<br>
public boolean drawImage(Image img, int x, int y,int width, int height,ImageObserver observer) {<br>
return g.drawImage(img, x, y,width, height,observer);<br>
}<br>
<br>
public boolean drawImage(Image img, int x, int y,Color bgcolor,ImageObserver observer) {<br>
return g.drawImage(img, x, y,bgcolor,observer);<br>
}<br>
<br>
public boolean drawImage(Image img, int x, int y,int width, int height,Color bgcolor,ImageObserver observer) {<br>
return g.drawImage(img, x, y,width, height,bgcolor,observer);<br>
}<br>
<br>
public boolean drawImage(Image img,int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2,ImageObserver observer) {<br>
return g.drawImage(img,dx1, dy1, dx2, dy2,sx1, sy1, sx2, sy2,observer);<br>
}<br>
<br>
public boolean drawImage(Image img,int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2,Color bgcolor,ImageObserver observer) {<br>
return g.drawImage(img,dx1, dy1, dx2, dy2,sx1, sy1, sx2, sy2,bgcolor,observer);<br>
}<br>
<br>
public void dispose() {<br>
g.dispose();<br>
}<br>
<br>
public void finalize() {<br>
g.finalize();<br>
}<br>
<br>
public boolean hitClip(int x, int y, int width, int height) {<br>
return g.hitClip(x, y, width, height);<br>
}<br>
<br>
public Rectangle getClipBounds(Rectangle r) {<br>
return g.getClipBounds(r);<br>
}<br>
}<br>
<br>
<br>
Which is a graphic object that has no problems with printing empty labels. The way to use it is to wrap the given Graphics object and to give the wrapped object to the print function of the table:<br>
<br>
table.print(new GraphicsShell(graphics));<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top