kennymydude
kennymydude

Reputation: 1

How to make a jtextarea respond to another jtextarea change

    JComboBox cBox = new JComboBox();
    cBox.addItem("Food"); String x = "Food";
    cBox.addItem("Shirt");
    cBox.addItem("Computer");
    cBox.setSelectedItem(null);
    cBox.setBounds(56, 127, 336, 27);
    contentPane.add(cBox);

    tPName = new JTextArea();
    tPName.setBounds(38, 227, 130, 26);
    contentPane.add(tPName);
    tPName.setColumns(10);

    tSName = new JTextArea();
    tSName.setBounds(262, 227, 130, 26);
    contentPane.add(tSName);
    tSName.setColumns(10);

    JButton btn = new JButton("Further Details");
    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String name = (String) cBox.getSelectedItem(); //gets value of combo box
            tPName.setText(name); //text area displays value
            String x = (String) tPName.getText();
            name = "Food"; tSName.setText(F);
            name = "Shirt"; tSName.setText(S);
            name ="Computer"; tSName.setText(C);

        }
    });
    btn.setBounds(162, 166, 117, 29);
    contentPane.add(btn);

how would I make tSName display a text corresponding to a specific value of tPName which is copied from a combobox where F, S and C strings stand for those corresponding values I want to use.

Upvotes: 0

Views: 48

Answers (2)

Khushit Shah
Khushit Shah

Reputation: 555

I right now dont have pc so I cant check errors but. this should work

 tPName.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tSName.setText(tPName.getText()); // put the value of tPName to tSName.
            // i think this is all you want. i actually cant get your question.
           // you can put a switch case to put coreesponding values in tSName. like: 
     switch(tPName.getText()){
            case "a" : tSName.setText("1");
                         break;
            //..... so on
             default:
                      tSName.setText("stack overflow is great");
                         break;

         }
      }
    });

EDIT:- Onece you do this try to refresh and validate the textareas.

tPName.validate();
tSName.validate();
tPName.repaint(); // refresh  still not sure as i dont have pc if this not works try <your jframe>.repaint(); this should work

Upvotes: 0

Robin
Robin

Reputation: 41

I'm not sure if I understand you correctly. You try to make one textarea display something, when the text of another textarea changes, am I right? If yes, you should look for an Event of the first Textbox (something like tPNameKeyTyped) in this Event you can check the tPName.getText() simply with

if(tPName.getText().equals("whatever")){
   tSName.setText("whatever u want");
}

Is that what you are looking for?

Upvotes: 1

Related Questions