Search code examples
javaswingborderjtextfield

Is there any way to get one side(i.e., right bordered line) of the jtextfield colored


Hi I prepared one swing frame in which I placed two text fields. Is there any way to get one side(i.e., right bordered line) of the jtextfield colored? Please suggest. I checked many things, but I couldn't find.Thanks in advance.


Solution

  • I would add a Border to the text field, something along the lines of:

    Border oldBorder = jTextField.getBorder();
    Border redBorder = BorderFactory.createMatteBorder(0, 0, 0, 5, Color.RED);
    Border newBorder = BorderFactory.createCompoundBorder(redBorder, oldBorder);
    jTextField.setBorder(newBorder);
    

    This approach keeps the old border and wraps it inside your red (partial) border.