JButton question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • porky008
    New Member
    • Oct 2006
    • 20

    #1

    JButton question

    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?

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.JFrame.*;
    import java.io.*;
    
    class Testing
    {
      java.util.List<DVD> dvds = new java.util.ArrayList<DVD>();
     
      JTextField tfTitle = new JTextField(15);
      JTextField tfGenre = new JTextField();
      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);
      public void buildGUI()
      {
        JButton btnAdd = new JButton("Add");
        JButton btnPrevious = new JButton ("Previous");
        JButton btnNext = new JButton ("Next");
        JPanel p1 = new JPanel(new BorderLayout());
        JPanel p = new JPanel(new GridLayout(8,4));
        p.add(new JLabel("DVD Title: "));
        p.add(tfTitle);
        p.add(new JLabel("Genre: "));
        p.add(tfGenre);
        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-fee: "));
        p.add(tfrestock);
        p1.add(p,BorderLayout.NORTH);
        JPanel p2 = new JPanel();
        p2.add(btnAdd);
        p2.add(btnPrevious);
        p2.add(btnNext);
        p1.add(p2,BorderLayout.CENTER);
        JFrame f = new JFrame();
        f.setIconImage(new ImageIcon("sunlogo64x30.gif").getImage());
        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);
        f.setIconImage(new ImageIcon("Images/sunlogo64x30.gif").getImage());
        btnAdd.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent ae){
            dvds.add(new DVD(tfTitle.getText(),tfGenre.getText(),
                                    tfProductNumber.getText(),
                                    Double.parseDouble(tfPrice.getText()),
                                    Double.parseDouble(tfvalue.getText()),
                                    Double.parseDouble(tfrestock.getText()),
                                   Integer.parseInt(tfQuantity.getText())));
            setList();
            clearTextFields();
                  }
        });
        btnPrevious.addActionListener(new ActionListener(){
    	      public void actionPerformed(ActionEvent ae){
    	        DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
    	        			int index = (list.getSelectedIndex()-1) % dvds.size();
    	        			if(index < 0) index = dvds.size()-1;
    	        			list.setSelectedIndex(index);
    	        setList();
           
          }
        });
    
        btnNext.addActionListener(new ActionListener(){
    	      public void actionPerformed(ActionEvent ae){
    	        DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
    	        			int index = (list.getSelectedIndex()+1) % dvds.size(); 
    	        			list.setSelectedIndex(index);
    	        setList();
           
          }
        });
        list.addListSelectionListener(new ListSelectionListener(){
          public void valueChanged(ListSelectionEvent lse){
            if(lse.getValueIsAdjusting() == false)
            {
              DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
              tfTitle.setText(dvd.title);
              tfGenre.setText(dvd.genre);
              tfProductNumber.setText(dvd.productNumber);
              tfPrice.setText(""+dvd.price);
              tfQuantity.setText(""+dvd.quantity);
              tfvalue.setText(""+dvd.value);
              tfrestock.setText(""+dvd.restock);
            }
          }
        });
      }
      public void setList()
      {
        dlm.clear();
        for(int x = 0, y = dvds.size(); x < y; x++)
        {
          dlm.addElement((DVD)dvds.get(x));
        }
      }
      public void clearTextFields()
      {
        tfTitle.setText("");
        tfGenre.setText("");
        tfProductNumber.setText("");
        tfPrice.setText("");
        tfQuantity.setText("");
        tfvalue.setText("");
        tfrestock.setText("");
        tfTitle.requestFocusInWindow();
      }
      public static void main(String[] args)
      {
        EventQueue.invokeLater(new Runnable(){
          public void run(){
            new Testing().buildGUI();
          }
        });
      }
    }
    class DVD
    {
      String title;
      String genre;
      String productNumber;
      double price;
      double value;
      double restock;
      int quantity;
      public DVD(String t, String g, String pn, double p, double v, double r, int q)
      {
        title = t; genre = g; productNumber = pn; price = p; value = v; restock = r; quantity = q;
        value = p * q;
        restock = v * 1.05;
      }
      public String toString(){return title;}
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    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.getSelecte dIndex() to -1 which causes your indexOutOfBound s Exceptions

    Comment

    • porky008
      New Member
      • Oct 2006
      • 20

      #3
      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.

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by porky008
        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
        Code:
            btnFirst.addActionListener(new ActionListener(){
                      public void actionPerformed(ActionEvent ae){
                        DVD dvd = (DVD)dvds.get(0);
                        list.setSelectedIndex(0);
              }
            });

        Comment

        • porky008
          New Member
          • Oct 2006
          • 20

          #5
          thank you now i just have to re-read the book to see if i can find the value of the last item.

          Comment

          Working...