I am using Highlighter.HighlightPainter
interface for highlighting the lines of a text area. I used the source code from this website: Line Painter. It works just great, but when I used org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel
for decorating the GUI, an issue comes up. Whenever I change the font of the text area to Monospaced
, the paint()
method of Highlighter.HighlightPainter
isn't invoked for some reason. Here is a sample code:
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Shape;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;
public class TestFrame extends JFrame implements Highlighter.HighlightPainter
{
private static final long serialVersionUID = 1L;
static
{
try
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
UIManager.setLookAndFeel(new org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel());
}
catch(Exception e)
{
e.printStackTrace();
}
}
public TestFrame() throws BadLocationException
{
super("The title");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JTextArea txt = new JTextArea(10, 30);
txt.getHighlighter().addHighlight(0, 0, this);
txt.setFont(new Font("Monospaced", Font.PLAIN, 12));
JPanel container = (JPanel) getContentPane();
container.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
container.add(txt);
pack();
setLocationRelativeTo(null);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
new TestFrame().setVisible(true);
}
catch(BadLocationException e)
{
e.printStackTrace();
}
}
});
}
@Override
public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c)
{
System.out.println("paint() is invoked!");
}
}
If I comment this line:
txt.setFont(new Font("Monospaced", Font.PLAIN, 12));
paint()
will be invoked. Is there anyway to fix this issue?
1) @Eng.Fouad this was n_times mentioned (@camickr, @StanislavL) here for styled and highligted test use JTextComponent that supporting that
2) @Eng.Fouad you are right output from JTextArea
from code
3) as @ trashgod correctly mentioned never to set whatever in Substance without invokeLater, never, whatever Look and Feel sensitive, right maybe in this moment Font isn't important, maybe not
4) simple JTextArea have got some issue with Font and Look and Feel, maybe there are own Highlighter concept as for Renderer (sorry I lazy read API after wake_up) and Substance, for Renderer concept you must to use SubstanceRenderer instead of XxxRenderer, then all formatting wokrs as you excepted