0

I have a project that I'm a bit stuck on. I have a JFrame with a JTextArea and a button when the button is clicked it is to open the test class in the text area of the frame. So my question is, how do I get the test class to open in the text area of the frame?

I have tried getText(test()) but it say that can not find symbol method test.

This is my JFrame code:

package sunday;

/**
 *
 * @author warwick
 */
public class sunday1 extends javax.swing.JFrame {
/**
 * Creates new form sunday1
 */
public sunday1() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    TxtField = new javax.swing.JTextArea();
    btnOpen = new javax.swing.JToggleButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    TxtField.setColumns(20);
    TxtField.setRows(5);
    jScrollPane1.setViewportView(TxtField);

    btnOpen.setText("Open");
    btnOpen.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnOpenActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btnOpen)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(57, 57, 57))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(52, 52, 52)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(101, 101, 101)
                    .addComponent(btnOpen)))
            .addContainerGap(36, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {                                        

    TxtField.setText(test());
}                                       

This is my class code that has to display in the text area of the frame.

public class sundaytest {
  private void test() {
    System.out.println("Hello World");
  }  
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
warwick
  • 13
  • 1
  • 8
  • 1) Words typed in all lower case are hard to read, like trying to listen to someone who is mumbling. Please use an upper case letter at the start of sentences, for the word I, and proper names like `ArrayList` or Oracle. 2) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. 3) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Dec 21 '14 at 05:37
  • 1
    Turn your problem around, have your "test" class provide a means by which frame class can extract that information it needs and then have the frame class display it within the text area – MadProgrammer Dec 21 '14 at 05:45
  • 1
    The `sundaytest` class is designed to dump output to the `System.out` output stream. While it is possible to redirect an output stream, it would be better to redesign the class to have a `getString()` or `getMessage()` method that returns `"Hello World"`.. Then an instance of it can be used to supply text for the text area in the GUI, or the `System.out` as needed. – Andrew Thompson Dec 21 '14 at 05:46
  • For redirecting output to a `JTextArea`, have a look at [this example](http://stackoverflow.com/questions/12945537/how-to-set-output-stream-to-textarea/12945678#12945678) – MadProgrammer Dec 21 '14 at 05:51

1 Answers1

1

There are a few things wrong here.

  1. test() is not found because there is no test function in the class that is trying to execute it. (sunday1)

  2. test() in sundaytest is private, meaning no class outside of sundaytest can execute it.

  3. test() is a void method which would not return the String object that TxtField.setText(String str) is looking for.

To remedy these issues you can do the following:

a. Modify your method btnOpenActionPerformed to set the string without utilizing a separate class (sundaytest) like so:

TxtField.setText("Text to be set");

Or

b. Modify your test() method in sundaytest to be accessible from sunday1 and return a String object when executed.

public String test() {
    return "Hello World";
}

And modify btnOpenActionPerformed to access this function via sundaytest.

TxtField.setText(new sundaytest().test());
John Cipponeri
  • 882
  • 8
  • 12