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.


I think best practice is you should include an IoC in your project and inject a configuration object to your controller.

Example code of a controller:

public class YourController : Controller
 {
     private IConfigService _configService;

     //inject your configuration object here.
     public YourController(IConfigService configService){
           // A guard clause to ensure we have a config object or there is something wrong
           if (configService == null){
               throw new ArgumentNullException("configService");
           }
           _configService = configService;
     }
 }

You could configure your IoC to specify singleton scope for this configuration object. In case you need to apply this pattern to all your controllers, you could create a base controller class to reuse code.

Your IConfigService

public interface IConfigService
{
    string ConfiguredPlan{ get; }
}

Your ConfigService:

public class ConfigService : IConfigService
{
    private string _ConfiguredPlan = null;

    public string ConfiguredPlan
    {
        get
        {
            if (_ConfiguredPlan == null){
                    //load configured plan from DB
            }
            return _ConfiguredPlan;
        }
    }
}
  • This class is easily extended to include more configurations like connection String, Default timeout,...
  • We're passing in an interface to our controller class, it's easy for us to mock this object during unit testing.
4

3 に答える 3

0

TransactionObject の toString を実装してオーバーライドする必要があります。

于 2013-07-27T09:31:52.087 に答える