'Is there any way to return the color of a JTextField?
For example if you set the color of a textfield as Color.RED, is there a method to actually return that color?
I found a method in the Oracle logs called getCaretColor() but it doesn't seem to return the right thing...
Solution 1:[1]
JTextField inherits getBackground() and getForeground() from Component. If I understand your question, that should get you the values.
Solution 2:[2]
The below code will return a JTextField with a black background and red text.
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.FlowLayout;
public class test
{
public static void main(String[] args)
{
JTextField textField = new JTextField(20);
JFrame frame = new JFrame("Java"); // Just the window title, name doesn't matter
frame.setLayout(new FlowLayout());
Color color = new Color(255,0,0); // Set the text color
textField.setBackground(Color.BLACK); // Set the black background
textField.setForeground(color);
frame.add(textField);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,200);
frame.setVisible(true);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mike Brazinski |
| Solution 2 | Matthew |
