简体   繁体   中英

Switching JTextField when clicking a button

I have got a JTextArea and a JTextField and now i want that if i click on JTextArea button the control shifts to JTextArea and when i click the JTextField button control shifts to JTextField. I am not getting anything to do such stuff. The program just changes the text in the JTextField based on the JRadioButton clicked.

   ***This is the code:***



import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


public class prg_32 extends JPanel implements ItemListener{

    JTextArea a1;       
    JButton t1,t2;
    JRadioButton b1,b2,b3,b4;
    JTextField fld1;
    ButtonGroup grp;
    GridBagConstraints gbc = new GridBagConstraints();      //GridBagConstraints object

    public prg_32(){
        setLayout(new GridBagLayout());

        a1 = new JTextArea(3,10);       //TextArea
        gbc.gridx = 0;              
        gbc.gridy = 0;
        gbc.gridheight = 3;
        gbc.fill = GridBagConstraints.VERTICAL;
        add(a1,gbc);        //location of JTextArea

        t1 = new JButton("TextArea");
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        add(t1,gbc);        //location of JButton

        t2 = new JButton("TextField");
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        add(t2,gbc);        //location of JButton

        b1 = new JRadioButton("Bold",false);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        add(b1,gbc);

        b2 = new JRadioButton("Italic",false);
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        add(b2,gbc);

        b3 = new JRadioButton("Plain",false);
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridheight = 1;
        add(b3,gbc);

        b4 = new JRadioButton("Bold/Italic",true);
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.gridheight = 1;
        add(b4,gbc);

        grp = new ButtonGroup();
        grp.add(b1);
        grp.add(b2);
        grp.add(b3);
        grp.add(b4);

        fld1 = new JTextField("enter your name");
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        add(fld1,gbc);

        fld1.setFont(new Font("Serif",Font.BOLD + Font.ITALIC,14));

        b1.addItemListener(this);       //Event Handling
        b2.addItemListener(this);       //Event Handling
        b3.addItemListener(this);       //Event Handling
        b4.addItemListener(this);       //Event Handling
    }

    public void itemStateChanged(ItemEvent e) {
        Font font = null;

        if(b1.isSelected())
            font = new Font("Serif",Font.BOLD,14);
        else if(b2.isSelected())
            font = new Font("Serif",Font.ITALIC,14);
        else if(b3.isSelected())
            font = new Font("Serif",Font.PLAIN,14);
        else
            font = new Font("Serif",Font.BOLD + Font.ITALIC,14);

        fld1.setFont(font);

    }

    public static void main(String[] args){
        prg_32 px = new prg_32();
        JFrame jf = new JFrame();

        jf.setSize(500, 300);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(px);
    }

}

I would suggest implementing an ActionListener and using the method Component.requestFocus() in it to set the focus on the text fields.

    t1 = new JButton("TextArea");
    t1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            a1.requestFocus();

        }
    });
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.gridheight = 1;
    add(t1,gbc);        //location of JButton

    t2 = new JButton("TextField");
    t2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            fld1.requestFocus();

        }
    });
    gbc.gridx = 2;
    gbc.gridy = 0;
    gbc.gridheight = 1;
    add(t2,gbc);    

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