Results 1 to 8 of 8
Thread: Why isn't it working? :confused:
- 09-14-2010, 08:39 AM #1
Member
- Join Date
- Jun 2010
- Location
- HI
- Posts
- 9
- Rep Power
- 0
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(); } }
- 09-14-2010, 09:29 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
What does "can't get the submit button to work" mean?
You need to be a bit more descriptive, including any exception messages.
- 09-14-2010, 09:50 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 31
- Rep Power
- 0
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);
}
- 09-14-2010, 10:14 AM #4
Member
- Join Date
- Jun 2010
- Location
- HI
- Posts
- 9
- Rep Power
- 0
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.
- 09-14-2010, 10:16 AM #5
Member
- Join Date
- Jun 2010
- Location
- HI
- Posts
- 9
- Rep Power
- 0
- 09-14-2010, 10:26 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 09-14-2010, 10:38 AM #7
Member
- Join Date
- Jun 2010
- Location
- HI
- Posts
- 9
- Rep Power
- 0
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.
- 09-14-2010, 11:41 AM #8
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
-
Help i am so confused..:(
By angeltiner in forum AWT / SwingReplies: 6Last Post: 04-11-2010, 01:43 AM -
The code isnt working unless I add print statements at diffrent points!:confused:
By Addez in forum New To JavaReplies: 6Last Post: 11-12-2009, 10:50 AM -
Very confused Plz help!!
By ratb0y in forum NetBeansReplies: 0Last Post: 02-14-2009, 04:34 PM -
confused
By updev in forum AWT / SwingReplies: 6Last Post: 11-14-2008, 03:33 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks