I would like to know how can I make my JTextArea
global and how can I use that to write in it with the result I get as an integer?
I am trying to do program for queues with linked link implementation but I don't actually use the LinkedList
class. I have two different classes in my project. There is one class which is deneme2
. In that class I have the queue methods. In the second class I have the JFrame
, so I would like to get my enqueue and dequeue result in JTextArea
. So far I could only use println
but can't manage to get it into the JTextArea
.
This is my deneme4
class
public class Deneme4 extends JFrame {
public static void main(String a[]) throws FileNotFoundException {
SecondFrame frame = new SecondFrame();
}}
This is the Queue
class for the GUI I tried to make
public class Queue {
public static interface MessageOutput {
void appendMessage(String message);
void appendHead(String message);
}
private MessageOutput msgOutput = new MessageOutput() {
@Override
public void appendMessage(String message) {
System.out.println(message);
}
@Override
public void appendHead(String head) {
System.out.println(head);
}
};
public void setMessageOutput(MessageOutput value) {
msgOutput = value;
}
public void setHeadOutput(MessageOutput value) {
msgOutput = value;
}
private Node front, rear;
private int currentSize;
private class Node {
int data;
Node next;
}
public Queue() {
front = null;
rear = null;
currentSize = 0;
}
public boolean isEmpty() {
if (currentSize == 0) {
msgOutput.appendMessage("Que is Empty\n");
}
return currentSize == 0;
}
public int dequeue() {
int data = front.data;
front = front.next;
if (isEmpty()) {
rear = null;
}
currentSize--;
msgOutput.appendMessage(data + " removed from the queue\n");
return data;
}
public int enqueue(int data) throws FileNotFoundException {
Node oldRear = rear;
rear = new Node();
rear.data = data;
rear.next = null;
if (isEmpty()) {
front = rear;
} else {
oldRear.next = rear;
}
currentSize++;
msgOutput.appendMessage(data + " added to the queue\n");
return data;
}
public int queueSize() {
msgOutput.appendMessage("Size of the Que is" + currentSize + "\n");
return currentSize;
}
public int getHead() {
int data = front.data;
msgOutput.appendHead("Head of the Que is " + data + "\n");
return data;
}}
and this is my QueueFrame which i want when the button clicked it outputs the value to txt1 but cant seem to do it
public class QueueFrame extends JFrame implements Queue.MessageOutput {
private JTextArea txt1;
private JTextArea txt2;
private JTextArea txt3;
private JButton b1;
private JButton b2;
private Queue queue = new Queue();
public static interface MessageOutput {
void appendMessage(String message);
void appendHead(String message);
}
private MessageOutput msgOutput = new MessageOutput() {
@Override
public void appendMessage(String message) {
System.out.println(message);
}
@Override
public void appendHead(String head) {
System.out.println(head);
}
};
public void setMessageOutput(MessageOutput value) {
msgOutput = value;
}
public void setHeadOutput(MessageOutput value) {
msgOutput = value;
}
@Override
public void appendHead(String head) {
txt2.append(head);
}
public QueueFrame() throws FileNotFoundException {
JFrame frame = new JFrame();
b1 = new JButton("Load up the Que");
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
Scanner s = new Scanner(new File("list.txt"));
while (s.hasNext()) {
queue.setMessageOutput((Queue.MessageOutput) queue.
queue.enqueue(s.nextInt());
}
s.close();
queue.queueSize();
queue.getHead();
} catch (FileNotFoundException ex) {
Logger.getLogger(QueueFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
b2 = new JButton("Head of the Que");
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
queue.getHead();
}
});
txt1 = new JTextArea();
txt2 = new JTextArea();
txt3 = new JTextArea();
txt1.setEditable(false);
txt2.setEditable(false);
txt3.setEditable(true);
b1.setBounds(50, 100, 180, 100);
b2.setBounds(50, 300, 180, 100);
txt1.setBounds(600, 100, 200, 600);
txt2.setBounds(300, 300, 180, 100);
txt3.setBounds(300, 100, 180, 100);
frame.add(b1);
frame.add(b2);
frame.add(txt1);
frame.add(txt2);
frame.add(txt3);
frame.setLayout(null);
frame.setSize(1000, 1500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}}
There are many possible solutions to this problem. This is the way that I would approach it:
Create an interface called MessageOutput inside the class deneme2 like this:
public class deneme2 {
public static interface MessageOutput {
void appendMessage(String message);
}
}
And in the same class, you need a reference to the MessageOutput so you can append message to it. And let's initialize it to some default implementation in case no one set it later:
private MessageOutput msgOutput = new MessageOutput() {
@override
public void appendMessage(String message) {
System.out.println(message);
}
};
And so you need a setter to the msgOutput field:
public void setMessageOutput(MessageOutput value) {
msgOutput = value;
}
And so you can now change all the println code to use MessageOutput's appendMessage() method:
public int dequeue() {
. . .
msgOutput.appendMessage(data + " removed from the queue");
return data;
}
Then let MainFrame implements deneme2.MessageOutput. Note that you can no longer use the JTextArea as a local variable, you must make it an attribute of MainFrame:
public class MainFrame implements deneme2.MessageOutput {
@override
public void appendMessage(String message) {
txt2.append(message);
}
}
And finally, update the main() method to pass the MainFrame to the deneme2 instance:
MainFrame frame = new MainFrame();
queue.setMessageOutput(frame);
And you should start processing the queue after the MainFrame is created.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.