-1

How do I make a textfield, that is editable, but invisible? I need to give parameters to my program, but I don't want the users to see what they are actually putting into the input. If I set it to invisible, I can't edit it's contents either. I tried making it transparent like this: Making a JButton invisible, but clickable? but for some reason, the textfield still shows. I also tried using layeredpanes, but I can only put the textfield on top of them, not the other way, NetBeans just moves them around, so everything fit.

I'm open to other ideas, the input is a string followed by an "enter".

I think I asked the question wrong. The problem isn't that the text is visible, but the whole text field is visible. The input is a string from the barcode reader, so nothing needed to be shown at all.

EDIT: We solved it in a different way. We added a white line to the top of the background image, put the textfield there, and changed every of it's colours to white.

kry
  • 362
  • 3
  • 13

3 Answers3

2

I think you can use a JPasswordField for your purpose the user can see the number of characters that he write but no the content

https://docs.oracle.com/javase/tutorial/uiswing/components/passwordfield.html

New idea:

you can add a changelistener to your jtextfield and when the user write any character it save to a StringBuilder whit the append method, then set the Jtextfield to null automatically to clear jtextfield

whit this way all the characters that user write will store in a StringBuilder and when he clicks the enter button you will have the String that user write character to character

then you only need to call toString() method of StingBuilder to get the complete String

MaQuiNa1995
  • 431
  • 1
  • 9
  • 23
  • Not really. It fixes the problem, where the users don't see their input, but I don't want them to see anything at all. In design sense, it just looks idiotic, to have a field somewhere, in which data is written automatically. But it's good as a "nothing else" solution. – kry Apr 28 '17 at 11:56
  • I think I asked the question wrong. The problem isn't that the text is visible, but the whole text field is visible. The input is a string from the barcode reader, so nothing needed to be shown at all. – kry Apr 28 '17 at 12:23
  • ahh okey so i thnik you can use a String parameter in the constructor to pass the String from the barcode reader instead to store in a UIComponent – MaQuiNa1995 Apr 28 '17 at 12:28
1

I think that adding keylistener to jpanel would solve your issues, try something like this:

String str = ""; // global
public void yourMethod() {
    JFrame yourFrame = new JFrame();
    JPanel yourPanel = new JPanel();
    yourFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    yourFrame.add(yourPanel);
    yourPanel.addKeyListener(new KeyListener() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) { 
                System.out.println(str);// here you can use switch for cases that you want or whatever you want  to do with string, I simply print it out
                str = "";
            } else {
                str += e.getKeyChar();                    
            }
        }
        @Override
        public void keyTyped(KeyEvent e) {
        }
        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    // jpanel must be focused if you want key listener to work
    yourPanel.setFocusable(true);
    yourPanel.requestFocusInWindow();

    yourFrame.setSize(300, 250);
    yourFrame.setVisible(true);

}
FilipRistic
  • 2,661
  • 4
  • 22
  • 31
  • It works as long as the input is from a keyboard. The reader gives the enter effect, but not the string. However, we solved it in a different way, updating the question. – kry Apr 28 '17 at 12:51
-1

First of all I know I'm late and it's no more help for you but maybe it is for others.

What i did was to just set the textfield in the corner and set both width and height to 1 (like this: TextField.setBounds(0, 0, 1, 1);) which makes it pretty much impossible to be seen. This enables you to still use it. I know this technically doesn't make it invisible but it is very close.

MoLa0404
  • 1
  • 4
  • 1
    By ignoring the "Invisible" requirement, you are not answering the question and thus will likely be downvoted. I would recommend posting this type of idea in a comment instead of an answer to avoid getting downvotes. Your idea, which may be useful to some, will still get seen. – Timothy C. Quinn Dec 12 '17 at 18:08
  • The question wasn't really "Invisible", but "not visible", so the users can still use a pseudo keyboard, but don't see what they are doing (A magnetic card reader was in this case.) But since you can't comment until you have 50 reputation, you can't even give out minor insights or notes. Kinda like you can't comment on CodeGolf like "you hit an extra space there, so -1 byte". Kinda idiotic if you ask me. – kry Dec 13 '17 at 19:07