Help with JButton
I need to add a next, previous, first and last button and can not figure it out. Can some one help me out on this? Also is there some place that may have something like templates for GUI programming?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Testing
{
java.util.List dvds =new java.util.ArrayList();
JTextField tfTitle =new JTextField(15);
JTextField tfProductNumber =new JTextField();
JTextField tfPrice =new JTextField();
JTextField tfQuantity =new JTextField();
JTextField tfvalue =new JTextField();
JTextField tfrestock =new JTextField();
DefaultListModel dlm =new DefaultListModel();
JList list =new JList(dlm);
publicvoid buildGUI()
{
JButton btn =new JButton("Add");
JPanel p1 =new JPanel(new BorderLayout());
JPanel p =new JPanel(new GridLayout(6,4));
p.add(new JLabel("DVD Title: "));
p.add(tfTitle);
p.add(new JLabel("Product Number: "));
p.add(tfProductNumber);
p.add(new JLabel("Price per Unit: "));
p.add(tfPrice);
p.add(new JLabel("Quantity on Hand: "));
p.add(tfQuantity);
p.add(new JLabel("value: "));
p.add(tfvalue);
p.add(new JLabel("restock: "));
p.add(tfrestock);
p1.add(p,BorderLayout.NORTH);
JPanel p2 =new JPanel();
p2.add(btn);
p1.add(p2,BorderLayout.CENTER);
JFrame f =new JFrame();
f.getContentPane().add(p1,BorderLayout.NORTH);
JScrollPane sp =new JScrollPane(list);
f.getContentPane().add(sp,BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
btn.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent ae){
dvds.add(new DVD(tfTitle.getText(),tfProductNumber.getText(),
Double.parseDouble(tfPrice.getText()),
Double.parseDouble(tfvalue.getText()),
Double.parseDouble(tfrestock.getText()),
Integer.parseInt(tfQuantity.getText())));
setList();
clearTextFields();
}
});
list.addListSelectionListener(new ListSelectionListener(){
publicvoid valueChanged(ListSelectionEvent lse){
if(lse.getValueIsAdjusting() ==false)
{
DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
tfTitle.setText(dvd.title);
tfProductNumber.setText(dvd.productNumber);
tfPrice.setText(""+dvd.price);
tfQuantity.setText(""+dvd.quantity);
tfvalue.setText(""+dvd.value);
tfrestock.setText(""+dvd.restock);
}
}
});
}
publicvoid setList()
{
dlm.clear();
for(int x = 0, y = dvds.size(); x < y; x++)
{
dlm.addElement((DVD)dvds.get(x));
}
}
publicvoid clearTextFields()
{
tfTitle.setText("");
tfProductNumber.setText("");
tfPrice.setText("");
tfQuantity.setText("");
tfvalue.setText("");
tfrestock.setText("");
tfTitle.requestFocusInWindow();
}
publicstaticvoid main(String[] args)
{
EventQueue.invokeLater(new Runnable(){
publicvoid run(){
new Testing().buildGUI();
}
});
}
}
class DVD
{
String title;
String productNumber;
double price;
double value;
double restock;
int quantity;
public DVD(String t,String pn,double p,double v,double r,int q)
{
title = t; productNumber = pn; price = p; value = v; restock = r; quantity = q;
value = p * q;
restock = v * q;
}
public String toString(){return title;}
}

