Results 1 to 6 of 6
- 08-22-2010, 07:03 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
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:
Here are the questions: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); } }); } }
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!
- 08-22-2010, 07:42 PM #2
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
First, when dealing with java classes you should check the api: All Classes (Java 2 Platform SE v1.4.2)
- 08-22-2010, 07:44 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
What specifically should I be looking for there? That page has a lot of links on it, to say the least.
- 08-22-2010, 07:49 PM #4
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
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.
-
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(...)
- 08-22-2010, 09:04 PM #6
Similar Threads
-
Top level and member level
By Differintegral in forum New To JavaReplies: 1Last Post: 07-30-2010, 03:50 AM -
JTable vs JTextArea scrolling text question
By adonos in forum AWT / SwingReplies: 2Last Post: 05-24-2010, 08:15 PM -
Accessing non-static fields from another class, before object creation
By mlad in forum New To JavaReplies: 3Last Post: 03-24-2010, 12:24 AM -
[SOLVED] Class-level vs Object-level method()
By mfaizan24 in forum New To JavaReplies: 7Last Post: 06-23-2009, 09:18 AM -
Accessing instance outside of creation method
By meringue in forum New To JavaReplies: 6Last Post: 03-22-2009, 01:36 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks