I'm trying to display a few Jlabels in the output of my actionlistener. I have a combobox included in the fields I needed added. i attempted to use a Jtable but had issues getting the selected item from the combobox. I choose to this method because I had already had my Slabel working and displaying. Since I added other Jlabels to display it is not displaying anything.
/* Date: 1/4/2012
* Contents: This program use 4 JTextFields, 4 JLabels, a comboBox, and two JButtons with actionlisteners.
* Users can select a department, input the item name, price, and desired discount for the items.
* The calculate button will calculate the discount rate using decimal form (i.e. .12, .13)
* using % for the discount will not work.
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Ash Anderson
*/
public class myGUI extends JFrame {
/*
* declaration for field varibles.
*/
double pf, df, sf;
String p, d, s, i, dpt;
private final JLabel slabel;
private final JTextField itemfield;
private final JTextField discountfield;
private final JTextField pricefield;
private final JButton calc;
/*
* below declarations are used in the GridBagLayout constraints
*/
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = true;
private final JLabel plabel;
private final JLabel dplabel;
private final JLabel ilabel;
private final JLabel drlabel;
public myGUI() {
/*
* creates the JFrame
*/
super("Retail Calculator");
setLookAndFeel();
JFrame frame = new JFrame("Retail Calculator");
GridLayout layout = new GridLayout();
frame.setLayout(layout);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
* create the ContentPane to hold components.
* create a new layout GridBagLayout
* assign layout to content pane
*/
Container container1 = getContentPane();
GridBagConstraints c = new GridBagConstraints();
container1.setLayout(new GridBagLayout());
/*
* if statement to set gridBagLayout fill mode to false.
* This creates the layout constraints for container1
*/
if (shouldFill) {
c.ipady = 1;//reset to default, gives the container padding.
c.weighty = 1.0;//request vertical space
c.weightx = 1.0;//request horizontal space
c.gridwidth = GridBagConstraints.REMAINDER;//shows all components within the container
c.fill = GridBagConstraints.BOTH;//fills the content pan with the components.
}
/*
* creates a JPanel to hold the combo box and item text field
*/
JPanel panel1 = new JPanel();
/*
* create label and combo box for departments
*/
JLabel dlabel = new JLabel("Department:");
String[] department = {"Women's Clothing", "Men's Clothing", "Boy's Clothing", "Girl's Clothing", "Infant Clothing", "Kitchen", "Sporting Goods", "Toys"};
final JComboBox dept = new JComboBox(department);
dept.setSelectedIndex(7);
/*
* creates label and text field for Item.
*/
JLabel item = new JLabel("Item:");
itemfield = new JTextField(8);
/*
* adds department and item components to JPanel panel1
*/
panel1.add(dlabel, c);
panel1.add(dept, c);
panel1.add(item, c);
panel1.add(itemfield, c);
/*
* creates JPanel panel2 to hold item price and discount rate fields
*/
JPanel panel2 = new JPanel();
final JLabel price = new JLabel("Original Price:");
pricefield = new JTextField(5);
final JLabel discount = new JLabel("Discount Rate:");
discountfield = new JTextField(5);
/*
* adds price and discount fields to panel2
*/
panel2.add(price);
panel2.add(pricefield);
panel2.add(discount, c);
panel2.add(discountfield, c);
panel2.add(Box.createGlue(), c);
/*
* creates JPanel panel3 to hold calculate button and "sumfield" textfield
*/
JPanel panel3 = new JPanel();
calc = new JButton("Calculate");
slabel = new JLabel();
ilabel = new JLabel();
dplabel = new JLabel();
plabel = new JLabel();
drlabel = new JLabel();
calc.setEnabled(true); //enables user clicking
/*
* creates an action listener for JButton calc
*/
calc.addActionListener(new ActionListener() {
private JPanel pane;
@Override
public void actionPerformed(ActionEvent ae) {
/*
* assigns p to pricefield and d to discountfield to retrive user input
*/
i = itemfield.getText();
dpt = (String) dept.getSelectedItem();
p = pricefield.getText();
d = discountfield.getText();
/*
* creats a try and catch to handle invalid input
*/
try {
/*
* creates an object to store pricefield user input for calculation
*/
Object val = pricefield.getText();
/*
* if statement to handel all invalid input.
*/
if (val != null) {
/*
* converts a String into a double
*/
pf = Double.parseDouble(p);
df = Double.parseDouble(d);
/*
* equation to multiple the item price times the discount rate
*/
double sum = pf * df;
/*
* sets computed Double back to the label and converts to string format.
* The discount price will be visible after button clicks in a JLabel.
*/
ilabel.setText(String.format("Item Name:", i));
dplabel.setText(String.format("Department", dpt));
plabel.setText(String.format("Original price", p));
drlabel.setText(String.format("Discount Rate", d));
slabel.setText(String.format("Discount Price: $" + "%3.2f", (pf - sum)));
ilabel.setVisible(true);
dplabel.setVisible(true);
plabel.setVisible(true);
drlabel.setVisible(true);
slabel.setVisible(true);
}
/*
* catch exception to handle invalid input.
*/
} catch (NumberFormatException e) {
slabel.setText(" Not valid input.");
dplabel.setText("Not valid input");
plabel.setText("Not valid input");
drlabel.setText("Not valid input");
}
}
});
/*
* adds components label and textfield for diuscount price to panel4
*/
panel3.add(calc, c);
panel3.add(slabel, c);
/*
* creates JPanel panel5 to hold the exit button
*/
JPanel panel5 = new JPanel();
JButton exit = new JButton("Exit");
/*
* adds an annonomous inner class ActionListener to JButton
*/
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { //actionlistener will close program.
System.exit(0);
}
});
JButton clear = new JButton("Clear");
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
itemfield.setText("");
pricefield.setText("");
discountfield.setText("");
slabel.setText("");
}
});
/*
* adds component buttons "exit" and "clear" to panel5
*/
panel5.add(clear, c);
panel5.add(exit, c);
/*
* adds panels to main contentPane named container. this contains all components that will be added to the JFrame
*/
container1.add(panel1, c);
container1.add(panel2, c);
container1.add(panel3, c);
// container1.add(panel4, c);
container1.add(panel5, c);
/*
* adds container1 to Jframe, sets frame visible and "packs" it to show all components.
*/
frame.add(container1);
frame.pack();
frame.setVisible(true);
}
/*
* creates a try and catch for the lookAndFeel of the frame.
* This is a standard set of code provided by Oracle. It creats a nice look and feel to your JFrame.
*/
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException exc) {
}
}
/*
* Main string of arguments to call the GUI retail calc
*/
public static void main(String[] args) {
myGUI g = new myGUI();
}
}