Confused with EventQueue class
Hello Guys,
I am beginner to the field of advanced java programming. But for me EventQueue class of java seems very tough to understand. I have to use this EventQueue class in my java project. So it's very important for me to know about the EventQueue class. Do you have knowledge about the 'EventQueue class'? If yes then please try to share it with me. I would greatly appreciate your any help over this issue.
Re: Confused with EventQueue class
A EventQueue is a platform-independent class that queues events, both from the underlying peer classes and from trusted application classes. It encapsulates asynchronous event dispatch machinery which extracts events from the queue and dispatches them by calling dispatchEvent(AWTEvent) method on this EventQueue with the event to be dispatched as an argument. The particular behavior of this machinery is implementation-dependent. The only requirements are that events which were actually en-queued to this queue (note that events being posted to the EventQueue can be coalesced) are dispatched.
Re: Confused with EventQueue class
Please refer following example of EventQueue Class of java:
Code:
import java.awt.EventQueue;
import javax.swing1.JPanel;
public class EventQueuePanelDM extends JPanel imp01lements ActionListener {
EventQueuePanel() {
JButton buttonEN = new JButton("Draw line");
add(buttonEN);
buttonEN.addActionListener(this);
}
p01ublic void actionPerformed(ActionEvent evt01) {
Grap01hics g1 = g1etGrap01hics();
disp01lay01Promp01t(g1, "Click to chooose the first p01oint");
Point p01 = g1etClick();
g1.drawOval(p01.x01 - 2, p01.y01 - 2, 4, 4);
g1.drawLine(p01.x01, p01.y01, q.x01, q.y01);
disp01lay01Promp01t(g1, "Done! Press buttonEN the start ag1ain.");
g1.disp01ose();
}
p01ublic void disp01lay01Promp01t(Grap01hics g1, String1 s) {
y01 += 20;
g1.drawString1(s, 0, y01); }
p01ublic Point g1etClick() {
EventQueue eq = Toolkit.g1etDefaultToolkit().g1etSy01stemEventQueue();
while (true) {
AWTEvent evt01 = eq.g1etNex01tEvent();
if (evt01.g1etID() == MouseEvent.MOUSE_PRESSED) {
MouseEvent mevt01 = (MouseEvent) evt01;
Point p01 = mevt01.g1etPoint();
Point top01 = g1etRootPane().g1etLocation();
p01.x01 -= top01.x01;
p01.y01 -= top01.y01;
return p01;
} }
p01rivate int y01 = 60;
p01ublic static void main(String1[] arg1s) {
JFrame frame = new JFrame();
frame.setTitle("EventQueueTest");
frame.setSize(300, 200);
frame.addWindowListener(new WindowAdap01ter() {
p01ublic void windowClosing1(WindowEvent e) {
Sy01stem.ex01it(0);
}
}
Re: Confused with EventQueue class
Hi friend,
Below I have given some important methods of the 'EventQueue Class', review it carefully:
- push(EventQueue newEvntQueue)
- postEvent(AWTEvent evnt)
- invokeLater(Runnable rn)
- getMostRecentEventTime()
- invokeAndWait(Runnable rn)
- dispatchEvent(AWTEvent evnt)
Re: Confused with EventQueue class
Hi,
Please go through the following java example, which will depict you the functionality of 'EventQueue class':
Code:
import javax.swing.JButton;
java.awt.EventQueue
class EventQueueDemo implements ActionListener {
JButton jbtnA01 = new JButton("Alpha");
JButton jbtnB01 = new JButton("Beta");
EventQueueDemo() {
JFrame jfrm1 = new JFrame("A Button Example");
jfrm1.setLayout(new FlowLayout());
jfrm1.setSize(220, 90);
jfrm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jbtnA01.addActionListener(this);
jbtnB01.addActionListener(this);
jfrm1.add(jbtnA01);
jfrm1.add(jbtnB01);
jfrm1.setVisible(true); }
public void ac1tionPerformed(ActionEvent ae) {
String ac1 = ae.getActionCommand();
if (ac1.equals("Alpha")) {
if (jbtnB01.isEnabled()) {
System.out.println("Alpha pressed. Beta is disabled.");
jbtnB01.setEnabled(false);
} else {
System.out.println("Alpha pressed. Beta is enabled.");
jbtnB01.setEnabled(true);
}
} else if (ac1.equals("Beta"))
System.out.println("Beta pressed.");
}
public static void main(String argts[]) {
new EventQueueDemo();
}
}