What's Here?
- Members: 340,098
- Replies: 920,354
- Topics: 154,917
- Snippets: 4,854
- Tutorials: 1,257
- Total Online: 4,834
- Members: 139
- Guests: 4,695
|
Welcome to Dream.In.Code |
|
|
Become an Expert!
Join 340,098 Programmers for FREE! Get instant access to thousands  of experts, tutorials, code snippets, and more! There are 4,834 people online right now. Registration is fast and FREE... Join Now!
Chat LIVE With a Expert
|
JSpinner
JSpinner
how do i get the info from a jspinner into an output
Rate Topic:
   

- New D.I.C Head
-
-
Group:
Members
-
Posts:
27
-
Joined:
29-November 08
Dream Kudos: 0
Posted 29 November 2008 - 04:34 PM
the program is cigar store basically i need to get info from a spinner to get the total boxes for purchase so i can calculate the subtotal any pointers on how to use a jspinner to get info for a textfield would help
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CigarStore2 extends JFrame
{
/*------------------------- declarations */
// color objects
Color black=new Color(0,0,0);
Color white=new Color(255,255,255);
// inputs
JComboBox brandJComboBox;
JLabel selectBrandJLabel;
JSpinner numberOfBoxesJSpinner;
JLabel numberOfBoxesJLabel;
JComboBox countyJComboBox;
JLabel selectCountyJLabel;
// outputs
JLabel itemSelectionJLabel;
JTextField itemSelectionJTextField;
JLabel brandPriceJLabel;
JTextField brandPriceJTextField;
JLabel subTotalJLabel;
JTextField subTotalJTextField;
JLabel salesTaxJLabel;
JTextField salesTaxJTextField;
JLabel totalSaleJLabel;
JTextField totalSaleJTextField;
// controls
JButton enterJButton;
JButton clearJButton;
JButton closeJButton;
// variables
String[] BrandArray ={"Europa","Parodi","De Noboli","Petri","Hodura"};
String[] CountyArray ={"Allegheny","Beaver"};
int itemIndex;
String brandName;
double brandPrice;
double subTotal;
double salesTax;
double totalSale;
// object classes
DetermineValue determineValue;
public CigarStore2()
{
createUserInterface();
}
public void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground(white);
contentPane.setLayout(null);
/*----------------------- initialize */
// inputs
brandJComboBox = new JComboBox(BrandArray);
brandJComboBox.setBounds(190,10,100,20);
brandJComboBox.setForeground(black);
brandJComboBox.setBackground(white);
brandJComboBox.setMaximumRowCount(5);
contentPane.add(brandJComboBox);
selectBrandJLabel = new JLabel();
selectBrandJLabel.setBounds(80, 10, 100, 20);
selectBrandJLabel.setFont(new Font("Default", Font.PLAIN, 12));
selectBrandJLabel.setText("Select Brand:");
selectBrandJLabel.setForeground(black);
selectBrandJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(selectBrandJLabel);
countyJComboBox = new JComboBox(CountyArray);
countyJComboBox.setBounds(190,70,100,20);
countyJComboBox.setForeground(black);
countyJComboBox.setBackground(white);
countyJComboBox.setMaximumRowCount(2);
contentPane.add(countyJComboBox);
selectCountyJLabel = new JLabel();
selectCountyJLabel.setBounds(80, 70, 100, 20);
selectCountyJLabel.setFont(new Font("Default", Font.PLAIN, 12));
selectCountyJLabel.setText("Select County:");
selectCountyJLabel.setForeground(black);
selectCountyJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(selectCountyJLabel);
//jspinner for number of boxes
numberOfBoxesJSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 5,1));
numberOfBoxesJSpinner.setBounds(190,40, 50, 20);
contentPane.add(numberOfBoxesJSpinner);
//jlabel for number of boxes
numberOfBoxesJLabel = new JLabel();
numberOfBoxesJLabel.setBounds(80, 40, 100, 20);
numberOfBoxesJLabel.setFont(new Font("Default", Font.PLAIN, 12));
numberOfBoxesJLabel.setText("Number Of Boxes:");
numberOfBoxesJLabel.setForeground(black);
numberOfBoxesJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(numberOfBoxesJLabel);
// outputs
itemSelectionJLabel = new JLabel();
itemSelectionJLabel.setBounds(80, 100, 100, 20);
itemSelectionJLabel.setFont(new Font("Default", Font.PLAIN, 12));
itemSelectionJLabel.setText("Selection:");
itemSelectionJLabel.setForeground(black);
itemSelectionJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(itemSelectionJLabel);
itemSelectionJTextField = new JTextField();
itemSelectionJTextField.setBounds(190, 100, 100, 20);
itemSelectionJTextField.setFont(new Font("Default", Font.PLAIN, 12));
itemSelectionJTextField.setHorizontalAlignment(JTextField.CENTER);
itemSelectionJTextField.setForeground(black);
itemSelectionJTextField.setBackground(white);
itemSelectionJTextField.setEditable(false);
contentPane.add(itemSelectionJTextField);
brandPriceJLabel = new JLabel();
brandPriceJLabel.setBounds(80, 130, 100, 20);
brandPriceJLabel.setFont(new Font("Default", Font.PLAIN, 12));
brandPriceJLabel.setText("Price Per Box:");
brandPriceJLabel.setForeground(black);
brandPriceJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(brandPriceJLabel);
brandPriceJTextField = new JTextField();
brandPriceJTextField.setBounds(190, 130, 50, 20);
brandPriceJTextField.setFont(new Font("Default", Font.PLAIN, 12));
brandPriceJTextField.setHorizontalAlignment(JTextField.CENTER);
brandPriceJTextField.setForeground(black);
brandPriceJTextField.setBackground(white);
brandPriceJTextField.setEditable(false);
contentPane.add(brandPriceJTextField);
subTotalJLabel = new JLabel();
subTotalJLabel.setBounds(80, 160, 100, 20);
subTotalJLabel.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJLabel.setText("SubTotal:");
subTotalJLabel.setForeground(black);
subTotalJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(subTotalJLabel);
subTotalJTextField = new JTextField();
subTotalJTextField.setBounds(190, 160, 50, 20);
subTotalJTextField.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJTextField.setHorizontalAlignment(JTextField.CENTER);
subTotalJTextField.setForeground(black);
subTotalJTextField.setBackground(white);
subTotalJTextField.setEditable(false);
contentPane.add(subTotalJTextField);
salesTaxJLabel = new JLabel();
salesTaxJLabel.setBounds(80, 190, 100, 20);
salesTaxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJLabel.setText("Sales Tax:");
salesTaxJLabel.setForeground(black);
salesTaxJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(salesTaxJLabel);
salesTaxJTextField = new JTextField();
salesTaxJTextField.setBounds(190, 190, 50, 20);
salesTaxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJTextField.setHorizontalAlignment(JTextField.CENTER);
salesTaxJTextField.setForeground(black);
salesTaxJTextField.setBackground(white);
salesTaxJTextField.setEditable(false);
contentPane.add(salesTaxJTextField);
totalSaleJLabel= new JLabel();
totalSaleJLabel.setBounds(80, 220, 100, 20);
totalSaleJLabel.setFont(new Font("Default", Font.PLAIN, 12));
totalSaleJLabel.setText("Total Sale:");
totalSaleJLabel.setForeground(black);
totalSaleJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(totalSaleJLabel);
totalSaleJTextField= new JTextField();
totalSaleJTextField.setBounds(190, 220, 50, 20);
totalSaleJTextField.setFont(new Font("Default", Font.PLAIN, 12));
totalSaleJTextField.setHorizontalAlignment(JTextField.CENTER);
totalSaleJTextField.setForeground(black);
totalSaleJTextField.setBackground(white);
totalSaleJTextField.setEditable(false);
contentPane.add(totalSaleJTextField);
// controls
enterJButton = new JButton();
enterJButton.setBounds(10, 300, 100, 20);
enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
enterJButton.setText("Enter");
enterJButton.setForeground(black);
enterJButton.setBackground(white);
contentPane.add(enterJButton);
enterJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
enterJButtonActionPerformed(event);
}
}
);
clearJButton = new JButton();
clearJButton.setBounds(130, 300, 100, 20);
clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
clearJButton.setText("Clear");
clearJButton.setForeground(black);
clearJButton.setBackground(white);
contentPane.add(clearJButton);
clearJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
clearJButtonActionPerformed(event);
}
}
);
closeJButton = new JButton();
closeJButton.setBounds(240, 300, 100, 20);
closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
closeJButton.setText("Close");
closeJButton.setForeground(black);
closeJButton.setBackground(white);
contentPane.add(closeJButton);
closeJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
closeJButtonActionPerformed(event);
}
}
);
determineValue = new DetermineValue();
setTitle("CigarStore2");
setSize(400, 400);
setVisible(true);
}
// main method
public static void main(String[] args)
{
CigarStore2 application = new CigarStore2();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void enterJButtonActionPerformed(ActionEvent event)
{
itemIndex = brandJComboBox.getSelectedIndex();
brandName = BrandArray[itemIndex];
determineValue.setIndexOfValue(itemIndex);
brandPrice = determineValue.getPricePerBox();
// calculatesubTotal();
displayOutput();
}
/* public void calculatesubTotal()
{
subTotal = numberOfBoxesJSpinner.getNumberOfBoxes();
determineValue.setIndexOfValue(itemIndex);
}
*/
public void displayOutput()
{
itemSelectionJTextField.setText("" + brandName);
brandPriceJTextField.setText("" + brandPrice);
//itemNUmberJTextField.setText("" + itemNumberValue);
}
public void clearJButtonActionPerformed(ActionEvent event)
{
itemSelectionJTextField.setText("");
brandPriceJTextField.setText("");
}
public void closeJButtonActionPerformed(ActionEvent event)
{
CigarStore2.this.dispose();
}
}
class DetermineValue
{
double []numericValue = {40.95,26.95,34.95,24.50,19.95};
int valueOfIndex;
public void setIndexOfValue(int indexValue)
{
valueOfIndex = indexValue;
}
public double getPricePerBox()
{
return numericValue[valueOfIndex];
}
}
This post has been edited by NewToJava1980: 29 November 2008 - 04:44 PM
Posted 29 November 2008 - 04:51 PM
Use the method getValue().

- New D.I.C Head
-
-
Group:
Members
-
Posts:
27
-
Joined:
29-November 08
Dream Kudos: 0
Posted 29 November 2008 - 05:04 PM
cfoley, on 29 Nov, 2008 - 04:51 PM, said:
Use the method getValue().
maybe I am a fool but, How?
I am not trying to not do this i am just lost, I guess...
getValue() in javax.swing.JSpinner cannot be applied to (javax.swing.JSpinner) I tried this
public void enterJButtonActionPerformed(ActionEvent event)
{
itemIndex = brandJComboBox.getSelectedIndex();
brandName = BrandArray[itemIndex];
determineValue.setIndexOfValue(itemIndex);
brandPrice = determineValue.getPricePerBox();
calculatesubTotal();
// calculatesalesTax();
displayOutput();
}
public void calculatesubTotal()
{
subTotal = numberOfBoxesJSpinner.getValue(numberOfBoxesJSpinner);
determineValue.setIndexOfValue(itemIndex);
}
This post has been edited by NewToJava1980: 29 November 2008 - 05:16 PM
Posted 29 November 2008 - 05:19 PM
No problem. You've initialised a JSpinner like this:
numberOfBoxesJSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 5,1));
When you want the value of that one, just use the line:
numberOfBoxesJSpinner.getValue();

- New D.I.C Head
-
-
Group:
Members
-
Posts:
27
-
Joined:
29-November 08
Dream Kudos: 0
Posted 29 November 2008 - 05:51 PM
cfoley, on 29 Nov, 2008 - 05:19 PM, said:
No problem. You've initialised a JSpinner like this:
numberOfBoxesJSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 5,1));
When you want the value of that one, just use the line:
numberOfBoxesJSpinner.getValue();
that was helpful, but, how do i multiply the numberOfBoxes by the price per box?
I was thinking
subTotal = (numberOfBoxesJSpinner.getValue()) * brandPrice;
hmm, as i look at it, it doesn't make sense... and of course i get an error...
operator * cannot be applied to java.lang.Object,double
NewToJava1980, on 29 Nov, 2008 - 05:48 PM, said:
cfoley, on 29 Nov, 2008 - 05:19 PM, said:
No problem. You've initialised a JSpinner like this:
numberOfBoxesJSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 5,1));
When you want the value of that one, just use the line:
numberOfBoxesJSpinner.getValue();
that was helpful, but, how do i multiply the numberOfBoxes by the price per box?
I was thinking
subTotal = (numberOfBoxesJSpinner.getValue()) * brandPrice;
hmm, as i look at it, it doesn't make sense... and of course i get an error...
operator * cannot be applied to java.lang.Object,double
wait just referred to my notes i think i may have figured it out...
//get Input
firstNumber = Integer.parseInt(firstNumberJTextField.getText());
secondNumber = Integer.parseInt(secondNumberJTextField.getText());
//add Numbers
finalNumber = firstNumber + secondNumber;
displayResults();
just got to adjust for my current scheme... right?
Posted 29 November 2008 - 05:55 PM
Or assign a temp double variable to spinner.getValue() then multiply it by brandPrice.

- New D.I.C Head
-
-
Group:
Members
-
Posts:
27
-
Joined:
29-November 08
Dream Kudos: 0
Posted 29 November 2008 - 06:01 PM
KYA, on 29 Nov, 2008 - 05:55 PM, said:
Or assign a temp double variable to spinner.getValue() then multiply it by brandPrice.
how would that work?
i have been at this for two hours now and i feel i am getting nowhere...
This post has been edited by NewToJava1980: 29 November 2008 - 07:06 PM
Posted 29 November 2008 - 07:02 PM
I haven't personally used a JSpinner but the following should work:
double temp = numberOfBoxesJSpinner.getValue();
subTotal = temp * brandPrice;
edit: Your spinner is filled with doubles right? If it keeps returning just "Object" type cast it to a double.
This post has been edited by KYA: 29 November 2008 - 07:03 PM

- New D.I.C Head
-
-
Group:
Members
-
Posts:
27
-
Joined:
29-November 08
Dream Kudos: 0
Posted 29 November 2008 - 07:17 PM
KYA, on 29 Nov, 2008 - 07:02 PM, said:
I haven't personally used a JSpinner but the following should work:
double temp = numberOfBoxesJSpinner.getValue();
subTotal = temp * brandPrice;
edit: Your spinner is filled with doubles right? If it keeps returning just "Object" type cast it to a double.
ahh no?
spinner
numberOfBoxesJSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 5,1));
numberOfBoxesJSpinner.setBounds(190,40, 50, 20);
contentPane.add(numberOfBoxesJSpinner);
and my calculatesubTotal
public void calculatesubTotal()
{
double temp = numberOfBoxesJSpinner.getValue();
subTotal = temp * brandPrice;
}
i get an error code
incompatible types
am i doing something wrong?
This post has been edited by NewToJava1980: 29 November 2008 - 07:18 PM
Posted 29 November 2008 - 07:21 PM
Ok so based on your declaration it holds integers. edit: Or at least it holds 0-5 steps in 1 increments so integers
public void calculatesubTotal()
{
int temp = (int) numberOfBoxesJSpinner.getValue(); //type cast
subTotal = temp * brandPrice;
}
This post has been edited by KYA: 29 November 2008 - 07:22 PM

- New D.I.C Head
-
-
Group:
Members
-
Posts:
27
-
Joined:
29-November 08
Dream Kudos: 0
Posted 29 November 2008 - 07:31 PM
KYA, on 29 Nov, 2008 - 07:21 PM, said:
Ok so based on your declaration it holds integers. edit: Or at least it holds 0-5 steps in 1 increments so integers
public void calculatesubTotal()
{
int temp = (int) numberOfBoxesJSpinner.getValue(); //type cast
subTotal = temp * brandPrice;
}
inconvertible types...
shananigans....
ok here is the entire code thus far
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.awt.*;
import java.util.*;
public class CigarStore2 extends JFrame
{
/*------------------------- declarations */
// color objects
Color black=new Color(0,0,0);
Color white=new Color(255,255,255);
// inputs
JComboBox brandJComboBox;
JLabel selectBrandJLabel;
JSpinner numberOfBoxesJSpinner;
JLabel numberOfBoxesJLabel;
JComboBox countyJComboBox;
JLabel selectCountyJLabel;
// outputs
JLabel itemSelectionJLabel;
JTextField itemSelectionJTextField;
JLabel brandPriceJLabel;
JTextField brandPriceJTextField;
JLabel subTotalJLabel;
JTextField subTotalJTextField;
JLabel salesTaxJLabel;
JTextField salesTaxJTextField;
JLabel totalSaleJLabel;
JTextField totalSaleJTextField;
// controls
JButton enterJButton;
JButton clearJButton;
JButton closeJButton;
// variables
String[] BrandArray ={"Europa","Parodi","De Noboli","Petri","Hodura"};
String[] CountyArray ={"Allegheny","Beaver"};
int itemIndex;
String brandName;
double brandPrice;
double subTotal;
double salesTax;
double totalSale;
double spinnerValue;
// object classes
DetermineValue determineValue;
public CigarStore2()
{
createUserInterface();
}
public void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground(white);
contentPane.setLayout(null);
/*----------------------- initialize */
// inputs
brandJComboBox = new JComboBox(BrandArray);
brandJComboBox.setBounds(190,10,100,20);
brandJComboBox.setForeground(black);
brandJComboBox.setBackground(white);
brandJComboBox.setMaximumRowCount(5);
contentPane.add(brandJComboBox);
selectBrandJLabel = new JLabel();
selectBrandJLabel.setBounds(80, 10, 100, 20);
selectBrandJLabel.setFont(new Font("Default", Font.PLAIN, 12));
selectBrandJLabel.setText("Select Brand:");
selectBrandJLabel.setForeground(black);
selectBrandJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(selectBrandJLabel);
countyJComboBox = new JComboBox(CountyArray);
countyJComboBox.setBounds(190,70,100,20);
countyJComboBox.setForeground(black);
countyJComboBox.setBackground(white);
countyJComboBox.setMaximumRowCount(2);
contentPane.add(countyJComboBox);
selectCountyJLabel = new JLabel();
selectCountyJLabel.setBounds(80, 70, 100, 20);
selectCountyJLabel.setFont(new Font("Default", Font.PLAIN, 12));
selectCountyJLabel.setText("Select County:");
selectCountyJLabel.setForeground(black);
selectCountyJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(selectCountyJLabel);
//jspinner for number of boxes
numberOfBoxesJSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 5,1));
numberOfBoxesJSpinner.setBounds(190,40, 50, 20);
contentPane.add(numberOfBoxesJSpinner);
//jlabel for number of boxes
numberOfBoxesJLabel = new JLabel();
numberOfBoxesJLabel.setBounds(80, 40, 100, 20);
numberOfBoxesJLabel.setFont(new Font("Default", Font.PLAIN, 12));
numberOfBoxesJLabel.setText("Number Of Boxes:");
numberOfBoxesJLabel.setForeground(black);
numberOfBoxesJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(numberOfBoxesJLabel);
// outputs
itemSelectionJLabel = new JLabel();
itemSelectionJLabel.setBounds(80, 100, 100, 20);
itemSelectionJLabel.setFont(new Font("Default", Font.PLAIN, 12));
itemSelectionJLabel.setText("Selection:");
itemSelectionJLabel.setForeground(black);
itemSelectionJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(itemSelectionJLabel);
itemSelectionJTextField = new JTextField();
itemSelectionJTextField.setBounds(190, 100, 100, 20);
itemSelectionJTextField.setFont(new Font("Default", Font.PLAIN, 12));
itemSelectionJTextField.setHorizontalAlignment(JTextField.CENTER);
itemSelectionJTextField.setForeground(black);
itemSelectionJTextField.setBackground(white);
itemSelectionJTextField.setEditable(false);
contentPane.add(itemSelectionJTextField);
brandPriceJLabel = new JLabel();
brandPriceJLabel.setBounds(80, 130, 100, 20);
brandPriceJLabel.setFont(new Font("Default", Font.PLAIN, 12));
brandPriceJLabel.setText("Price Per Box:");
brandPriceJLabel.setForeground(black);
brandPriceJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(brandPriceJLabel);
brandPriceJTextField = new JTextField();
brandPriceJTextField.setBounds(190, 130, 50, 20);
brandPriceJTextField.setFont(new Font("Default", Font.PLAIN, 12));
brandPriceJTextField.setHorizontalAlignment(JTextField.CENTER);
brandPriceJTextField.setForeground(black);
brandPriceJTextField.setBackground(white);
brandPriceJTextField.setEditable(false);
contentPane.add(brandPriceJTextField);
subTotalJLabel = new JLabel();
subTotalJLabel.setBounds(80, 160, 100, 20);
subTotalJLabel.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJLabel.setText("SubTotal:");
subTotalJLabel.setForeground(black);
subTotalJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(subTotalJLabel);
subTotalJTextField = new JTextField();
subTotalJTextField.setBounds(190, 160, 50, 20);
subTotalJTextField.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJTextField.setHorizontalAlignment(JTextField.CENTER);
subTotalJTextField.setForeground(black);
subTotalJTextField.setBackground(white);
subTotalJTextField.setEditable(false);
contentPane.add(subTotalJTextField);
salesTaxJLabel = new JLabel();
salesTaxJLabel.setBounds(80, 190, 100, 20);
salesTaxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJLabel.setText("Sales Tax:");
salesTaxJLabel.setForeground(black);
salesTaxJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(salesTaxJLabel);
salesTaxJTextField = new JTextField();
salesTaxJTextField.setBounds(190, 190, 50, 20);
salesTaxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJTextField.setHorizontalAlignment(JTextField.CENTER);
salesTaxJTextField.setForeground(black);
salesTaxJTextField.setBackground(white);
salesTaxJTextField.setEditable(false);
contentPane.add(salesTaxJTextField);
totalSaleJLabel= new JLabel();
totalSaleJLabel.setBounds(80, 220, 100, 20);
totalSaleJLabel.setFont(new Font("Default", Font.PLAIN, 12));
totalSaleJLabel.setText("Total Sale:");
totalSaleJLabel.setForeground(black);
totalSaleJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(totalSaleJLabel);
totalSaleJTextField= new JTextField();
totalSaleJTextField.setBounds(190, 220, 50, 20);
totalSaleJTextField.setFont(new Font("Default", Font.PLAIN, 12));
totalSaleJTextField.setHorizontalAlignment(JTextField.CENTER);
totalSaleJTextField.setForeground(black);
totalSaleJTextField.setBackground(white);
totalSaleJTextField.setEditable(false);
contentPane.add(totalSaleJTextField);
// controls
enterJButton = new JButton();
enterJButton.setBounds(10, 300, 100, 20);
enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
enterJButton.setText("Enter");
enterJButton.setForeground(black);
enterJButton.setBackground(white);
contentPane.add(enterJButton);
enterJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
enterJButtonActionPerformed(event);
}
}
);
clearJButton = new JButton();
clearJButton.setBounds(130, 300, 100, 20);
clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
clearJButton.setText("Clear");
clearJButton.setForeground(black);
clearJButton.setBackground(white);
contentPane.add(clearJButton);
clearJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
clearJButtonActionPerformed(event);
}
}
);
closeJButton = new JButton();
closeJButton.setBounds(240, 300, 100, 20);
closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
closeJButton.setText("Close");
closeJButton.setForeground(black);
closeJButton.setBackground(white);
contentPane.add(closeJButton);
closeJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
closeJButtonActionPerformed(event);
}
}
);
determineValue = new DetermineValue();
setTitle("CigarStore2");
setSize(400, 400);
setVisible(true);
}
// main method
public static void main(String[] args)
{
CigarStore2 application = new CigarStore2();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void enterJButtonActionPerformed(ActionEvent event)
{
itemIndex = brandJComboBox.getSelectedIndex();
brandName = BrandArray[itemIndex];
determineValue.setIndexOfValue(itemIndex);
brandPrice = determineValue.getPricePerBox();
calculatesubTotal();
}
public void calculatesubTotal()
{
int temp = (int) numberOfBoxesJSpinner.getValue(); //type cast
subTotal = temp * brandPrice;
}
public void displayOutput()
{
itemSelectionJTextField.setText("" + brandName);
brandPriceJTextField.setText("" + brandPrice);
subTotalJTextField.setText("" + subTotal );
}
public void clearJButtonActionPerformed(ActionEvent event)
{
itemSelectionJTextField.setText("");
brandPriceJTextField.setText("");
subTotalJTextField.setText("");
}
public void closeJButtonActionPerformed(ActionEvent event)
{
CigarStore2.this.dispose();
}
}
class DetermineValue
{
double []numericValue = {40.95,26.95,34.95,24.50,19.95};
int valueOfIndex;
public void setIndexOfValue(int indexValue)
{
valueOfIndex = indexValue;
}
public double getPricePerBox()
{
return numericValue[valueOfIndex];
}
}
I really appreciate everyone's help
Posted 29 November 2008 - 07:56 PM
This compiles
public void calculatesubTotal()
{
Object temp = numberOfBoxesJSpinner.getValue();
int num = Integer.parseInt(""+temp);
subTotal = num * brandPrice;
}
edit: It works, just have to call displayOutput() after sub totaling
This post has been edited by KYA: 29 November 2008 - 07:58 PM

- New D.I.C Head
-
-
Group:
Members
-
Posts:
27
-
Joined:
29-November 08
Dream Kudos: 0
Posted 29 November 2008 - 08:15 PM
KYA, on 29 Nov, 2008 - 07:56 PM, said:
This compiles
public void calculatesubTotal()
{
Object temp = numberOfBoxesJSpinner.getValue();
int num = Integer.parseInt(""+temp);
subTotal = num * brandPrice;
}
edit: It works, just have to call displayOutput() after sub totaling 
thanks i hope i have better luck with the rest of the program!!!
Posted 29 November 2008 - 10:55 PM
NewToJava1980, on 29 Nov, 2008 - 08:15 PM, said:
public void calculatesubTotal()
{
Object temp = numberOfBoxesJSpinner.getValue();
int num = Integer.parseInt(""+temp);
subTotal = num * brandPrice;
}
This is not valid Java code .... won't compile
What are you trying to do ?
Posted 30 November 2008 - 11:24 AM
Yes it will and it does. getValue() returns an object. For whatever reason, java won't let me do an easy type cast. So I parsed the integer from the object and performed the calculations.
JSpinner.getValue(), Earlier in the thread, we tried C style casting, but apparently that is illegal if an Object is getting returned. It should return an Integer (at least I think that), but it doesn't. I think its an acceptable workaround for the time being.
edited for typos
This post has been edited by KYA: 30 November 2008 - 11:29 AM
1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users
|
Be Social
Programming
Web Development
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|