0

I'm working on a Bibliography Generator Application and I want the values from my JTable to display in a textarea in Hardvard Referencing Format.

This is my code to get and print the values:

for (int row = 0; row < CitationTable.getRowCount(); row++) {
            JournalCitation js = new JournalCitation();

            js.setAuthorFirstName((String) CitationTable.getValueAt(row, 1));
            js.setAuthorLastName((String) CitationTable.getValueAt(row, 2));
            js.setYearPublished((int) CitationTable.getValueAt(row, 3));
            js.setTitle((String) CitationTable.getValueAt(row, 4));
            js.setVolumeNo((int) CitationTable.getValueAt(row, 5));
            js.setIssueNo((int) CitationTable.getValueAt(row, 6));
            js.setPagesUsed((int) CitationTable.getValueAt(row, 7));
            js.setDoi((String) CitationTable.getValueAt(row, 8));
            js.setDatabaseLocation((String) CitationTable.getValueAt(row, 9));
            js.setUrl((String) CitationTable.getValueAt(row, 10));
            js.setAccessDate((String) CitationTable.getValueAt(row, 11));

            System.out.println(js.getAuthorFirstName() + js.getAuthorLastName() + js.getYearPublished() + js.getTitle() + js.getVolumeNo() + js.getIssueNo() + js.getPagesUsed() + js.getDoi() + js.getDatabaseLocation() + js.getUrl() + js.getAccessDate());


        }
dic19
  • 17,821
  • 6
  • 40
  • 69
Dekra
  • 1
  • 1
  • Thank you so much, this really helped me a lot. I am gonna try using DataObjectTableModel sounds more efficient. Thanks a lot :D – Dekra Jan 20 '15 at 16:57

1 Answers1

1

You can use String#format(...) method in order to generate the text you want to display in your text area with the desired format. For example:

String text = String.format("%1s, %2s (%3s) %4s"
                  , js.getAuthorLastName().toUpperCase()
                  , js.getAuthorFirstName().toUpperCase()
                  , js.getYearPublished()
                  , js.getTitle()
);
textArea.setText(text);

This format will produce something like this:

SOMMERVILLE, IAN (2011) Software engineering

Note: I don't know the Harvard Referencing System but I hope you get the idea.

Note 2: JTextArea doesn't support text format such as bold or italics. You might want to use JEditorPane or JTextPane to fulfill this requirement if needed.

Off-topic

Instead of mapping-back the table's cells to a JournalCitation object manually, you might consider use a custom table model that allows you to hold objects of this class. Something like DataObjectTableModel or Rob Camick's RowTableModel / ListTableModel / BeanTableModel might be helpful to avoid reinventing the wheel.

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69