Reputation: 13
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");
}
}
Upvotes: 0
Views: 2613
Reputation: 892
There are a few things wrong here.
test() is not found because there is no test function in the class that is trying to execute it. (sunday1)
test() in sundaytest is private, meaning no class outside of sundaytest can execute it.
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());
Upvotes: 1