Hi. I have a predicament... I'm trying to make a JComponent that looks like this:
_____________________
| |
|Option Name [Text Field] |
|____________________|

It's for a Option screen that I asked about earlier (I got that part working fine). But now, I need a component that has the option name, followed by a text field that the user enters the key that calls a command. Below is the code that i have, but when I add the JComponent to a JPanel, it doesn't show the text field, just the text that I'm drawing to the JComponent... Does anyone know how to fix this?

class JKeySetter extends JComponent implements KeyListener
{
    // Values that decide what key is linked to which movement
    //=============================================
    public static final int PIECE_LEFT = 0;
    public static final int PIECE_RIGHT = 1;
    public static final int PIECE_DOWN = 2;
    public static final int PIECE_ROTATE = 3;
    public static final int PADDLE_LEFT = 4;
    public static final int PADDLE_RIGHT = 5;
    public static final int PADDLE_DOWN = 6;
    public static final int PADDLE_UP = 7;
    //=============================================
    
    // JTextField that contains/displays key that 
    // moves the piece this object represents.
    private JTextField input; 
    private String name; // What is displayed next to the text field
    private int type; // piece this object represents.
    private int thisCode; // KeyCode that this object is set to
    private char thisChar; // KeyChar that this object is set to
    
    //Constructor
    public JKeySetter(String name, int type)
    {
        this.name = name;
        this.type = type;
        this.setSize(207, 64);
        
        input = new JTextField();
        add(input);
    }
    
    public void paint(Graphics g)
    {
        input.paint(g);
        g.drawString(name, 0, 30);
    }
    
    // set the values in the Options class to the key that was typed 
    // in the JTextField (still need to implement this part)
    public void setKey(KeyEvent e)
    {
        switch (type)
        {
            case PIECE_LEFT:   Options.pieceLeftCode = e.getKeyCode(); thisChar = e.getKeyChar();   break;
            case PIECE_RIGHT:  Options.pieceRightCode = e.getKeyCode(); thisChar = e.getKeyChar();  break;
            case PIECE_DOWN:   Options.pieceDownCode = e.getKeyCode(); thisChar = e.getKeyChar();   break;
            case PIECE_ROTATE: Options.pieceRotateCode = e.getKeyCode(); thisChar = e.getKeyChar(); break;
            case PADDLE_LEFT:  Options.paddleLeftCode = e.getKeyCode(); thisChar = e.getKeyChar();  break;
            case PADDLE_RIGHT: Options.paddleRightCode = e.getKeyCode(); thisChar = e.getKeyChar(); break;
            case PADDLE_DOWN:  Options.paddleDownCode = e.getKeyCode(); thisChar = e.getKeyChar();  break;
            case PADDLE_UP:    Options.paddleUpCode = e.getKeyCode(); thisChar = e.getKeyChar();    break;
            default: break;
        }
    }
    
    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}
}
This article has been dead for over six months. Start a new discussion instead.