Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tab character in JPanel string

 
Alan Drennan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I include a tab charater in a JPanel's String parameter, it does not display.

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

public class demo extends JApplet {
JPanel numBar = new JPanel(new FlowLayout(FlowLayout.LEADING));
...

public void init(){
JApplet myApp = new JApplet();
...
getContentPane().add(sumBar);
...
String str = "xxx" + '\t' + "zzz";
JLabel msg1 = new JLabel(str, JLabel.LEADING);
msg1.setOpaque(false);
msg1.setForeground(fg);
numBar.setBackground(bg);
numBar.add(msg1);

}
}

The JLabel displays "xxxzzz", instead of "xxx zzz"

What am I doing wrong?
 
Fahd Shariff
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes JLabel does not render tab characters properly. You will need to override the paint method of JLabel to handle this. Or the second option is to replace your \t characters with spaces.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic