import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import javax.swing.JFrame;
import java.awt.event.*;
class Product implements Comparable {
private int cost;
private
String name;
private String brief;
public Product(int c, String n, String b) {
cost = c;
name = n;
brief = b;
}
public Product() {
this("default", "default", "default", "default");
}
public int getCost() {
return cost;
}
public String getName() {
return name;
}
public String getBrief() {
return Brief;
}
// compareTo() allows Product instances in a container
// (e.g. an ArrayList) to be sorted by Collections.sort().
// It can only sort things in one order (e.g. ascending order of
// name). Need to investigate the use of Comparator objects to
// allow different sort orders to be defined.
public int compareTo(Object o) {
Product temp = (Product) o;
// Strings already implement compareTo() so just use that
return (name.compareTo(temp.name));
return (brief.compareTo(temp.brief));
}
}
public class GUI1 extends JFrame {
// A container for the Product instances
ArrayList products = new ArrayList();
// GUI bits and bobs
JPanel panInput = new JPanel(new GridLayout(3,1)),
panAdd =new JPanel(),
panDelSort = new JPanel(),
panbrief =new JPanel();
JTextField txtName = new JTextField(600),
txtCost = new JTextField(20),
txtBrief = new JTextField(30);
JButton btnAdd =new JButton("Add"),
btnDelete = new JButton("Delete"),
btnSort =new JButton("Sort Products");
JTable tab = new JTable();
// The DefaultTableModel will allow the contents of the
// JTable to be manipulated.
DefaultTableModel tabMod = new DefaultTableModel();
Product [] initialProducts = {new Product(57, "Widget", "Welco"),
new Product(700, "Cuddly toy", "welco"),
new Product(12000, "Digital Camera", "Welco")};
public GUI1() {
super("Product list");
tab.setModel(tabMod);
tabMod.addColumn("Product Name");
tabMod.addColumn("Product Cost");
tabMod.addColumn("Brief");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
addProduct();
}
});
btnDelete.setToolTipText("Select the records to delete first");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
deleteProduct();
}
});
btnSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
sortProducts();
}
});
panAdd.add(btnAdd);
panAdd.add(new JLabel("Product name:"));
panAdd.add(txtName);
panAdd.add(new JLabel("Product cost:"));
panAdd.add(txtCost);
panAdd.add(new JLabel("Product brief:"));
panAdd.add(txtBrief);
panDelSort.add(btnDelete);
panDelSort.add(btnSort);
panInput.add(panAdd);
panInput.add(panDelSort);
getContentPane().add(panInput, BorderLayout.SOUTH);
getContentPane().add(new JScrollPane(tab));
loadInitialProducts();
}
// Delete a product from the ArrayList and from the JTable
public void deleteProduct() {
int [] rows = tab.getSelectedRows();
for (int i = 0; i < rows.length; i++) {
tabMod.removeRow(rows[i] - i); // delete from JTable
products.remove(rows[i] - i); // delete from ArrayList
}
}
// Sort the products in the ArrayList and then
// reload them into the JTable in the sorted order
public void sortProducts() {
Collections.sort(products); // sorts into name order
loadProductsIntoTable();
}
// Add a new products to the ArrayList and the JTable
public void addProduct() {
String [] row = new String [2];
Product newProd = new Product(Integer.parseInt(txtCost.getText()), txtName.getText()), txtbrief.getText());
products.add(newProd); // add to the ArrayList
row[0] = newProd.getName();
row[1] = Integer.toString(newProd.getCost, newProd.getBrief());
tabMod.addRow(row); // add to the JTable
}
public void loadInitialProducts() {
for (int i = 0; i < initialProducts.length; i++) {
products.add(initialProducts[i]);
}
loadProductsIntoTable();
}
public void loadProductsIntoTable() {
tabMod.setRowCount(0);
String [] row = new String [2];
for (Iterator i = products.iterator(); i.hasNext()
{
Product temp = (Product) i.next();
row[0] = temp.getName();
row[1] = Integer.toString(temp.getCost());
row[2] = Integer.toString(temp.getBrief());
tabMod.addRow(row);
}
}
public static void main(String [] a){
GUI1 me = new GUI1();
me.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0) ;
}
});
me.pack();
me.setVisible(true);
}
}
error message:
---------- javac ----------
GUI1.java:139: ';' expected
Product newProd = new Product(Integer.parseInt(txtCost.getText()), txtName.getText()), txtbrief.getText());
^
1 error
Output completed (0 sec consumed) - Normal Termination