Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Make JTextArea act as console?

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question Make JTextArea act as console?

    Hello everyone,
    I was playing around with Java Swing since i wanted to learn more about it. I have 2 .java files as follows:

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
     
    public class Easy extends JFrame{
     
    JTextArea text=new JTextArea();
    JPanel panel=new JPanel(new GridLayout(2,2));
     
    JButton button1 =new JButton("Start");
     
    public Easy(){
     
    panel.add(text);
     
    panel.add(button1);
    add(panel,BorderLayout.CENTER);
     
    button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae){
    //add code to call the other class and make the JTextArea act as a console
     
     
    }
    });
    }
     
    public static void main(String arg[]){
    Easy frame=new Easy();
    frame.setSize(300,100);
    frame.setVisible(true);
    }
    }

    import java.util.Scanner;
     
    class AddNumber
    {
       public static void main(String args[])
       {
          int x, y, z;
          System.out.println("Enter two numbers to be added ");
          Scanner in = new Scanner(System.in);
          x = in.nextInt();
          y = in.nextInt();
          z = x + y;
          System.out.println("Sum of entered numbers = "+z);
       }
    }

    Both codes work fine individually. Now, when I run the file Easy, and click on the "Start" button, I want the class AddNumber to be implemented. I mean to say that, instead of the AddNumber running on the console, is there any way I could make AddNumber run in the JTextArea i have created in the first class upon clicking the "Start" button? I thought maybe by action listener? But I'm not sure. Is there any other way to make my JTextArea act as a console for the other .java file?

    Thanks


  2. #2
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Make JTextArea act as console?

    Your AddNumber class interacts with the console, so it doesn't matter where it is called, it will do just that. You're on the right path though for making your JTextArea resemble a console. Before tackling this problem, think about what the console does. For example, it can take in lots of input at once and process it. Likewise, it can output lots of data at once. You're going to have to use some form of I/O, such as an InputStream, OutputStream, BufferedReader, BufferedWriter, PipedInputStream, etc... . Next, you're going to have to tell your compiler that it should re-direct standard output from the console to a custom I/O. This can be done using the method System.setOut(PrintStream stream), where stream will encompass whatever I/O you've chosen. Similarly, you'll do the same for take in information using System.setIn(InputStream inStream). Next, you're going to have to override event listeners so when you press Enter, it will take in/print out information and move to the next line.

    The second last step would be to make sure your program will constantly execute. You can use a Thread for this or a SwingWorker (similar to a Thread but designed for javax.swing components, which is what you're using).

    The last step would be to adjust the fonts, background colour, foreground colour and make it look like a console.

    Overall, if I were you, I'd start on something simpler and work your way up to this.

Similar Threads

  1. How to make JTextArea size thesame with its splitpane
    By saniadeyi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 26th, 2013, 11:13 AM
  2. How to make console output bold
    By ColeTrain in forum Java Theory & Questions
    Replies: 6
    Last Post: October 16th, 2012, 09:54 AM
  3. How do you make a console program a .jar in Eclipse?
    By Programmer142 in forum Java Theory & Questions
    Replies: 9
    Last Post: January 13th, 2012, 12:13 AM
  4. [SOLVED] Make JTextArea Support HTML
    By bgroenks96 in forum AWT / Java Swing
    Replies: 12
    Last Post: August 28th, 2011, 01:01 PM
  5. Make JTextArea accept input from JButton
    By crazed8s in forum AWT / Java Swing
    Replies: 9
    Last Post: December 5th, 2010, 09:38 PM

Tags for this Thread