I'm need to modify letter-spacing (font tracking) in a JTextPane, and I can't get it to work.
When I'm using a JTextArea, I can just do:
Font font = new Font("Courier New", Font.PLAIN, 10);
HashMap <TextAttribute, Object> attrs = new HashMap<TextAttribute, Object>();
attrs.put(TextAttribute.TRACKING, -0.1);
font = font.deriveFont(attrs);
textArea.setFont(font);
but as I need to change line spacing, I need to use a JTextPane, and doing:
textPane.setFont(font)
as I did with the JTextArea doesn't work. another thing I tried was:
MutableAttributeSet set = new SimpleAttributeSet();
StyleConstants.setLineSpacing(set, -0.2);
StyleConstants.setFontFamily(set,"Courier New");
StyleConstants.setFontSize(set, 10);
set.addAttribute(TextAttribute.TRACKING, -0.1);
ta.setParagraphAttributes(set, true);
But the tracking attribute doesn't work.
What am I doing wrong?
Do you mean kerning? This one shows how to specify custom kerning and some more text effect
http://java-sl.com/gp_effects.html
Related
I'm using a text area in Netbeans(Java), and I want to highlight certain keywords in the text, something like syntax-highlighting in programming. How could I do that but within a JTextArea in Netbeans?
You can't use a JTextArea to highlight individual pieces of text.
I would suggest a JTextPane so you can use styled attributes.
The basic code would be something like:
JTextPane textPane = new JTextPane();
textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );
StyledDocument doc = textPane.getStyledDocument();
// Define a keyword attribute
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
// Change attributes on some text
doc.setCharacterAttributes(0, 5, keyWord, false);
Is there a way to set fonts on a string, and then draw the string using graphics? I know you can do it on Jlabels, and Jtextfields, and J-other components, but is there a way to do it just on the string instead? Thank you.
You can do it with attributed strings an example of is
Font font = new Font("LucidaSans", Font.PLAIN, 14);
AttributedString atString= new AttributedString("Example text string");
atString.addAttribute(TextAttribute.FONT, font);
graphic.drawString(atString.getIterator(),x,y);
Cheers!
We can actually set the font in graphics...and then draw the string.
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.setFont(Fonts.f);
g.drawString(Fonts.text, 50, 50);
}
Yes, it's possible, start by checking out the Graphics, Text tutorials
Basically, you can set the font using Graphics#setFont and draw a String using Graphics#drawString.
Also see Performing Custom Painting for how you can perform custom painting in Swing
For example
List available font names....
String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for (String font : fonts) {
System.out.println(font);
}
or
Font fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (Font font : fonts) {
System.out.println(font);
}
You can manipulate a font's properties using one of the Font#derive methods...
Font font = ...;
Font bigger = font.derive(32f);
Font bolder = font.derive(Font.BOLD);
Font biggerAndBolder = font.derive(Font.BOLD, 32f);
I found this component recently and its great exactly what I wanted but I am unsure how to use it.
How would I set the font of my JTextField to what is selected by the user in the JFontChooser?
This is all I can find on it:
The JFontChooser class is a swing component for font selection. This class has JFileChooser like APIs. The following code pops up a font chooser dialog.
JFontChooser fontChooser = new JFontChooser();
int result = fontChooser.showDialog(parent);
if (result == JFontChooser.OK_OPTION)
{
Font font = fontChooser.getSelectedFont();
System.out.println("Selected Font : " + font);
}
what I want it to do is:
update: tNumber.setFont(new font(""Tahoma", Font.BOLD, 300));
To what ever the user has chosen for the font style and size in font chooser.
Use font.deriveFont(Font.BOLD, 300f) to create a new font, based on the current font with the properties you supply.
tNumber.setFont(font.deriveFont(Font.BOLD, 300f));
See Font#deriveFont(float, int) for more details
Well you were very close,
JFontChooser fontChooser = new JFontChooser();
int result = fontChooser.showDialog(parent);
if (result == JFontChooser.OK_OPTION)
{
Font font = fontChooser.getSelectedFont();
tNumber.setFont(font.deriveFont(Font.BOLD, 300f));//This is the line I added.
}
See setFont and deriveFont documentation.
I have this class, and I am trying to display a custom font in a text field, but but when I run it the font is super tiny like 2px tiny. If i just run font = new Font("sans-serif", Font.PLAIN, 24); it displays just fine at the right font size.
Here is what it looks like:
Here is what it looks like when I only use font = new Font("sans-serif", Font.PLAIN, 24);
What is causing the small text box with a custom font?
public class Search extends JTextField{
public Search(int width){
super(width);
Font font;
String filename = "/media/fonts/SourceCodePro-Light.ttf";
try{
InputStream is = this.getClass().getResourceAsStream(filename);
font = Font.createFont(Font.TRUETYPE_FONT, is);
font = font.deriveFont(24);
}catch(FontFormatException | IOException ex){
font = new Font("sans-serif", Font.PLAIN, 24);
}
this.setFont(font);
}
}
font.deriveFont has two overloaded forms that can be quite similar. The one taking int sets the font style, the one taking float sets the font size. YOu are invoking the int version instead of the float version. Change 24 to 24.0f, and it will work
Usually, when I initialize the fonts I want to use in my SWING applications, I do it this way:
public static final Font TITLEFONT = new Font("Calibri", Font.BOLD, 40);
Now, I have to do it a bit differently since I'm using some custom fonts from a .ttf file. I initialize the font this way:
try
{
InputStream is = OptionsValues.class.getResourceAsStream("fonts//KOMIKAX_.ttf");
TITLEFONT = Font.createFont(Font.TRUETYPE_FONT, is);
}
catch (Exception ex)
{
ex.printStackTrace();
System.err.println("Font not loaded. Using Calibri font.");
TITLEFONT = new Font("Calibri", Font.BOLD, 40);
}
I'm pretty sure it initializes it correctly (I can't tell for sure since it is too small for me to see), but I'd like to know how I can manually set the font's size (and if it's bold / other attributes) when loading a font this way.
Thanks a lot in advance!
createFont returns a Font and you can call deriveFont(...) on this, passing in a float for the point size, or an int and float for Font style and point size. I cannot say whether it will work for your particular situation, but it's worth a try.
e.g.,
InputStream is = OptionsValues.class.getResourceAsStream("fonts//KOMIKAX_.ttf");
TITLEFONT = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(Font.BOLD, 40f);
I'd simply use:
Font.ITALIC
Font.BOLD
Font.PLAIN