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

public class Prg_38 extends JFrame{

    JTextArea area = new JTextArea(5,10);                   // 5 rows and 10 colums
    JButton btn1 = new JButton("TextArea");             // name of JBUtton is TextArea
    JButton btn2 = new JButton("TextField");            // name of JButton is TextField
    JRadioButton bold,italic,plain,bold_italic;         // radio buttons
    JTextField fld = new JTextField(15);                // jtextfield is 15 characters long

    Font bf = new Font("Serif",Font.BOLD, 14);
    Font itf = new Font("Serif",Font.ITALIC, 14);
    Font itbf = new Font("Serif",Font.BOLD + Font.ITALIC, 14);

    GridBagConstraints gc = new GridBagConstraints();       // used to place components on GUI

    public Prg_38() {

        super("Interface");                                 // title of Dialog box
        setLayout(new GridBagLayout());         

        gc.insets = new Insets(0,0,2,2);                        // margin between components of GUI (top,left,right,bottom)

        // textarea
        area.setText("textarea");          // default text in textarea
        addComponent(area,0,0);            // method to place components on GUI

        // jbutton

        addComponent(btn1,1,0);            // textare
        addComponent(btn2,2,0);            // textfield

After adding the components to the interface, I'm adding action listeners to every component so that when text area button is clicked, focus changes to text area and when text field button is clicked, focus changes to text field.

        //radio buttons

        bold = new JRadioButton("Bold",false);              // name of radio button is Bold
        addComponent(bold,1,1);

        italic = new JRadioButton("Italic",false);          // name of radio button is Italic
        addComponent(italic,2,1);

        plain = new JRadioButton("Plain",false);                // name of radio button is Plain
        addComponent(plain,1,2);

        bold_italic = new JRadioButton("Bold and Italic",true);     // // name of radio button is Bold and Italic
        addComponent(bold_italic, 2, 2);

        ButtonGroup group = new ButtonGroup();              // it makes sure that only 1 radiobutton is enabled at a time
        group.add(bold);
        group.add(bold_italic);
        group.add(italic);
        group.add(plain);

        // when radio buttons are added to a group, only one radio button can be selected at a time.

        // jtextfield

        fld.setText("enter your name");                 // default text
        addComponent(fld,0,3);

        // action listeners

        thehandler handler = new thehandler();          // action listener class

        btn1.addActionListener(handler);                // textarea action listener
        btn2.addActionListener(handler);                // textfield 


        bold.addItemListener(new handler1(bf));
        italic.addItemListener(new handler1(itf));


        setVisible(true);               // making GUI visible
        setSize(400,400);               // pixels (x * y)
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);             // Frame quits when X is clicked on top right corner

    }

    public class thehandler implements ActionListener{

        public void actionPerformed(ActionEvent e) {

            if(e.getSource() == btn1) {

                // change focus to textarea named area

                area.requestFocusInWindow();

            }


            if(e.getSource() == btn2) {

                fld.requestFocusInWindow();
            }               


        }

    }

Now i want is that if bold radio button is clicked, whichever component is selected(textarea and textfield), the text of that component changes to bold text

    public class handler1 implements ItemListener{

        Font font; 

        public handler1(Font f) {

            font = f;
        }

        public void itemStateChanged(ItemEvent event) {

            if((area.isFocusOwner() == true) && (event.getSource() == bold)) {

                area.setFont(font);
            }

        }
    }

    public void addComponent(Component c, int px, int py) {

        gc.gridx = px;                      // column
        gc.gridy = py;                      // row

        add(c, gc);             // add components(textarea, radio button and all other stuff) to GUI
    }

    public static void main(String[] args) {

        new Prg_38();

    }

}
5
  • 3
    what is the issue that you are facing Commented Mar 29, 2018 at 15:37
  • Don't use TextArea, that is an AWT component. Use a JTextArea. Commented Mar 29, 2018 at 15:46
  • You use Netbeans or Eclipse? Commented Mar 29, 2018 at 15:54
  • I am using Eclipse @HéctorManuelMartinezDurán Commented Mar 29, 2018 at 17:24
  • the issue i was facing was that i wanted to change the focus to JTextArea(named area in above program) when JButton(named TextArea in above program) was clicked by the user. Same goes for TextField. @AmanChhabra It has been solved below Commented Mar 29, 2018 at 17:25

1 Answer 1

2

For any JComponent such as a JTextField, use JComponent.requestFocusInWindow(). For the AWT based TextArea, change it to the Swing based JTextArea and do the same thing.

So:

TextArea area = new TextArea(5,10);

Becomes:

JTextArea area = new JTextArea(5,10);

And:

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == btn1) {
        // change cursor to textarea named area
    }
    if(e.getSource() == btn2) {
         // change cursor to textfield if btn2 is clicked
    }
}

Becomes:

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == btn1) {
        area.requestFocusInWindow();
    }
    if(e.getSource() == btn2) {
         fld.requestFocusInWindow();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm facing another problem. I have made the changes to the code, can you please have a look at it and help me out.
"I'm facing another problem." You can ask as many questions as needed, but please, each on a separate question thread. On the next question, please include a valid minimal reproducible example and leave out all the unnecessary blank lines.
You solved the problem of shifting the focus to TextArea when the TextArea button was clicked. Now i've created 4 radio buttons (Bold, Plain, Italic and Bold_Italic). What i want is when any of the 4 radio buttons is clicked, the text of the TextArea or TextField (which ever has the focus) converts to the corresponding radio button. Suppose Bold radio button is clicked and focus is on TextField, what i want is that the text of TextField changes to bold.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.