0

What I need to happen is when the user inputs text into a JTextField and then 'Enter' is pressed, that text is added to an arraylist. What actually happens is no data is added to arraylist. Here some code below.

public class browser extends JFrame{
public boolean actionState = false;
private JPanel panel;
protected JTextField field;

public browser(){
   panel = new JPanel();
   field = new JTextField("Enter a URL"); 
   panel.add(field, BorderLayout.NORTH);
   actionListenerCalls();
}

private void actionListenerCalls(){

        //action performed when user types url into input box and presses Enter key
        field.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        loadData(e.getActionCommand());
                        actionState = true;
                        }
                    }
                );
    }

Inside of another class I have the arraylist. When actionSate becomes true the addURL() method should be called.

private ArrayList<String> array new ArrayList<String> ();

    //constructor
    public buttons(){
        if(actionState == true || hyperlinkState == true){
             addURL(field);
            System.out.println("URL added");
         }
    }

    //method for adding text inside of field to arraylist
    public void addURL(JTextField field){
     array.add(field.getText());
    }

Constructor browser and buttons are called inside of another class.

public class readFile{
    public static void main(String args[]){
        browser file = new browser();
        file.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

        buttons b = new buttons();


    }
}
3
  • Did u check actionState or hyperlinkState true. Or both false Commented May 23, 2014 at 7:10
  • It would be easier to answer your question with a working code sample. Please post your code as an minimal example that demonstrates your problem. For instance, we can't even see where the buttons class is being constructed (note: Java classes should be TitleCase). Commented May 23, 2014 at 7:11
  • Please show when the constructor buttons is called. Commented May 23, 2014 at 7:19

2 Answers 2

2

You need to initialise array before using it

try

private ArrayList<String> array = new ArrayList<String> (); 
Sign up to request clarification or add additional context in comments.

Comments

1

I'm concerned by your wording:

When actionSate becomes true the addURL() method should be called.

Do you realise that this if statement is evaluated just once, when the constructor is called?

//constructor
public buttons(){
    if(actionState == true || hyperlinkState == true){ // <-- only evaluated once
         addURL(field);
        System.out.println("URL added");
     }
}

If actionState becomes true at a later time, the addURL() method will not be called.

It's not clear from your code example whether you are even calling this constructor. Possible you are calling it in the loadData() method. However, since the last action of your action listener is to set actionState to true, there is no chance that your addURL() method is being called.

Solution?

You probably need to directly invoke the addURL() method from your action listener method. Ensure your action listener class has a reference to an instance of your Buttons class, then it can invoke the method directly. E.g.

public class Foo {

  // private boolean actionState = false; remove this?
  private Buttons buttons;

  public Foo(Buttons buttons) {
    this.buttons = buttons;
  }

  private void actionListenerCalls(){

    //action performed when user types url into input box and presses Enter key
    field.addActionListener(
      new ActionListener(){
        public void actionPerformed(ActionEvent e){
          loadData(e.getActionCommand());
          buttons.addURL((JTextField) e.getSource());
        }
      }
    );
  }
}

1 Comment

This should be the accepted answer. @user3593948: you can solve this (for example) by passing an instance of your class to the actionlistener and calling some method on this instance so that you enforce the check to be made again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.