Configure JTextField

I would like to know how to set up ui jtextfield so that I can create a rounded rectangular border without a document going beyond the border.

So far, I think I tried the most that I can think of, I created a new FieldView class and changed the shape in the drawing method to fit my custom border, which draws rounded rectangles, the only way I could get rid of the white text document fields / views to set it to opaque, but I think there should be a different way without setting an opaque value.

Do you have any experience setting up laf jtextfield, please write back, I even read the extended Core Swing book with no luck, and if you try to search using Google, please let me know the search phrase since I tried with these keywords like "style", "setup", "ui", "plaf", "laf" and what not.

I sincerely hope that you can push me in the right direction, and I hope that no one will make anything out of it, I really used all my resources that I can think of.

Sincerely.

+4
source share
3 answers

I wanted to solve almost the same problem yesterday, and I got some inspiration from your thought, and I finally found a solution.

  • To make a document inside a JTextField border, you can use

javax.swing.border.EmptyBorder.EmptyBorder (Inserts borderInsets)

2. To avoid a space in the four corners of the JTextField, you can use

g2d.setStroke (new BasicStroke (12));

before you draw a round rectangle. The width of the stroke depends on your demand and simply makes it wide enough to cover the space in the corner.

This is the code:

import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.AbstractBorder; import javax.swing.border.EmptyBorder; public class JTextFieldTest { JTextField textField; boolean activate = false; public void createUI(){ JFrame frame = new JFrame("Test JTextField"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(true); MainPanel mainPanel = new MainPanel(); mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10)); frame.add(mainPanel,BorderLayout.CENTER); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { JTextFieldTest jTextFieldTest = new JTextFieldTest(); jTextFieldTest.createUI(); } public void setActivate(boolean activate){ this.activate = activate; } @SuppressWarnings("serial") class MainPanel extends JPanel{ public MainPanel(){ textField = new JTextField("Please input:"); Font fieldFont = new Font("Arial", Font.PLAIN, 20); textField.setFont(fieldFont); textField.setBackground(Color.white); textField.setForeground(Color.gray.brighter()); textField.setColumns(30); textField.setBorder(BorderFactory.createCompoundBorder( new CustomeBorder(), new EmptyBorder(new Insets(15, 25, 15, 25)))); textField.addActionListener(new FieldListener()); textField.addMouseListener(new FieldMouseListener()); add(textField,BorderLayout.CENTER); setBackground(Color.blue); setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); } } @SuppressWarnings("serial") class CustomeBorder extends AbstractBorder{ @Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { // TODO Auto-generated method stubs super.paintBorder(c, g, x, y, width, height); Graphics2D g2d = (Graphics2D)g; g2d.setStroke(new BasicStroke(12)); g2d.setColor(Color.blue); g2d.drawRoundRect(x, y, width - 1, height - 1, 25, 25); } } class FieldListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.out.println(textField.getText()); } } class FieldMouseListener implements MouseListener{ @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub if(activate == false){ textField.setText(""); } activate = true; textField.setForeground(Color.black); } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } } } 

This is the effect:

enter image description here

For more information, you can view How to create a round rectangle JTextField

+4
source

You should be able to do this using Swing boundaries. I posted some code for how to make rounded borders a long time ago, here - maybe you can adapt it: http://weblogs.java.net/blog/timboudreau/archive/2005/02/jnn_just_got_pr.html

+1
source

If you want to configure individual components, there is a way to do this in any component in fact. Check out this tutorial: http://www.eecchhoo.wordpress.com/2012/11/05/screencast-swingmakeover-extreme-java-gui-programming

It contains a brief summary and a link to download the video tutorial. FYI, the videos are in Indonesian, so if you have a problem to follow the step, there is another link to download the project that he used to create the tutorial.

Please tell me if you have any problems. I hope this link helps

0
source

All Articles