0

I have spent all day on the Web and on this site looking for an answer to my problem, and hope you guys can help. First of all, I am trying to display the contents of an ArrayList to a JTextArea when I select the 'report' JButton. The array list is in another class separate from the text area. My problem stems from the fact that the array list is an array of objects, so that when I try to display it I get the error:

The method append(String) in the type JTextArea is not applicable 
  for the arguments (ArrayList.Account.TransactionObject>)

I can display the array list just fine in the console window but am stumped when it comes to displaying it in the text area. I'm under the assumption that there must be some kind of issue converting the Object to a String, because I have been unable to cast it to a String or call a toString method with the array list. Here is the relevant parts of my code.....

This is the portion in the AccountUI class where I created the JTextArea:

private JPanel get_ReportPane()
{
    JPanel JP_reportPane = new JPanel(new BorderLayout());
    Border blackline = BorderFactory.createLineBorder(Color.BLACK);
    TitledBorder title = BorderFactory.createTitledBorder(blackline, "Transaction Report");
    title.setTitleJustification(TitledBorder.CENTER);
    JP_reportPane.setBorder(title); 

    /* Create 'labels' grid and JLabels */
    JPanel report_labels = new JPanel(new GridLayout(2, 1, 5, 5));
    report_labels.add(new JLabel("Current Account Balance: ", SwingConstants.RIGHT));
    report_labels.add(new JLabel("Account Creation Date: ", SwingConstants.RIGHT));
    JP_reportPane.add(report_labels, BorderLayout.WEST);

    /* Create 'data' grid and text fields */
    JPanel JP_data = new JPanel(new GridLayout(2, 1, 5, 5));
    JP_data.add(TF_balance2 = new JTextField(10));
    TF_balance2.setBackground(Color.WHITE);
    TF_balance2.setEditable(false);
    JP_data.add(TF_created = new JTextField(10));
    TF_created.setBackground(Color.WHITE);
    TF_created.setEditable(false);
    JP_reportPane.add(JP_data, BorderLayout.CENTER);

    /* Create 'buttons' grid and buttons */
    JPanel JP_buttons = new JPanel(new GridLayout(2, 1, 5, 5));
    JButton JB_report = new JButton("Report");
    JB_report.setBackground(Color.GRAY);
    JB_report.setMargin(new Insets(3, 3, 3, 3));
    JB_report.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            reportAccount();
        }
    }); 


    JP_buttons.add(JB_report);
    JButton JB_close = new JButton("Close");
    JB_close.setBackground(Color.GRAY);
    JB_close.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0) 
        {
            System.exit(0);
        }           
    });

    JP_buttons.add(JB_close);   
    JP_reportPane.add(JP_buttons, BorderLayout.EAST);

    /* Create text area and scroll pane */
    reportArea.setBorder(blackline);
    reportArea.setForeground(Color.BLUE);
    reportArea.setLineWrap(true);
    reportArea.setWrapStyleWord(true);
    JScrollPane scrollPane = new JScrollPane(reportArea);
    reportArea.setEditable(false);

    JP_reportPane.add(scrollPane, BorderLayout.SOUTH);

    return JP_reportPane;
}

This is the method (called from JB_reportAction listener class shown above) where I try to display the array list in the text area (also in AccountUI class):

/**
 * Method used to display account transaction history in the text field.
 */
protected void reportAccount()
{
    reportArea.append(A.getTransactions());
}

And this is the method in the Account class that I am able to display the Array contents in a console output, but have been unable to figure out how to pass the Array contents to the AccountUI class as a String to display in the text area:

public ArrayList<TransactionObject> getTransactions() 
{
    for (int i = 0; i < transactionList.size(); i++)
    {
        System.out.println(transactionList.get(i));
        System.out.println("\n");
    }
    return transactionList;
}

I hope I have clarified my issue without confusing anyone. Any insight would be much appreciated.


Call toString() on the list:

reportArea.append(A.getTransactions().toString());

Or, if you want to display the elements of the list in a different format, loop over the elements:

for (TransactionObject transaction : A.getTransactions()) {
    reportArea.append(transaction.toString());
    reportArea.append("\n");
}

Loops and types are an essential part of programming. You shouldn't use Swing if you don't understand loops and types.

Also, please respect the Java naming conventions. Variables start with a lower-case letter, and don't contain underscore. They're camelCased.

4

3 回答 3

6

调用toString()列表:

reportArea.append(A.getTransactions().toString());

或者,如果你想以不同的格式显示列表的元素,循环遍历元素:

for (TransactionObject transaction : A.getTransactions()) {
    reportArea.append(transaction.toString());
    reportArea.append("\n");
}

循环和类型是编程的重要组成部分。如果您不了解循环和类型,则不应使用 Swing。

另外,请尊重 Java 命名约定。变量以小写字母开头,并且不包含下划线。他们是驼色的。

于 2013-07-27T09:30:48.370 回答
0

If you want to append content of objects in ArrayList to JTextArea you can use this :

for (Object obj : arrayList) {
  textArea.append(obj.toString() + "");
}
于 2013-07-27T09:30:55.430 回答
0

You have to implement and override toString for TransactionObject.

于 2013-07-27T09:31:52.087 回答