
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we implement a rounded JTextField in Java?
In this article, we will learn to implement a rounded JTextField in Java. The JtextField is rectangular in shape, and to create a custom JTextField with rounded corners, we will use the RoundRectangle2D class and with the help of fillRoundRect() and drawRoundRect() methods in Java Swing.
What is a JTextField?
A JTextField is a subclass of the JTextComponent class, and it is one of the most important components that allows the user to an input text value in a single-line format.
Below is the graphical representation of a JTextField:
A JTextField class will generate an ActionListener interface when we try to enter some input inside it. The important methods of the JTextField class are setText(), getText(), setEnabled(), etc.
How to implement a rounded JTextField?
By default, a JTextField has a rectangular shape. We can also implement a round-shaped JTextField by using the RoundRectangle2D class and need to override the paintComponent() method.
RoundRectangle2D class
The RoundRectangle2D class represents a rectangle with rounded corners, defined by a location (x, y), dimensions, and the width and height of the arc used to round the corners.
fillRoundRect() Method
The fillRoundRect() is a method of the Graphics Class and is used for drawing the outline of a rounded rectangle. It takes 6 parameters as input: x-coordinate, y-coordinate, width, height, arcWidth, and arcHeight.
Syntax
The following is the syntax for the drawRoundRect() method declaration:
public abstract void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
drawRoundRect() Method
The drawRoundRect() is a method of the Graphics Class and is used to create solid, filled, rounded rectangles. It takes 6 parameters as input: x-coordinate, y-coordinate, width, height, arcWidth, and arcHeight
Syntax
The following is the syntax for the drawRoundRect() method declaration:
public abstract void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
paintComponent() Method
The paintComponent() method is needed to draw something on a frame other than drawing the background color. This method already exists in the JTextFiled class.
Implement a Rounded JTextField
To implement a rounded JTextField in Java, first, we will create a class named "RoundedJTextFieldTest" that extends the JFrame, declare a private JTextField named "tf", and initialize the constructor for the RoundedJTextFieldTest class. Also, set the title for the window as "RoundedJTextField Test".
Creates an object for the RoundedJTextField with a width of 15 characters, and adds the textfield at the top of the frame using the BorderLayout:
tf = new RoundedJTextField(15); add(tf, BorderLayout.NORTH);
Paints the rounded background and text content, then sets the graphics color to the component's background color using the setColor() method, draws a filled rounded rectangle using the fillRoundRect() method, and lastly calls super.paintComponent() to paint the text content on top.
protected void paintComponent(Graphics g) { g.setColor(getBackground()); g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15); super.paintComponent(g); }
Draws the rounded border, uses foreground color for the border, and draws an outline rounded rectangle matching the background shape using the drawRoundRect() method.
protected void paintBorder(Graphics g) { g.setColor(getForeground()); g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15); }
Creates a RoundRectangle2D shape matching the painted dimensions:
shape = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 15, 15);
Example
Below is an example to implement a rounded JTextField in Java:
import java.awt.*; import javax.swing.*; import java.awt.geom.*; public class RoundedJTextFieldTest extends JFrame { private JTextField tf; public RoundedJTextFieldTest() { setTitle("RoundedJTextField Test"); setLayout(new BorderLayout()); tf = new RoundedJTextField(15); add(tf, BorderLayout.NORTH); setSize(375, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String args[]) { new RoundedJTextFieldTest(); } } // implement a round-shaped JTextField class RoundedJTextField extends JTextField { private Shape shape; public RoundedJTextField(int size) { super(size); setOpaque(false); } protected void paintComponent(Graphics g) { g.setColor(getBackground()); g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15); super.paintComponent(g); } protected void paintBorder(Graphics g) { g.setColor(getForeground()); g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15); } public boolean contains(int x, int y) { if (shape == null || !shape.getBounds().equals(getBounds())) { shape = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 15, 15); } return shape.contains(x, y); } }