简体   繁体   中英

--Invisible, Not visible, but editable JTextField

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.

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

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);

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM