Results 1 to 11 of 11
  1. #1
    nawl is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default Adding to TextArea

    I am trying to build an application with NetBeans, a simple address book.
    My problem is, I cant add text to a TextArea, outside of the main frame constructor. I added a ListSelectionListener and that works. But when I have the Listener call a method in my main class, to add text to the TextArea, it doesn't happen. I can however add elements to my JList with one of my methods(this method is called from the main Frame constructor, and by a button event). I was thinking it was due to the fact that the method gets called from the listener class, but i have no idea as to how to correct this. Any ideas?

    If the code is needed I will post, was just wondering if anyone had some tips off the top of their head.

    Thanks!

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

    Default

    Quote Originally Posted by nawl View Post
    My problem is, I cant add text to a TextArea, outside of the main frame constructor.

    probably because the reference of your TextArea is only known inside the main frame. if you want to manipulate the TextArea outside the main frame you could add a method example public TextArea getTextArea() that returns the reference of the TextArea. then you can manipulate it. post your code, perhaps there are other solutions.

  3. #3
    nawl is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post
    probably because the reference of your TextArea is only known inside the main frame. if you want to manipulate the TextArea outside the main frame you could add a method example public TextArea getTextArea() that returns the reference of the TextArea. then you can manipulate it. post your code, perhaps there are other solutions.
    I tried editing the code outside of NetBeans, and instantiated the JTextArea outside methods of the main frame class, and that did not work as well.. The method I am calling is inside the main frame class, but its being called from outside.
    Forgive me I just came off python, and this is a bit confusing to me.

    Java Code:
    class MyListSelectionListener implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent evt) {
            MainFrame mf = new MainFrame();
            if (!evt.getValueIsAdjusting()) {
                int index = evt.getFirstIndex();
                mf.showDetails(index);
            }
        }
    }
    Java Code:
    public MainFrame() {
            initComponents();
            initText();
        }
    
        public void initText() {
            ArrayList<Contact> contactList = new ArrayList<Contact>();
            jList1.setModel(listModel);
            listSelectionModel = jList1.getSelectionModel();
            listSelectionModel.addListSelectionListener(new MyListSelectionListener());
            try {
                contactList = contact.readFile();
            } catch (IOException evt) {
                evt.printStackTrace();
            }
            for (int i = 0; i < contactList.size(); i++) {
                addToList(contactList.get(i));
            }
        }
        public void showDetails(int selection) {
            jTextArea1.append("hello");
        }

  4. #4
    nawl is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    omg i think i got it
    im retarded
    i instantiated a new mainframe.. in the listener. how would I call the method I want to? If I'm correct..

  5. #5
    nawl is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    I guess that would go back to what you said earlier?

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

    Wink

    only one example. if you use this class structure

    Java Code:
    public class MainFrame extends JFrame implements ActionListener {
    
    	private TextArea textArea;
    	// other members
    
    	public MainFrame(bla bla) {
    		// initialize your instance variables
    		// and layout your jframe
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    	Object component = e.getSource();
    	// here you can use your textArea because
    	// it's a instance variable and the method can access it
    	}
    
    	public static void main(String[] args) {
    		TextField textField = new TextField();
    		TextArea textArea = new TextArea();
    		MainFrame frame = new MainFrame(textField, textArea);
    		frame.setSize(300, 300);
    		frame.setVisible(true);
    	}
    
    }
    hope this help.
    Last edited by j2me64; 05-20-2010 at 11:09 PM.

  7. #7
    nawl is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    This led me to the right solution, but how would I get the contents of the selection. e.getSource() returns the jList.

  8. #8
    nawl is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    The .getFirst and .getLastIndex() is not very trustworthy.. The index is always wrong when skipping around.

  9. #9
    nawl is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    oh my... i got it thank you

  10. #10
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by j2me64 View Post
    only one example. if you use this class structure

    Java Code:
    public class MainFrame extends JFrame implements ActionListener {
       //......
    }
    Myself, I tend to do things the opposite. I avoid extending JFrame unless I have reason to, and I avoid having my GUI class implement any listeners as this can force them to be very ugly and clunky listeners that get more convoluted and ugly as the GUI becomes more complex. Instead I try to expose only that which needs to be exposed and nothing more or less. For instance, if a GUI class holds JTextArea and I want to get the text held by the JTextArea, or append text into the JTextArea, then I give my class public methods to do so:

    Java Code:
    public String myTextAreaGetText() {
      reutrn myTextArea.getText();
    }
    
    public void myTextAreaAppend(String text) {
      myTextArea.append(text);
    }
    This way, I strictly control what can be done to my JTextArea from the outside world.


    As for the OP's problem, it seems to me to be more of a problem of using the right references. I'd change this:
    Java Code:
    class MyListSelectionListener implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent evt) {
            MainFrame mf = new MainFrame();
            if (!evt.getValueIsAdjusting()) {
                int index = evt.getFirstIndex();
                mf.showDetails(index);
            }
        }
    }

    To something like this, for starters:
    Java Code:
    class MyListSelectionListener implements ListSelectionListener {
       private MainFrame mf;  // holds a reference to the current visible GUI
    
      public MyListSelectionListener(MainFrame mf) {
         this.mf = mf;  // gets the reference and stores it.
      }
    
        public void valueChanged(ListSelectionEvent evt) {
            // MainFrame mf = new MainFrame();
            if (!evt.getValueIsAdjusting()) {
                int index = evt.getFirstIndex();
                mf.showDetails(index);
            }
        }
    }
    so that my listener is working with the current visible GUI and not some invisible GUI object that is created in the listener and promptly discarded.

    Suerte!
    Last edited by curmudgeon; 05-21-2010 at 02:15 AM.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by curmudgeon View Post
    Java Code:
    public String myTextAreaGetText() {
      reutrn myTextArea.getText();
    }
    
    public void myTextAreaAppend(String text) {
      myTextArea.append(text);
    }
    This way, I strictly control what can be done to my JTextArea from the outside world.
    That is a very good practice; it even has a (mysterious?) name: Demeter's Law. Google for it, there are quite some interesting links about it.

    kind regards,

    Jos

Similar Threads

  1. TextArea in jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 0
    Last Post: 01-26-2010, 05:27 AM
  2. Output to TextArea
    By ljk8950 in forum AWT / Swing
    Replies: 4
    Last Post: 08-03-2008, 09:59 AM
  3. TextArea with Unicode
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-16-2008, 10:53 PM
  4. TextArea Bug?
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 12:37 PM
  5. textarea
    By ubuntu in forum AWT / Swing
    Replies: 4
    Last Post: 05-12-2007, 09:54 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
  •