How to cross out a Swing component?

Hi, I have a requirement when in some state I have to cross out the swing component (text field), while preserving its string value. those. the component should not show what string value it has.

+4
source share
6 answers

There is a JPasswordField , which is a text field that can be switched to show ** (or some other echo character) instead of the actual content. This is probably the easiest way.

If this is not enough, you can pull out the content and save it from the outside, empty or stain or something in this field, and setEnabled(false) on it. When you re-enable the field, return the contents. If you need to use getText() in the meantime, you will have to override this so that you can substitute the stored content when necessary.

+4
source

To make all text a black call

 textfield.setForeground(new Color(0,0,0,0)) textfield.setBackground(Color.BLACK) textfield.setOpaque(true) 

to prevent text call selection

 textfield.setFocusable(false) 
+3
source

You can use JLayer Component :

An example of gradient in front of JPanel

+3
source

You must hide the text box with .setVisible(false) and replace it with another component that is black (for example, a JLabel with a black background). This can be done by placing both components (label and text field) in the selected JPanel , stacking them on top of each other. You can simply just hide the text box if you don't need a black area instead.

There are other ways for this to use only a text field, but none of them is reliable, since different operating systems will separately present Swing components (especially Mac OS X), therefore overriding the text field with the paint() method or changing the color of the text (like described in @GarrettHall) will not always work, for example.

+2
source

Probably the easiest way to do this is to remove the component from your container and replace it with a dummy component of the same type. This can be beautifully encapsulated by creating your own component that encapsulates the replacement of components (skeleton):

 public class BlackOutTextField extends JPanel { private final JTextField realField = new JTextField(); private final JTextField dummyField = new JTextField(); private boolean isBlackedOut; { dummyField.setEditable(false); setLayout(new BorderLayout()); add(realField, BorderLayout.CENTER); } public String getText() { return isBlackedOut ? "" : realField.getText(); } public void setText(final String text) { if (!isBlackedOut) realField.setText(text); } public void setBlackedOut(final boolean blackedOut) { if (this.isBlackedOut != blackedOut) { this.isBlackedOut = blackedOut; removeAll(); add(this.isBlackedOut ? dummyField : realField, BorderLayout.CENTER); revalidate(); } } } 

You get the idea.

+2
source

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.

+1
source

Source: https://habr.com/ru/post/1413754/


All Articles