- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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?
A JTextField is a subclass of JTextComponent class and it is one of the most important components that allow the user to an input text value in a single-line format. A JTextField class will generate an ActionListener interface when we trying to enter some input inside it. The important methods of a JTextField class are setText(), getText(), setEnabled() and etc. By default, a JTextfield has a rectangle shape, we can also implement a round-shaped JTextField by using the RoundRectangle2D class and need to override the paintComponent() method.
Example
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); } }
Output
- Related Articles
- How can we implement cut, copy and paste functionality of JTextField in Java?
- How can we add padding to a JTextField in Java?
- How can we make JTextField accept only numbers in Java?
- How can we limit the number of characters inside a JTextField in Java?
- Can we save content from JTextField to a file in Java?
- How can we implement a JToggleButton in Java?
- How can we implement a Custom HashSet in Java?
- How can we implement a scrollable JPanel in Java?
- How can we implement a timer thread in Java?
- How can we implement a custom iterable in Java?
- How can we draw a rounded rectangle using the Graphics object in Java?
- How can we implement editable JComboBox in Java?
- How can we implement transparent JDialog in Java?
- How can we Implement a Stack using Queue in Java?
- How can we implement a map in JShell in Java 9?

Advertisements