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 7 of 7

Thread: Troubles Capturing Output into GUI JTextArea Program Going haywire

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Troubles Capturing Output into GUI JTextArea Program Going haywire

    Hello,
    So I'm pretty much done with this project except I am unable to get the println statements of "Produced" & "Consumed" to display in my JTextArea on my GUI. I am completely lost as to exactly why the program just continues to create new windows repeatedly crashing and was hoping someone could point out what I'm doing wrong. I will include the code to the 3 classes I'm working with - I'm capturing with the fac.println("") on both consumer and producer and have a method in the Factory class that tells it to print in JTTextArea

    import java.util.Date;
    public class Producer implements Runnable
    {
        private Buffer<Date> buffer;
        private Factory fac  = new Factory();
        public Producer(Buffer<Date> buffer) 
        {
            this.buffer = buffer;
        }
     
        public void run() {
            Date message;
     
            while (true) {
                // nap for awhile
                SleepUtilities.nap();
     
                // produce an item & enter it into the buffer
                message = new Date();
                buffer.insert(message);
                fac.println("Produced");
            }
        }
    }

    import java.util.Date;
    public class Consumer implements Runnable
    {
        private Buffer<Date> buffer;
        private Factory fac  = new Factory();
        public Consumer(Buffer<Date> buffer) {
           this.buffer = buffer;
        }
     
        public void run() {
            Date message;
            while (true) {
                // nap for awhile
                SleepUtilities.nap();
     
                // consume an item from the buffer
                message = (Date)buffer.remove();
                fac.println("Consumed");
            }
        }
    }

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.color.*;
    import java.util.Date;
    public class Factory extends JFrame
    {
        private JTextArea textArea;
        private JTextField textInput;
        Buffer<Date> buffer;
        public Factory()
        {
            super("Lab 3");
            makeFrame();
     
            Buffer<Date> buffer = new BoundedBuffer<Date>();
     
            // Create the producer and consumer threads
            Thread producer = new Thread(new Producer(buffer));
            Thread consumer = new Thread(new Consumer(buffer));
     
            producer.start();
            consumer.start();
        }
     
        public void makeFrame()
        {
            makeMenuBar();
            makeContentPane();
     
            this.pack();
            this.setVisible(true);
        }
     
        public void makeMenuBar()
        {
            JMenuBar menuBar = new JMenuBar();
            this.setJMenuBar(menuBar);
     
            JMenu file = new JMenu("File");
            menuBar.add(file);
     
            JMenuItem quit = new JMenuItem("Quit");
            quit.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {quit();}
                });
            file.add(quit);
     
            JMenu help = new JMenu("Help");
            menuBar.add(help);
     
            JMenuItem about = new JMenuItem("About");
            about.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) { about();}
                });
            help.add(about);
     
        }   
     
        public void makeContentPane()
        {
            JPanel contentPane = (JPanel) this.getContentPane();
            contentPane.setBorder(new EmptyBorder(8, 8, 8, 8));
            contentPane.setLayout(new BorderLayout(6, 6));
     
            textArea = new JTextArea(15, 30);
            textArea.setBorder(new EtchedBorder());
            textArea.setEditable(false);
     
            JScrollPane scrollPane = new JScrollPane(textArea);
            contentPane.add(scrollPane, BorderLayout.CENTER);
     
            JPanel textPanel = new JPanel();
            textPanel.setBorder(new LineBorder(Color.BLACK));
            textPanel.setLayout(new BorderLayout(6, 6));
     
            textInput = new JTextField();
     
            textPanel.add(new JLabel("Enter Time:"), BorderLayout.WEST);
            textPanel.add(textInput, BorderLayout.CENTER);
            contentPane.add(textPanel, BorderLayout.SOUTH);
     
            JPanel butCont = new JPanel();
            butCont.setLayout(new FlowLayout());
     
            textPanel.add(butCont, BorderLayout.SOUTH);
     
            JButton conButton = new JButton("Set Consumer");
            conButton.setPreferredSize(new Dimension(150,25));
            conButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {setCon();}
                });
            butCont.add(conButton, BorderLayout.WEST);
     
            JButton proButton = new JButton("Set Producer");
            proButton.setPreferredSize(new Dimension(150,25));
            proButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {setPro();}
                });
            butCont.add(proButton, BorderLayout.EAST);
        }
     
        public void quit()
        {
            int option = JOptionPane.showConfirmDialog(this, 
                    "Are you sure you want to quit?", "Quit Program", 0);
            if(option == JOptionPane.YES_OPTION) {
                System.exit(0);
            }
            else{
     
            }
        }
     
        public void about()
        {
     
        }
     
        public void println(String str)
        {
            textArea.setText(str);
            textArea.setCaretPosition(textArea.getText().length());
        }
     
        public void setCon()
        {
            if( textInput.getText().equals("") || textInput.getText() == null ){
                textArea.setText( "Null not allowed" );
            }
            else{
                int tmp = Integer.parseInt( textInput.getText() );
                buffer.setCSleep( tmp );
                textArea.setText( "Consumer Sleep is now: " + Integer.toString(tmp) );
            }
        }
     
        public void setPro()
        {
            if( textInput.getText().equals("") || textInput.getText() == null ){
                textArea.setText( "Null not allowed" );
            }
            else{
                int tmp = Integer.parseInt( textInput.getText() );
                buffer.setPSleep( tmp );
                textArea.setText( "Producer Sleep is now: " + Integer.toString(tmp) );
            }
        }   
     
        public static void main(String args[]) 
        {
     
        }
    }

    Any help would be appreciated.
    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Troubles Capturing Output into GUI JTextArea Program Going haywire

    How do you test the program? The only main() method I see is empty.

    program just continues to create new windows
    Do you mean the new instances of the Factory class are being created?
    Where are the new statements that create instances of that class? Why are they being executed more than one time?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Troubles Capturing Output into GUI JTextArea Program Going haywire

    When you create an instance of your class "Factory" then you will also create 1 instance of Producer, and 1 instance of Consumer.
    This is done in the following lines of code:
            Thread producer = new Thread(new Producer(buffer));
            Thread consumer = new Thread(new Consumer(buffer));
    But the constructors of Producer and Consumer create new instances of Factory.
    This is an endless loop. Everytime a Factory object is created it will create Producer and Consumer objects. And everytime a Producer or Consumer object is created they will each create a new Factory object.

    The problematic line in your Producer and Consumer class is:
        private Factory fac  = new Factory();
    The variable "fac" will be instanciated once the constructor for Producer / Consumer is called.

  4. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Troubles Capturing Output into GUI JTextArea Program Going haywire

    Norm: I use BlueJ to run all my code as of now.
    Cornix: thank you, I have been messing around with this and I did in fact figure out when I removed it all and just put Factory fac; It prints where it needs to but keeps making new windows, I think its the Data stream that's outputting causing issues, So I'm thinking I need to create input/output streams instead of the way I was doing it with standard println

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Troubles Capturing Output into GUI JTextArea Program Going haywire

    Ok, I'll leave it to you and BlueJ. I don't use BlueJ.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Troubles Capturing Output into GUI JTextArea Program Going haywire

    BlueJ is just a simple console we use at school to code, the code is no different there are just a few conveniences of running classes without the need of a main. The problem I think is with setting input and output streams for the data the consumer and producer is outputting, if i remove the capture the program does exactly as needed but prints to the Terminal whereas I was simply just trying to redirect it to my JTextArea so I'm thinking something like

    PrintWriter pout = new PrintWriter(Factory.prinln(), true);

    I guess I'll keep playing with it, but if you know of a correct way to place that around my code any help would be appreciated

    Thanks

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Troubles Capturing Output into GUI JTextArea Program Going haywire

    I can't compile and execute the code for testing without a main() method and all the needed classes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Show output on jTextArea
    By HugoMonteiro in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 18th, 2012, 07:35 PM
  2. GUI temp converter program troubles
    By copelandtml in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 14th, 2011, 10:28 AM
  3. JTextArea output problem
    By grimx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 8th, 2010, 07:54 PM
  4. [SOLVED] how to print for-loop output to JTextArea
    By voltaire in forum AWT / Java Swing
    Replies: 2
    Last Post: May 13th, 2010, 02:43 PM
  5. **Help!** Capturing external circuit data to GUI Jpanel
    By ROTNEVNI in forum AWT / Java Swing
    Replies: 2
    Last Post: November 27th, 2009, 09:40 AM