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!

Place that code in a method of your ListTest() class rather than adding it directly in main(). main() should be performing operations on your ListTest instance and should not directly operate on any members of that class.

Place that code in a method of your ListTest() class rather than adding it directly in main(). main() should be performing operations on your ListTest instance and should not directly operate on any members of that class.

Hi, thanks for the reply.

I tried doing that, ie, adding items to the list and then doing append, but it's still now working. Nothing comes out of the JTextArea.

ListTest list = new ListTest();
	ListTest.insert("--------------------");

	ListIterator iterator = ListTest.listIterator();

	while(iterator.hasNext())
	ListTest_TextArea.append(list);

I placed this on the ListTest class. Still no go. Am I doing something wrong?

Many thanks for the prompt reply!

yes, you're doing something wrong. If you weren't, it would work :)

you can't reference anything from main that's not static (and no, you should not make everything static to get around that).

Let me rephrase my question.

I'm unable to do this:

PhoneDirectorySystem list = new PhoneDirectorySystem();
list.insert( new PhoneEntry("Roger M", "999-000"));
list.insert( new PhoneEntry("Roger W", "888-000"));
list.insert( new PhoneEntry("Rich P", "777-000"));
list.insert( new PhoneEntry("Jane M", "666-000"));
list.insert( new PhoneEntry("Stacy K", "555-000"));

and append it to a JTextArea, something like:

Phone_TextArea.append(list);

Because it gives this error message:

append(java.lang.String) in javax.swing.JTextArea cannot be applied to (PhoneDirectorySystem)PhoneBook_TextArea.append(list);

Anywhere I put it in the program. How can I have it shown in the JTextArea then?

The error-- append(java.lang.String) in javax.swing.JTextArea cannot be applied to (PhoneDirectorySystem)PhoneBook_TextArea.append(list); --is a fairly obvious statement.


You could return the toString() of an element in the list and add to the JTextArea.

It may even be better to have a toString method available for the entire list (if you're making a custom list) and separate the values with the line-terminator "\n".

Hi Alex,

Thanks for the reply. It's extremely helpful. However, I'm still confused (a little bit) and I'm sorry for being so clueless.

I have a toString() method that reads:

public String toString()
{
return name + " " + number;
} // toString

How do I change it so I can append the list to the JTextArea?

Many thanks for the really prompt reply!

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