Results 1 to 6 of 6
  1. #1
    jferrara is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default Accessing a JTextArea after creation (Intro level question)

    Hi,

    First: I'm new to this. I've searched around a bit but think a human might be able to answer my question better than the search engines so far.

    So I understand that you create a class representing your GUI, add a bunch of controls to it, then start running it. My question is twofold: one relates to structure, one relates to how to access a text box.

    So here's the code:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class JCheck extends JFrame {
        static String msgStuff;
        private JTextArea txtBox;
        
        private JCheck() {
            setTitle("Test Window");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(1000, 600);
            JLabel emptyLabel = new JLabel("Blargle");
            emptyLabel.setPreferredSize(new Dimension(175, 100));
            getContentPane().add(emptyLabel, BorderLayout.NORTH);
    
            txtBox = new JTextArea(5, 20);
            txtBox.setRows(20);
            txtBox.setColumns(20);
            txtBox.setLineWrap(true);
            txtBox.setBorder(BorderFactory.createLineBorder(Color.black));
            JScrollPane scrollPane = new JScrollPane(txtBox);
            getContentPane().add(scrollPane, BorderLayout.SOUTH);
            
            pack();
            showText();
        }
        private void showText() {
            System.out.println(this.txtBox.getText());
        }
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new JCheck().setVisible(true);
    
                }
            });
        }
    }
    Here are the questions:
    1. Let's say I want to open a file and input all the contents into the JTextArea "txtBox". How do I access that control to add text to it?
    2. Where should I do that? To clarify: what method should I add or utilize to do that. I don't think it should be in the constructor JCheck() if I insert multiple files - should I create another function to do this? And where should I call it from? The run() function?

    I know these are fairly entry level questions but I'd like to set myself straight before I start writing poorly formed Java code and any references you can point me to would be awesome too.

    Thanks for your time!

  2. #2
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    First, when dealing with java classes you should check the api: All Classes (Java 2 Platform SE v1.4.2)

  3. #3
    jferrara is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default

    What specifically should I be looking for there? That page has a lot of links on it, to say the least.

  4. #4
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    It has all the java provided classes in it. The class you have trouble with is the JTextArea class, so thats the one you need to click.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The API is critical, yes, but I think more important for you is to go through some of the Swing tutorials before trying to guess how to code it as much of what you seek to learn can be found there:

    Using Swing Components
    Using Text Components

    To answer your question, to read text from a file into a text component such as a JTextArea, you use the read method that the text component has available. The API can help with this: JTextComponent#Read(...)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

Similar Threads

  1. Top level and member level
    By Differintegral in forum New To Java
    Replies: 1
    Last Post: 07-30-2010, 03:50 AM
  2. JTable vs JTextArea scrolling text question
    By adonos in forum AWT / Swing
    Replies: 2
    Last Post: 05-24-2010, 08:15 PM
  3. Replies: 3
    Last Post: 03-24-2010, 12:24 AM
  4. [SOLVED] Class-level vs Object-level method()
    By mfaizan24 in forum New To Java
    Replies: 7
    Last Post: 06-23-2009, 09:18 AM
  5. Accessing instance outside of creation method
    By meringue in forum New To Java
    Replies: 6
    Last Post: 03-22-2009, 01:36 AM

Posting Permissions

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