By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,666 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,666 IT Pros & Developers. It's quick & easy.

JButton question

P: 20
I put in a next and previous button but it crashes when I run it. I also need it to loop around to the first or last if at the ends. Can some one help me with this?

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5. import javax.swing.JFrame.*;
  6. import java.io.*;
  7.  
  8. class Testing
  9. {
  10.   java.util.List<DVD> dvds = new java.util.ArrayList<DVD>();
  11.  
  12.   JTextField tfTitle = new JTextField(15);
  13.   JTextField tfGenre = new JTextField();
  14.   JTextField tfProductNumber = new JTextField();
  15.   JTextField tfPrice = new JTextField();
  16.   JTextField tfQuantity = new JTextField();
  17.   JTextField tfvalue = new JTextField();
  18.   JTextField tfrestock = new JTextField();
  19.   DefaultListModel dlm = new DefaultListModel();
  20.   JList list = new JList(dlm);
  21.   public void buildGUI()
  22.   {
  23.     JButton btnAdd = new JButton("Add");
  24.     JButton btnPrevious = new JButton ("Previous");
  25.     JButton btnNext = new JButton ("Next");
  26.     JPanel p1 = new JPanel(new BorderLayout());
  27.     JPanel p = new JPanel(new GridLayout(8,4));
  28.     p.add(new JLabel("DVD Title: "));
  29.     p.add(tfTitle);
  30.     p.add(new JLabel("Genre: "));
  31.     p.add(tfGenre);
  32.     p.add(new JLabel("Product Number: "));
  33.     p.add(tfProductNumber);
  34.     p.add(new JLabel("Price per Unit: "));
  35.     p.add(tfPrice);
  36.     p.add(new JLabel("Quantity on Hand: "));
  37.     p.add(tfQuantity);
  38.     p.add(new JLabel("Value: "));
  39.     p.add(tfvalue);
  40.     p.add(new JLabel("Restock-fee: "));
  41.     p.add(tfrestock);
  42.     p1.add(p,BorderLayout.NORTH);
  43.     JPanel p2 = new JPanel();
  44.     p2.add(btnAdd);
  45.     p2.add(btnPrevious);
  46.     p2.add(btnNext);
  47.     p1.add(p2,BorderLayout.CENTER);
  48.     JFrame f = new JFrame();
  49.     f.setIconImage(new ImageIcon("sunlogo64x30.gif").getImage());
  50.     f.getContentPane().add(p1,BorderLayout.NORTH);
  51.     JScrollPane sp = new JScrollPane(list);
  52.     f.getContentPane().add(sp,BorderLayout.CENTER);
  53.     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54.     f.pack();
  55.     f.setLocationRelativeTo(null);
  56.     f.setVisible(true);
  57.     f.setIconImage(new ImageIcon("Images/sunlogo64x30.gif").getImage());
  58.     btnAdd.addActionListener(new ActionListener(){
  59.       public void actionPerformed(ActionEvent ae){
  60.         dvds.add(new DVD(tfTitle.getText(),tfGenre.getText(),
  61.                                 tfProductNumber.getText(),
  62.                                 Double.parseDouble(tfPrice.getText()),
  63.                                 Double.parseDouble(tfvalue.getText()),
  64.                                 Double.parseDouble(tfrestock.getText()),
  65.                                Integer.parseInt(tfQuantity.getText())));
  66.         setList();
  67.         clearTextFields();
  68.               }
  69.     });
  70.     btnPrevious.addActionListener(new ActionListener(){
  71.           public void actionPerformed(ActionEvent ae){
  72.             DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
  73.                         int index = (list.getSelectedIndex()-1) % dvds.size();
  74.                         if(index < 0) index = dvds.size()-1;
  75.                         list.setSelectedIndex(index);
  76.             setList();
  77.  
  78.       }
  79.     });
  80.  
  81.     btnNext.addActionListener(new ActionListener(){
  82.           public void actionPerformed(ActionEvent ae){
  83.             DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
  84.                         int index = (list.getSelectedIndex()+1) % dvds.size(); 
  85.                         list.setSelectedIndex(index);
  86.             setList();
  87.  
  88.       }
  89.     });
  90.     list.addListSelectionListener(new ListSelectionListener(){
  91.       public void valueChanged(ListSelectionEvent lse){
  92.         if(lse.getValueIsAdjusting() == false)
  93.         {
  94.           DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
  95.           tfTitle.setText(dvd.title);
  96.           tfGenre.setText(dvd.genre);
  97.           tfProductNumber.setText(dvd.productNumber);
  98.           tfPrice.setText(""+dvd.price);
  99.           tfQuantity.setText(""+dvd.quantity);
  100.           tfvalue.setText(""+dvd.value);
  101.           tfrestock.setText(""+dvd.restock);
  102.         }
  103.       }
  104.     });
  105.   }
  106.   public void setList()
  107.   {
  108.     dlm.clear();
  109.     for(int x = 0, y = dvds.size(); x < y; x++)
  110.     {
  111.       dlm.addElement((DVD)dvds.get(x));
  112.     }
  113.   }
  114.   public void clearTextFields()
  115.   {
  116.     tfTitle.setText("");
  117.     tfGenre.setText("");
  118.     tfProductNumber.setText("");
  119.     tfPrice.setText("");
  120.     tfQuantity.setText("");
  121.     tfvalue.setText("");
  122.     tfrestock.setText("");
  123.     tfTitle.requestFocusInWindow();
  124.   }
  125.   public static void main(String[] args)
  126.   {
  127.     EventQueue.invokeLater(new Runnable(){
  128.       public void run(){
  129.         new Testing().buildGUI();
  130.       }
  131.     });
  132.   }
  133. }
  134. class DVD
  135. {
  136.   String title;
  137.   String genre;
  138.   String productNumber;
  139.   double price;
  140.   double value;
  141.   double restock;
  142.   int quantity;
  143.   public DVD(String t, String g, String pn, double p, double v, double r, int q)
  144.   {
  145.     title = t; genre = g; productNumber = pn; price = p; value = v; restock = r; quantity = q;
  146.     value = p * q;
  147.     restock = v * 1.05;
  148.   }
  149.   public String toString(){return title;}
  150. }
Nov 14 '06 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 1,510
why do you call the setList() method in your btnPrevious and btnNext actionPerformed() methods - it clears the List and rewrites it (which you don't need to do for these buttons) and in the process sets list.getSelectedIndex() to -1 which causes your indexOutOfBounds Exceptions
Nov 14 '06 #2

P: 20
Thank you thank you. My next question is how would I add a button that goes to the last or first item? I have not been able to figure that part out.
Nov 14 '06 #3

Expert 100+
P: 1,510
Thank you thank you. My next question is how would I add a button that goes to the last or first item? I have not been able to figure that part out.
You could add two more buttons First and Last with appropriate ActionListeners, e.g. for First
Expand|Select|Wrap|Line Numbers
  1.     btnFirst.addActionListener(new ActionListener(){
  2.               public void actionPerformed(ActionEvent ae){
  3.                 DVD dvd = (DVD)dvds.get(0);
  4.                 list.setSelectedIndex(0);
  5.       }
  6.     });
  7.  
Nov 14 '06 #4

P: 20
thank you now i just have to re-read the book to see if i can find the value of the last item.
Nov 14 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.