Xtrato
Xtrato

Reputation: 495

Clearing a JTextArea from another class

I'm very new to Java and I'm setting myself the challenge on writing a Caesar shift cipher decoder. I'm basically trying to clear a JTextArea from another class. I have two classes, a GUI class called CrackerGUI and a shift class. The JtextArea is in the GUI class along with the following method:

public void setPlainTextBox(String text)
{
    plainTextBox.setText(text);
}

The GUI class also has a clear button with the following:

private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Shift classShift = new Shift();
    classShift.btnClear(); 
}   

Lastly i have the method in the shift class to clear the JTextArea.

public class Shift extends CrackerGUI {

public void btnClear()
{
    CrackerGUI gui = new CrackerGUI();
    gui.setPlainText(" ");
    System.out.println("testing");
} 
}

The testing text is printing out to console but the JTextArea wont clear. I'm not sure as to why :). I am sure it's a very simple mistake but it has me baffled. Any help would be appreciated.

Thank you in advance.

Upvotes: 0

Views: 737

Answers (4)

JB Nizet
JB Nizet

Reputation: 692231

The btnClear method clears the text area of a new CrackerGUI instance. It's like if you wanted to clear a drawing on a sheet of paper by taking a new blank sheet and clearing it. The original sheet of paper will keep its drawing.

You need to pass the gui instance to your Shift:

public class Shift { 
    private CrackerGUI gui;

    public Shift(CrackerGUI gui) {
        this.gui = gui;
    }

    public void btnClear() {
        this.gui.setPlainText(" ");
    }
}

and in the CrackerGUI class :

private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Shift classShift = new Shift(this);
    classShift.btnClear(); 
}   

Upvotes: 3

GETah
GETah

Reputation: 21459

Assuming CrackerGUI is your GUI, you should have the following instead:

public class CrackerGUI {

  public void setPlainTextBox(String text)
  {
    plainTextBox.setText(text);
  }
  public void btnClear()
  {
    setPlainTextBox("");
    System.out.println("testing");
  } 
}

One last thing, never make your GUI elements public! You should ask the GUI to clear itself and leave that knowledge of clearing elements hidden inside it.

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285450

You're misusing inheritance to solve a problem that doesn't involve inheritance. Don't have Shift extend CrackerGUI and don't create a new CrackerGUI object inside of the btnClear() method since neither CrackerGUi is the one that's displayed. Instead have Shift hold a reference to the displayed CrackerGUI object and have it call a public method of this object.

e.g.,

public class Shift  {
  private CrackerGUI gui;

  // pass in a reference to the displayed CrackerGUI object
  public Shift(CrackerGUI gui) {
    this.gui = gui;
  }

  public void btnClear() {
    //CrackerGUI gui = new CrackerGUI();
    gui.setPlainText(" ");
    System.out.println("testing");
  } 
}

You also should probably not be creating new Shift objects in your GUI's actionPerformed methods, but rather use only one Shift object that is a class field.

Upvotes: 3

Matt
Matt

Reputation: 95

You could try using static methods, as you would end up creating a new gui, then displaying that one, in stead of the current one already displayed.

This would require the parent class to be static too, which may cause errors in some of your methods, just a heads up.

Or else, you could create your own setText method:

void setText(JTextField t, String s){
  t.setText(s);
}

that may enable you to directly edit components in the current GUI.

Upvotes: -2

Related Questions