2 Replies - 546 Views - Last Post: 13 March 2015 - 01:29 PM Rate Topic: -----

#1 owl2122   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-March 15

How to add user input from a textfield and add to a queue

Posted 13 March 2015 - 01:07 PM

I currently have a GUI that asks a user to:

1) Enter a word into a text field
2) Select one of two radio buttons
3) Click "next" or click "exit"
... It also has a display area

I also have a class that contains a queue.

I currently am able to display in the display area:
1) The contents of the queue... which is "null"
2) Some text that I added in to do some checking

What I want next is for the user input from the text field to be added to my queue.
Everything compiles and all buttons work, but I just cannot figure out how to add the user input to my queue.

Here is my class code:

public class MyClass
{
   private Queue oneQ;
   public MyClass(String oneQ)
   {
      
   }

   public String getQ()
   {
      Queue<String> oneQ = new LinkedList<String>();
      return oneQ.peek();
   }
   public String toString()
   {
      return "This queue: " + oneQ;
   }
 } 
  



Here is my GUI information.... No I did not include the actual GUI format, and I won't unless absolutely necessary to get help. I only need help understanding this queue issue. The problem comes from the ActionListener, as I want input to be added to the queue when user clicks "Go".

public class MyFrame extends JFrame 
{
   private JPanel Panel1;
   private JPanel Panel2;
   private JLabel enterWord;
   private JRadioButton one, two;
   private JButton go, exit;
   private JScrollPane ScrollPane;
   private JTextField input;
   private JTextArea TextArea;
   private Listener listener;
    
    
   public MyFrame()
      {
         listener = new Listener();
         this.setTitle("Title");
         this.createComponents();
         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
         this.setVisible(true);
      }
      
private void createComponents()
      {
        Panel1 = new JPanel();
        enterWord = new JLabel("Enter a Word:");
        input = new JTextField();
        one = new JRadioButton("One");
        two = new JRadioButton("Two");
        go = new JButton("Go");
        go.addActionListener(listener); 
        exit = new JButton("Exit");
        exit.addActionListener(listener); 
        Panel2 = new JPanel();
        ScrollPane = new JScrollPane();
        TextArea = new JTextArea(5, 20);

class Listener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         if(e.getActionCommand().equals("Go") && one.isSelected())
         {
            MyClass temp = new MyClass(input.getText());

            //oneQ.add(temp); .... tried this among other things that did not work

            TextArea.append(temp.getQ());     // this will display "null" in text area
            TextArea.append(temp.toString()+"\n");    // this will display "This queue: null"
            System.out.println("Test");      //only added earlier to verify button was working
         }
         
         if (e.getActionCommand().equals("Exit")) 
         {System.exit(0);}
       }
    }
}




Any help or insight is greatly appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: How to add user input from a textfield and add to a queue

#2 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: How to add user input from a textfield and add to a queue

Posted 13 March 2015 - 01:24 PM

09	   public String getQ()
10	   {
11	      Queue<String> oneQ = new LinkedList<String>();
12	      return oneQ.peek(); 
13	   }
This method creates a new(empty) queue and returns the peek element from it(null), why not just return from the queue you defined earlier?

public String getQ()
{
   return oneQ.peek();
}

This post has been edited by mike73: 13 March 2015 - 01:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 owl2122   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-March 15

Re: How to add user input from a textfield and add to a queue

Posted 13 March 2015 - 01:29 PM

View Postmike73, on 13 March 2015 - 01:24 PM, said:

09	   public String getQ()
10	   {
11	      Queue<String> oneQ = new LinkedList<String>();
12	      return oneQ.peek(); 
13	   }
This method creates a new(empty) queue and returns the peek element from it(null), why not just return from the queue you defined earlier?

public String getQ()
{
   return oneQ.peek();
}


But I did not define a queue prior to that? Will this enable me to add input to the queue?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1