I made a small example that just makes black characters and the text box black using Only JTextField, as you requested.
import java.awt.Color; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class RunnableTest{ public static void main(String args[]){ JFrame frame = new JFrame("frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JTextField textfield = new JTextField(); textfield.setPreferredSize(new Dimension(400,30)); textfield.setForeground(Color.black); textfield.setBackground(Color.black); textfield.setSelectedTextColor(Color.black); textfield.setSelectionColor(Color.black); frame.getContentPane().add(panel); panel.add(textfield); frame.pack(); frame.setVisible(true); } }
this method textfield.setForeground(Color.black);
sets the font to black, and textfield.setBackground(Color.black);
this other method sets the black background. and
textfield.setSelectedTextColor(Color.black); textfield.setSelectionColor(Color.black);
sets a black selection so that you cannot see what was selected.
Of course, you can use the password field as an alternative if you do not want to use JTextField and do not allow the user to copy the text.
EDIT: if you do not want the user to copy text using CTRL + C, add a key element to the text box so that you know when the user pressed both keys simultaneously. Of course, there are many other combinations in other systems, but this is not a question.
source share