I having been trying to change the font size and textposition of a textfield but I have not had any luck.

I am trying to change the font size of a text field. I would like the font that is typed in the text field to be bigger and bolded.

I have tried this, but it does does not work:

Font font1 = new Font("SansSerif", Font.BOLD, 20);
JTextField textfield = new JTextField (); 
 
textfield.setFont(font1);

Also, I want the texted to be centered in the textfield. I have used this:

textfield.setHorizontalTextPostion.CENTER
textfield.setVerticalTextPostion.CENTER

Thank you for your help in advance

Recommended Answers

All 6 Replies

setting the font to the text field should work.
Also to center align the text use textfield.setHorizontalAlignment(JTextField.CENTER);

Following code might help you,

import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import java.awt.font.*;
import java.io.*;
import javax.swing.*;


public class TextFieldExample{
  public static void main(String[] args) {
    JFrame frame = new JFrame();
	frame.setLayout(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	Font font1 = new Font("SansSerif", Font.BOLD, 20);
    
    JTextField textfield = new JTextField();
	textfield.setLocation(5, 5);
	textfield.setSize(150,20);	
	textfield.setFont(font1);
	textfield.setHorizontalAlignment(JTextField.CENTER);
    frame.getContentPane().add(textfield);
	
	frame.setSize(350, 60);
    frame.setVisible(true);

  }

}

I have no idea of what I am doing! It's all complicated. I only wanted to change my font size and type of writing. HELLLLPP!!!!

Carrie Pugh:
Welcome to Daniweb.

Just a few hints, though: don't hijack old threads. This thread was answered five years ago.
Saying you have no idea what you are doing, doesn't help us ascertan what you have tried. But, in my opinion, you have tried about nothing.

"I only wanted to change my font size and type of writing"
the post right above yours shows you how to do that.

Hey, just a guy from 4 years in the future. I came to stop by and tell you to stop criticizing people and just answer the question if a person has one. You're attitude is not helping anyone dude.

Howard said the same thing...

Yeah, i had the same thing too

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.