Hi all, I hope you can help me as I am pretty desperate now. I've been trying to add contents of a linkedlist to a JTextArea, and it's not letting me.
I know, it might be a little simple, but any help would be very much appreciated!
I'm using TextPad, and it gives this error everytime non-static variable PhoneBook_TextArea cannot be referenced from a static context
everytime i remove it from main, it doesn't work as well, so please help?
Ann
public class ListTest extends JFrame implements ActionListener
{
private PhoneEntry head;
// Class Data
// Declare GUI Components
private JLabel Name_Label = new JLabel("Name");
private JLabel Number_Label = new JLabel("Number");
private JTextField Name_TextField = new JTextField (10);
private JTextField Number_TextField = new JTextField (10);
private JTextArea PhoneBook_TextArea = new JTextArea(20,30);
private JButton Add_Button = new JButton ("Add");
private JButton Remove_Button = new JButton ("Remove");
private JButton Update_Button = new JButton ("Update");
public ListTest()
{
// Initialise and set-up GUI components
// Interface has 3 panels
// First Panel
Panel First_Panel = new Panel();
First_Panel.setLayout(new FlowLayout(FlowLayout.CENTER));
First_Panel.add(Name_Label);
First_Panel.add(Name_TextField);
First_Panel.add(Number_Label);
First_Panel.add(Number_TextField);
// Second Panel
Panel Second_Panel = new Panel();
Second_Panel.setLayout(new FlowLayout(FlowLayout.CENTER));
Second_Panel.add(PhoneBook_TextArea);
// Third Panel
Panel Third_Panel = new Panel();
Third_Panel.setLayout(new FlowLayout(FlowLayout.CENTER));
Third_Panel.add(Add_Button);
Third_Panel.add(Remove_Button);
Third_Panel.add(Update_Button);
// Define the user interface
this.setLayout(new FlowLayout(FlowLayout.CENTER));
add(First_Panel);
add(Second_Panel);
add(Third_Panel);
Add_Button.addActionListener(this);
Remove_Button.addActionListener(this);
Update_Button.addActionListener(this);
head = null;
}
public void actionPerformed(ActionEvent e)
{
String Clicked_Button_String = e.getActionCommand();
if(Clicked_Button_String.compareTo("Remove") == 0)
{
PhoneBook_TextArea.setText("Haha");
}
else if(Clicked_Button_String.compareTo("Add") == 0)
{
PhoneBook_TextArea.setText("Lala");
}
}
public static void main(String[] args)
{
ListTest My_App = new ListTest ();
My_App.setBounds(20,20,450,300);
My_App.setTitle("PhoneBook v.1");
My_App.setVisible(true);
LinkedList staff = new LinkedList();
staff.addLast("Dick");
staff.addLast("Harry");
staff.addLast("Romeo");
staff.addLast("Tom");
// | in the comments indicates the iterator position
ListIterator iterator = staff.listIterator(); // |DHRT
iterator.next(); // D|HRT
iterator.next(); // DH|RT
// add more elements after second element
iterator.add("Juliet"); // DHJ|RT
iterator.add("Nina"); // DHJN|RT
iterator.next(); // DHJNR|T
// remove last traversed element
iterator.remove(); // DHJN|T
// print all elements
iterator = staff.listIterator();
while (iterator.hasNext())
PhoneBook_TextArea.append(iterator.next());
}
}
Now I know, I know, my code looks silly, but that what I pretty much want to do - put the contents of my LinkedList to a JTextArea.
Thank you so much in advance!