Home > other >  JTextfield customization issue
JTextfield customization issue

Time:12-20

I have made a frame and put a button and a text field in it. I am using the setMargin method to set a margin between the caret and the border of the text field and works perfectly fine until I add a border to it.

After adding the border the setMargin call method does not work.

Could you please help me understand the origin of the issue and find an alternative of having both a border and a certain margin?

public class main extends JFrame  {

    public static void main(String[]args){
        JTextField textfield0;
        JButton button0;
        
        Border border7=BorderFactory.createDashedBorder(new Color(0xA524FF), 2, 5, 4, true);
        Border border8=BorderFactory.createCompoundBorder();
        Border border01=BorderFactory.createLineBorder(Color.RED);
        Border border02=BorderFactory.createLineBorder(Color.YELLOW);
        Border border9=BorderFactory.createCompoundBorder(border01, border02);
        
        textfield0=new JTextField();
        textfield0.setPreferredSize(new Dimension(300,30));
        textfield0.setFont(new Font("Consolas",Font.BOLD,15));
        textfield0.setCaretColor(Color.RED);
        textfield0.setBackground(Color.CYAN);
        textfield0.setForeground(Color.MAGENTA);
        textfield0.setText("name");
        //textfield0.setBorder(border9);
        textfield0.setSelectedTextColor(Color.YELLOW);
        textfield0.setMargin(new Insets(0,7,0,5));
        textfield0.setCaretPosition(0);
        textfield0.setSelectionColor(Color.PINK);

        button0=new JButton();
        button0.setText("submit");
        button0.setPreferredSize(new Dimension(100,30));    
        button0.setFocusable(false);
        button0.setBackground(textfield0.getBackground());
        button0.setFont(textfield0.getFont());
        button0.setBorder(textfield0.getBorder());
        
        JFrame frame00=new JFrame();
        frame00.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame00.setLayout(new FlowLayout()); 
        frame00.add(button0);
        frame00.add(textfield0);
        frame00.pack();
        frame00.setVisible(true);
    }
}

CodePudding user response:

If you read the javadoc for Border, it states: "Use EmptyBorder to create a plain border (this mechanism replaces its predecessor, setInsets)"

So you can either use a CompoundBorder with a PlainBorder and the one you are setting, or override getBorderInsets(Component c) on your existing Border

CodePudding user response:

We can find an answer for this in the JavaDoc which requires a few extra steps:

Sets margin space between the text component's border and its text. The text component's default Border object will use this value to create the proper margin. However, if a non-default border is set on the text component, it is that Border object's responsibility to create the appropriate margin space (else this property will effectively be ignored).

Source: https://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#setMargin-java.awt.Insets-

And to do the above we can do one of two things, we can use a compound border as shown here: Java Swing - setting margins on TextArea with Line Border or we can take the following steps:

Overriding both AbstractBorder.getBorderInsets(Component c) and AbstractBorder.getBorderInsets(Component c, Insets insets) to provide the correct insets

Source: https://docs.oracle.com/javase/tutorial/uiswing/components/border.html#custom

I strongly recommend reading through the above link to get a better understanding of borders and how you can work with them.

  • Related