S.Q
S.Q

Reputation: 155

Adding text to JTextArea box when a button is clicked?

Okay so I am trying to figure out how to add text into a TextArea when a certain button is clicked in my GUI. For this current problem, When i click the reset button I want it to display a few lines of text that i have prewritten already. I am not sure how i would implement this into my applet.

here is my code for the the button

public class DisplayButtonPanel2 extends JPanel{

//create buttons for reseting back to original data, displaying data and sorting data
JButton resebutton = new JButton("RESET");
JButton displaybutton = new JButton("DISPLAY");  
JButton sortbutton = new JButton("SORT"); 

//constructor 
public DisplayButtonPanel2()
{

      //create action listener object
  BListen listener = new BListen();

      //add action listen functionalty to buttons
  resebutton.addActionListener(listener);
  displaybutton.addActionListener(listener);
  sortbutton.addActionListener(listener);

     //add buttons to panel
  add(resebutton);
  add(displaybutton);
  add(sortbutton);

}

public class BListen implements ActionListener
{

   public void actionPerformed(ActionEvent event){

     if (event.getSource() == resebutton){

        JOptionPane.showMessageDialog(null,"reset Button Clicked");
        DisplayEmployeePanel displayemply = new DisplayEmployeePanel();
        displayemply.resetText();


     }

     else if(event.getSource() == displaybutton){

        JOptionPane.showMessageDialog(null,"display Button Clicked");
       // DisplayEmployeePanel displayemply = new DisplayEmployeePanel();
     }
     else if(event.getSource() == sortbutton){

        JOptionPane.showMessageDialog(null,"sort Button Clicked");
     }


  }

}

code for the textArea

public class DisplayEmployeePanel extends JPanel{


//create a text area that will display employees

JTextArea employeetextarea = new JTextArea("~Company Employee's~", 20 ,30);          //textinarea, hieght, width



 //constructor
 public DisplayEmployeePanel()
 {

   employeetextarea.setEditable(false);   //will not be able to edit text in area

  String ns = "\nDisplay Employee Info BELOW";
  employeetextarea.append(ns);



  //add text area to panel
  add(employeetextarea);


}

public void resetText()
{

  String sn = "\nreset to original emmployee list";
  employeetextarea.append(sn);
  add(employeetextarea);
  System.out.println("reset text");///testing purposes

 }



 }//end class

EDIT* adding in more source code if that helps, this code for creating frame

 public class CreateCompanyGUI extends JFrame{

 //constructor
 public CreateCompanyGUI()
 {
   super("Company Applet!");                                      //tittle
  setSize(800,800);                                              //set size  *careful when using pack*
  setLocationRelativeTo(null);                                   //places frame in middle of screen by default
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                //how to close
  setLayout(new BorderLayout());                                 //set BorderLayout manager for this frame


  //create panels
  DisplayEmployeePanel dep = new DisplayEmployeePanel();
  //  DisplayEmployeePanel2 dep2 = new DisplayEmployeePanel2();
  DisplayButtonPanel dbp = new DisplayButtonPanel();
  DisplayButtonPanel2 dbp2 = new DisplayButtonPanel2();
 // DisplayListPanel dlp = new DisplayListPanel();


  //add panels to frame
  add(dep, BorderLayout.NORTH);
  add(dbp2, BorderLayout.CENTER);
  add(dbp, BorderLayout.SOUTH);
 // add(dlp, BorderLayout.EAST);
 // add(dep2, BorderLayout.CENTER);

  //pack (packs the window and creates appporiate size
  pack();
  //visible = TRUE (so we can see the frame)
  setVisible(true);


}



}//end class

code for creating GUI

public class CompanyApplet extends JApplet{

public static void main(String[] args)
{
     //create GUI for company applet
      new CreateCompanyGUI();


  }
 } 

Upvotes: 0

Views: 1668

Answers (1)

camickr
camickr

Reputation: 324098

    DisplayEmployeePanel displayemply = new DisplayEmployeePanel();
    displayemply.resetText();

You can't keep creating a new DisplayEmployeePanel. You want to update the textarea of the existing panel that is visible on the GUI. Then means that the DisplayButtonPanel2, needs a reference to the DisplayEmployeePanel.

So in your code where you create the two panels and add them to the frame you need to pass the DisplayEmployeedPanel as a parmameter to the DisplayButtonPanel2.

Or the simpler solution is to just have a single class that create the two panels. Then all the methods in that class have access to all the components defined in the class.

Upvotes: 2

Related Questions