Results 1 to 8 of 8
  1. #1
    ocarabal is offline Member
    Join Date
    Jun 2010
    Location
    HI
    Posts
    9
    Rep Power
    0

    Question Why isn't it working? :confused:

    Okay so I was working on some homework and I just completed this assignment, but I can't figure out why I can't get the submit button to work..

    Java Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    
    public class SkateBoardDesigner extends JFrame
    {
    	private String[] boards = { "The Master Thrasher / $60","The Dictator / $45","The Street King / $50" };
    	private String[] axles = { "7.75in axle / $35","8in axle / $40","8.5in axle / $45" };
    	private String[] wheels = { "55mm / $20","55mm / $22","58mm / $24","61mm / $28" };
    	private String[] misc = { "Grip Tape / $10","Bearings / $30","Riser Pads / $2","Nuts & Bolts kit / $3" };
    	private int[][] prices = { {60, 45, 50}, //deck
    					  {35, 40, 45}, //assembly
    					  {20, 22, 24, 28}, //wheels
    					  {10, 30,  2,  3} }; //Misc.
    
    	private int deckPrice, assemblyPrice, wheelPrice, miscPrice=0;
    	private int priceSTotal;
    	private double priceTax, priceTotal;
    	private JLabel name;
    
    	private JPanel titlePanel;
    	private JPanel choicePanel;
    	private JPanel totalPanel;
    	private JPanel bottomPanel;
    
    	private JLabel deck, assembly, whel, mis;
    	private JComboBox decks;
    	private JComboBox assemblies;
    	private JComboBox wheel;
    	private JList other;
    
    	private JButton total;
    
    	public SkateBoardDesigner()
    	{
    		setTitle("Skate Board Designer");
    		setSize(300, 270);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new BorderLayout());
    
    		titlePanel();
    		choicePanel();
    		totalPanel();
    
    		add(titlePanel, BorderLayout.NORTH);
    		add(choicePanel, BorderLayout.CENTER);
    		add(totalPanel, BorderLayout.SOUTH);
    		setVisible(true);
    	}
    	public void titlePanel()
    	{
    		name = new JLabel("The Skate Shop");
    		titlePanel = new JPanel();
    		titlePanel.add(name);
    	}
    
    	public void choicePanel()
    	{
    		deck = new JLabel("Decks");
    		decks = new JComboBox(boards);
    		decks.addActionListener(new DeckListener());//deck Listener
    		assembly = new JLabel("Truck Assemblies");
    		assemblies = new JComboBox(axles);
    		assemblies.addActionListener(new AssemblyListener());//AssemblyListener
    		whel = new JLabel("     Wheels");
    		wheel = new JComboBox(wheels);
    		wheel.addActionListener(new WheelListener());//WheelListner
    		mis = new JLabel("               Miscellaneous");
    		other = new JList(misc);
    		other.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    
    		choicePanel = new JPanel();
    		choicePanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		choicePanel.add(deck);
    		choicePanel.add(decks);
    		choicePanel.add(assembly);
    		choicePanel.add(assemblies);
    		choicePanel.add(whel);
    		choicePanel.add(wheel);
    		choicePanel.add(mis);
    		choicePanel.add(other);
    	}
    
    	private class DeckListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			deckPrice = prices[0][decks.getSelectedIndex()];
    		}
    	}
    
    	private class WheelListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			wheelPrice = prices[1][wheel.getSelectedIndex()];
    		}
    	}
    
    	private class AssemblyListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			assemblyPrice = prices[1][assemblies.getSelectedIndex()];
    		}
    	}
    
    	public void totalPanel()
    	{
    		total = new JButton("Total");
    		total.addActionListener(new TotalListener()); //TotalListener
    
    		totalPanel = new JPanel();
    		totalPanel.add(total);
    	}
    
    	private class TotalListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			int[] selections = other.getSelectedIndices();
    			int size = selections.length;
    			for (int index = 0; index<=size; index++)
    			{
    				miscPrice += prices[4][selections[index]];
    			}
    			priceSTotal = deckPrice+assemblyPrice+wheelPrice+miscPrice;
    			priceTax = priceSTotal*.06;
    			priceTotal = priceSTotal+priceTax;
    
    			DecimalFormat USCurrency = new DecimalFormat("#,##0.00");
    			JOptionPane.showMessageDialog(null, "Sub Total: $"+USCurrency.format(priceSTotal)+"\nTax: $"+USCurrency.format(priceTax)+"\nTotal Price: $"+USCurrency.format(priceTotal));
    		}
    	}
    	public static void main(String[] args)
    	{
    		new SkateBoardDesigner();
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    What does "can't get the submit button to work" mean?
    You need to be a bit more descriptive, including any exception messages.

  3. #3
    siva_vs_siva is offline Member
    Join Date
    Aug 2010
    Posts
    31
    Rep Power
    0

    Default your code to modify as

    null pointer arises when you referring undefined values.. so you have to change your code as..

    for (int index = 0; index<=size-1; index++)
    {
    miscPrice += prices[3][selections[index]];
    System.out.println(miscPrice);
    }

  4. #4
    ocarabal is offline Member
    Join Date
    Jun 2010
    Location
    HI
    Posts
    9
    Rep Power
    0

    Default

    ummm, you're going to have to explain what exactly your code means. I'm not following. I don't want to use code without understanding it.
    Last edited by ocarabal; 09-14-2010 at 10:17 AM.

  5. #5
    ocarabal is offline Member
    Join Date
    Jun 2010
    Location
    HI
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    What does "can't get the submit button to work" mean?
    You need to be a bit more descriptive, including any exception messages.
    On my computer, when I hit the button it doesn't do anything.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    What siva is saying is indexes for an array run from 0 to length - 1.

    Consequently prices[4] will cause an array index out of bounds exception, since there are only 4 entries in that array. In addition (and they missed this bit) "index <= size" will cause the same problem.

    You would see this if you looked at the console of whatever it is you are using to run this.

  7. #7
    ocarabal is offline Member
    Join Date
    Jun 2010
    Location
    HI
    Posts
    9
    Rep Power
    0

    Default

    Man, I feel dumb, now I understand what you mean. The same thing got me when I was first learning about arrays. In the book I think it was called off by one or something like that.

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by ocarabal View Post
    Man, I feel dumb, now I understand what you mean. The same thing got me when I was first learning about arrays. In the book I think it was called off by one or something like that.

    the WheelListener will also cause an runtime error when you select the last listitem from the weel combobox, because prices[1][wheel.getSelectedIndex()] has only 3 elements but the combobox returns 4 when you select the last item. so i guess the right code should be

    wheelPrice = prices[2][wheel.getSelectedIndex()];

Similar Threads

  1. Help i am so confused..:(
    By angeltiner in forum AWT / Swing
    Replies: 6
    Last Post: 04-11-2010, 01:43 AM
  2. Replies: 6
    Last Post: 11-12-2009, 10:50 AM
  3. Very confused Plz help!!
    By ratb0y in forum NetBeans
    Replies: 0
    Last Post: 02-14-2009, 04:34 PM
  4. confused
    By updev in forum AWT / Swing
    Replies: 6
    Last Post: 11-14-2008, 03:33 PM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •