Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-11-2012, 02:41 PM   PM User | #1
jimutt
New Coder

 
Join Date: Sep 2011
Posts: 47
Thanks: 1
Thanked 15 Times in 14 Posts
jimutt is an unknown quantity at this point
Accessing JTextArea from another class

Hi, I've been programming for quite a long time in PHP and other web programming languages but I'm really new to Java. And as I've been using a procedural approach when programming in PHP I'm quite new to OOP as well. Now I'm following a very basic Java tutorial.

I have this code for displaying to different "bank accounts":

Code:
public class UseAccount extends JFrame {

public static void main(String[] args) {
    
    Account myAccount = new Account();
    Account yourAccount = new Account();
    
    myAccount.name = "Jimmy";  
    myAccount.address = "Arjeplogsvägen 1";
    myAccount.balance = 1250.70;
    
    yourAccount.name = "Greg Giraldo";
    yourAccount.address = "Fishermans friend's 4";
    yourAccount.balance = -5820.30;
    
    myAccount.display();
    System.out.println();
    yourAccount.display();
}
}
And here is the "Account" class:

Code:
public class Account{
    String name;
    String address;
    double balance;
    
    void display() {
        System.out.print(name);
        System.out.print(" (");
        System.out.print(address);
        System.out.print(") has $");
        System.out.print(balance);
    }     
}
This works really well. but now I want to output this information to a JTextArea. So I've written this code for the UseAccount class:

Code:
import java.awt.*;
import javax.swing.*;

public class UseAccount extends JFrame {
        JTextArea output = new JTextArea();
    
    public UseAccount() {
        setLayout(new BorderLayout());
        add(output, BorderLayout.CENTER);
    }
    
    public static void main(String[] args) {
    UseAccount frame = new UseAccount();
    frame.setTitle("Account");
    frame.setSize(500,400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    
    Account myAccount = new Account();
    Account yourAccount = new Account();
    
    myAccount.name = "Jimmy";  
    myAccount.address = "Arjeplogsvägen 1";
    myAccount.balance = 1250.70;
    
    yourAccount.name = "Greg Giraldo";
    yourAccount.address = "Fishermans friend's 4";
    yourAccount.balance = -5820.30;
    
    myAccount.display();
    System.out.println();
    yourAccount.display();
    }
}
And then I was trying to make the "Account" class extend the "UseAccount" class and then use output.append("the_text") for displaying the text. But this obviously doesn't work:

Code:
public class Account extends UseAccount{
    String name;
    String address;
    double balance;
    
    void display() {
        output.append(name);
        output.append(" (");
        output.append(address);
        System.out.print(") has $");
        System.out.print(balance);
    }     
}
(I did not change every system.out.print() to output.append as it isn't working anyway.

I'm wondering how to access and change the text of my textarea("output") from this other class?

I hope someone will be able to help me with this little problem.

Last edited by jimutt; 06-11-2012 at 03:04 PM..
jimutt is offline   Reply With Quote
Old 06-11-2012, 06:43 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Don't. Keep them separate, one to present the output, and one to process the data.
There are several routes you can go with though. You can take a Document type, or a StringBuilder and append to that for example, or you can simply return a string. I'd do neither and opt for using single property accessor's instead, but if I were to group them I'd return a string:
PHP Code:
public String display()
{
    
StringBuilder sb = new StringBuilder();
    
sb.append(name);
    
sb.append(" (");
    
sb.append(address);
    
sb.append(") has $");
    
sb.append(balance);
    return 
sb.toString();
}

// Used
jtText.setText(object.display()); 
Unfortunately I don't believe there is a really generic interface to work with when it comes to the swing JTextArea. Document I think is the lowest you can really go, and its still a javax.swing.text type. There's some patterned approaches you can follow as well, but those are more advanced and require a good understanding of OOP to implement.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
jimutt (06-14-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:30 PM.


Advertisement
Log in to turn off these ads.