• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with sales enquiry in Java Editplus

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

I'm moving this to our "Java in General (Beginner)" forum where it will get the attention it deserves.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically you have too many closing )'s

You have

Product newProd = new Product(Integer.parseInt(txtCost.getText()), txtName.getText()), txtbrief.getText());

It should look like this:

Product newProd = new Product(Integer.parseInt(txtCost.getText()), txtName.getText(),txtbrief.getText());

There is a need for the extra ) after txtCost because you are closing out the parseInt() method, and there is a need for the extra ) after textbrief because you are closing out the constructor, but there is no need for the extra ) after txtName.

Hope that helps.
[ January 22, 2007: Message edited by: Nicholas Carrier ]
 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic