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