Array output to GUI

I need to write a gui that takes data from an array and puts it in a gui frame. Nothing fancy, no buttons, just a frame with all the data in there. A close button wouldnt be a bad idea but its not needed.

The program currently displays the information I want just fine but does it in console. I just want to tell that data to use a gui instead of console.

Can anyone give me a hint on where to begin? I havent used swing or created a gui before this and I am new to java entirely.

[499 byte] By [Jmichelsen4a] at [2007-11-27 7:57:24]
# 1
add a JTextArea to a JScrollpaneadd the scrollpane to a JFramewhat you currently output to the console as System.out.println(...), change totextArea.append(... + "\n");
Michael_Dunna at 2007-7-12 19:39:14 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hmm, that gives me an idea where to start but I think my case is a bit more sophisticated then that. Here is the code.

// Jmichelsen

// Created June 17, 2007

/* This program is designed to show an inventory of CD's in alphabetical order

and their corresponding prices.

It gives the prices of the CD's and the value of the entire inventory.

It adds a restocking fee for the entire product inventory and displays it separately */

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Inventory4 {

public static void main(String[] args) {

CD cd;

Inventory inventory = new Inventory();

cd = new CDWithGenre(16, "Snatch Soundtrack", 11, 13.01f, "Soundtrack");

inventory.add(cd);

cd = new CDWithGenre(12, "Best Of The Beatles", 10, 19.99f, "Classic Rock");

inventory.add(cd);

cd = new CDWithGenre(45, "Garth Brooks", 18, 10.87f, "Country");

inventory.add(cd);

cd = new CDWithGenre(32, "American Idol", 62, 25.76f, "Alternative");

inventory.add(cd);

cd = new CDWithGenre(18, "Photoshop", 27, 99.27f, "None");

inventory.add(cd);

inventory.display();

} //End main method

} //End Inventory4 class

/* Defines data from main as CD data and formats it. Calculates value of a title multiplied by its stock.

Creates compareTo method to be used by Arrays.sort when sorting in alphabetical order. */

class CD implements Comparable{

//Declares the variables as protected so only this class and subclasses can act on them

protected int cdSku;

protected String cdName;

protected int cdCopies;

protected double cdPrice;

protected String genre;

//Constructor

CD(int cdSku, String cdName, int cdCopies, double cdPrice, String genre) {

this.cdSku= cdSku;

this.cdName= cdName;

this.cdCopies = cdCopies;

this.cdPrice = cdPrice;

this.genre = genre;

}

// This method tells the sort method what is to be sorted

public int compareTo(Object o)

{

return cdName.compareTo(((CD) o).getName());

}

// Calculates the total value of the copies of a CD

public double totalValue() {

return cdCopies * cdPrice;

}

// Tells the caller the title

public String getName() {

return cdName;

}

//Displays the information stored by the constructor

public String toString() {

return String.format("SKU=%2dName=%-20sStock=%3dPrice=$%6.2fValue=$%,8.2f",

cdSku, cdName, cdCopies, cdPrice, totalValue());

}

} // end CD class

//Class used to add items to the inventory, display output for the inventory and sort the inventory

class Inventory {

private CD[] cds;

private int nCount;

// Creates array cds[] with 10 element spaces

Inventory() {

cds = new CD[10];

nCount = 0;

}

// Used by main to input a CD object into the array cds[]

public void add(CD cd) {

cds[nCount] = cd;

++nCount;

sort();

}

//Displays the arrays contents element by element

public void display() {

System.out.println("\nThere are " + nCount + " CD titles in the collection\n");

for (int i = 0; i < nCount; i++)

System.out.println(cds[i]);

System.out.printf("\nTotal value of the inventory is $%,.2f\n\n", totalValue());

}

// Steps through the array adding the totalValue for each CD to "total"

public double totalValue() {

double total = 0;

double restock = 0;

for (int i = 0; i < nCount; i++)

total += cds[i].totalValue();

return total;

}

//Method used to sort the array by the the name of the CD

private void sort() {

Arrays.sort(cds, 0, nCount);

}

} // end Inventory class

// Subclass of CD. Creates new output string to be used, adds a restocking fee calculation and genre catagory to be displayed.

class CDWithGenre extends CD {

String genre;

CDWithGenre(int cdSku, String cdName, int cdCopies, double cdPrice, String genre) {

super(cdSku, cdName, cdCopies, cdPrice, genre);

this.cdName= cdName;

this.cdCopies = cdCopies;

this.cdPrice = cdPrice;

this.genre = genre;

}

// Calculates restocking fee based on previous data.

public double restockFee() {

double total = 0;

double restock = 0;

total = cdCopies * cdPrice;

restock = total * .05;

return restock;

}

// New output method overrides superclass's output method with new data and format.

public String toString() {

return String.format("SKU: %2dGenre: %-12sName: %-20s\nPrice: $%6.2fValue: $%,8.2fStock: %3d Restocking Fee: $%6.2f\n",

cdSku, genre , cdName, cdPrice, totalValue(), cdCopies, restockFee());

}// Ends CDWithGenre class

}

would I make a new class calling it GUI or whatever, in there extend JFrame and add a JTextArea and JScrollpane there? Would I need to make the class public or just an internal class?

Jmichelsen4a at 2007-7-12 19:39:14 > top of Java-index,Desktop,Core GUI APIs...
# 3

here's a simple way to answer your current problem - just change display()

public void display() {

JTextArea textArea = new JTextArea();

textArea.append("\nThere are " + nCount + " CD titles in the collection\n\n");

for (int i = 0; i < nCount; i++)

textArea.append(cds[i]+"\n");

textArea.append("\nTotal value of the inventory is "+new java.text.DecimalFormat("$0.00").format(totalValue())+"\n\n");

JFrame frame = new JFrame();

frame.getContentPane().add(new JScrollPane(textArea));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

but it would need to be a bit more elaborate if you start adding listeners, save data etc

Michael_Dunna at 2007-7-12 19:39:14 > top of Java-index,Desktop,Core GUI APIs...
# 4
Wow that was so simple. That worked great, thank you
Jmichelsen4a at 2007-7-12 19:39:14 > top of Java-index,Desktop,Core GUI APIs...