I want to change the look of the whole JTextField but still want it to be editable. The first picture shows a normal JTextField, the second picture shows, what I want it too look like. So basically it is just a blue horizontal line instead of a whole Box.
I tried to override the paintComponent() method of the JTextField and draw a blue line with the Graphics object, but it didn't show any effect. Thanks for any help and sorry if this question does not fit the rules of Stackoverflow.
CodePudding user response:
Try setting a custom border on the JTextField or overriding paintBorder instead
CodePudding user response:
You can override paintComponent(), just as you've said. Here's snippet for JTextField in simple JPanel:
JPanel panel = new JPanel();
JTextField textField = new JTextField() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawLine(10, getHeight() - 10, getWidth() - 10, getHeight() - 10);
}
};
panel.add(textField);
textField.setPreferredSize(new Dimension(100, 50));
textField.setForeground(Color.BLUE);
textField.setHorizontalAlignment(JTextField.CENTER);
add(panel);
setSize(200, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);