Results 1 to 3 of 3
  1. #1
    doha786 is offline Member
    Join Date
    Mar 2009
    Posts
    48
    Rep Power
    0

    Arrow need help to call another class

    hi,
    some examples i found online to call another class but during practice i stuck in GUI once i call another class. hope somebody can help me pls..

    i have one class name 'nestedLoop' that makes output: 60
    Java Code:
    public class nestedLoop {
    
    public static void main(String[] args) {
    
    int sum=0 ;
    for (int i=0; i<5; i++) { sum+= i;
    for (int j=0; j<5; j++) {sum =sum+j;} }
    System.out.print(sum);
    }
    }
    and now i have a GUI where has button 'showResult' to show that output in TextArea..
    Java Code:
    public void actionPerformed(ActionEvent e) {
    if (e.getSource()==showResult)
    nestedLoop np= new nestedLoop();
    
    {txtArea.setText(np.getText); }
    what should i write instead of txtArea.setText(np.getText) ??
    what we are thinking, it might not be true

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    If you want the text to go to a text area then you need a method that returns a String rather than one that prints to System.out. You then call on that method to give you the String which you set onto your text area.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    hallo doha786

    i think your design is weak. my suggestion is to create a class with the ActionListener and to pass the JTextField and the sum as constructor arguments so that the constructor can implement your requirement with JTextField.addText(sum).

Similar Threads

  1. How can I call method from class in other class??
    By Hisham in forum New To Java
    Replies: 6
    Last Post: 02-14-2010, 03:49 PM
  2. How do I call a class in jsp page
    By Greg in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 09-20-2009, 10:27 PM
  3. How to call a method from another class?
    By jboy in forum New To Java
    Replies: 8
    Last Post: 09-09-2009, 07:29 AM
  4. How to call a class within a method
    By Manfizy in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 12:34 PM
  5. Can I call directly the *.class
    By doanhanam in forum New To Java
    Replies: 1
    Last Post: 11-16-2008, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •