hi everybody! i have this code for queues but it doesn't execute due to final Queue s = new Queue(); what should i do with this? pls help me :(

// Testing the Queue class of the java.util package
 import java.util.*;
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;

 public class QueueTest extends JFrame
 {
	 public QueueTest()
	 {
		 super( "Queue Trial ^_^" );

		 Container c = getContentPane();

		 final JLabel status = new JLabel();
		 __

		 c.setLayout( new FlowLayout() );
		 c.add( new JLabel( "Enter a string" ) );
		 final JTextField input = new JTextField( 10 );
		 c.add( input );

		 JButton enqueueBtn = new JButton( "Enqueue" );
		 enqueueBtn.addActionListener( new ActionListener()
		 {
			 public void actionPerformed( ActionEvent e )
			 {
				 status.setText( "Enqueued: " + s.enqueue( input.getText() ) );
			 }
		 } );
		 c.add( enqueueBtn );

		 JButton dequeueBtn = new JButton( "Dequeue" );
		 dequeueBtn.addActionListener( new ActionListener()
		 {
			 public void actionPerformed( ActionEvent e )
			 {
				 try
				 {
					 status.setText( "Dequeued: " + s.dequeue() );
				 }
				 catch ( EmptyStackException exception )
				 {
					 status.setText( exception.toString() );
				 }
			 }
		 } );
		 c.add( dequeueBtn );

		 JButton peekBtn = new JButton( "Peek" );
		 peekBtn.addActionListener( new ActionListener()
		 {
			 public void actionPerformed( ActionEvent e )
			 {
				 try
				 {
					 status.setText( "Top: " + s.peek() );
				 }
				 catch ( EmptyStackException exception )
				 {
					 status.setText( exception.toString() );
				 }
			 }
		 } );
		 c.add( peekBtn );

		 JButton emptyBtn = new JButton( "Is Empty?" );
		 emptyBtn.addActionListener( new ActionListener()
		 {
			 public void actionPerformed( ActionEvent e )
			 {
				 status.setText( s.isEmpty() ? "Queue is empty" : "Stack is not empty" );
			 }
		 } );
		 c.add( emptyBtn );

		 JButton searchBtn = new JButton( "Search" );
		 searchBtn.addActionListener( new ActionListener()
		 {
			 public void actionPerformed( ActionEvent e )
			 {
				 String searchKey = input.getText();
				 int result = s.search(searchKey);

				 if ( result == -1 )
					 status.setText( searchKey + " not found" );
				 else
					 status.setText( searchKey + " found at element " + result );
			 }
		} );
		c.add( searchBtn );

		 JButton displayBtn = new JButton( "Display" );
		 displayBtn.addActionListener( new ActionListener()
		 {
public void actionPerformed( ActionEvent e )
			 {
				 Enumeration en = s.elements();
				 StringBuffer buf = new StringBuffer();

				 while ( en.hasMoreElements() )
					 buf.append( en.nextElement() ).append( " " );

				 JOptionPane.showMessageDialog( null, buf.toString(), "Display", JOptionPane.PLAIN_MESSAGE );
			 }
		 } );
		 c.add( displayBtn );
		 c.add( status );

		 setSize( 675, 100 );
		 show();
	 }

	 public static void main( String args[] )
	 {
		 QueueTest app = new QueueTest();

		 app.addWindowListener( new WindowAdapter()
		 {
            @Override
			 public void windowClosing( WindowEvent e )
			 {
				 System.exit( 0 );
			 }
		 } );
	 }
 }

Where is the line final Queue s = new Queue(); in your code? I didn't find it. Also, I thought that Queue was an interface...

Anyway, try declaring your Queue and GUI elements as members of your QueueTest class. Then you should be able to access them from your ActionListeners using the QueueTest.this variable, which is implicitly declared for you inside your ActionListeners. See the code below for an example.

// Testing the Queue class of the java.util package
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class QueueTest extends JFrame
{
	Queue q = new ArrayDeque();
	
	JTextField textfield = new JTextField(20);
	JButton btnAdd = new JButton("Add To Queue");
	JButton btnGet = new JButton("List Queue");
	
	public QueueTest()
	{
		super("Queue Trial");

		JPanel pane = new JPanel(new FlowLayout());
		pane.add(textfield);
		pane.add(btnAdd);
		pane.add(btnGet);
		
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = QueueTest.this.textfield.getText();
				QueueTest.this.q.add(text);
			}
		});
		
		btnGet.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				StringBuilder buf = new StringBuilder();
				Iterator it = QueueTest.this.q.iterator();
				while (it.hasNext())
					buf.append(it.next()).append(", ");
				JOptionPane.showMessageDialog(null, buf.toString(), "Display", JOptionPane.PLAIN_MESSAGE);
			}
		});
		
		add(pane);

		setSize( 675, 100 );
		show();
	}

	public static void main( String args[] )
	{
		QueueTest app = new QueueTest();

		app.addWindowListener( new WindowAdapter() {
			@Override
			public void windowClosing( WindowEvent e ) {
				System.exit( 0 );
			}
		} );
	}
}

hi thanks for ur reply i didnt include Queue q = new Queue();

i tried ur code but still it has error
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
required: Queue
found: java.util.ArrayDeque
at QueueTest.<init>(QueueTest.java:9)
at QueueTest.main(QueueTest.java:49)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

It compiled and ran just fine for me. I was using Java 6 compiler. Which Java version are you using? Also, are you using a custom implementation of the Queue class? Or are you using the java.util.Queue?

hey i changed Queue q = new ArrayDeque() to PriorityQueue q = new PriorityQueue()

i used remove() method.. thanks

but what if i want to add a button clear?
what method should i use then?

This article has been dead for over six months. Start a new discussion instead.