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();
}
}
buttonsclass is being constructed (note: Java classes should beTitleCase).buttonsis called.