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();

    }
}

Calling setVisible(true) doesn't help for JLabels. They are naturally visible, but only if their parent container is visible, and I can't see where most of your labels are added to a parent container. That's probably a problem.

I also notice that you have two JFrames. You have the JFrame called frame that you are creating in the constructor for myGUI and myGUI is also a JFrame, but it seems that it is never made visible. I guess that is unintentional.

Im not sure what is the expected output, but I can see the discount JLabel (sLAbel)?

kgfsd

Please be more specific.

A thing to note though regarding your code, no need for the setVisible(..) on JLabel as you dont call setVisible(false) you simply clear text.

Also dont forget to create Swing components on Event Dispatch Thread via SwingUtilities.invokeLater(Runnable r) block.

Edited 3 Years Ago by DavidKroukamp

I need to display the item name, department, original price, discount rate, and discounted price not just the discounted price. I over looked this requirement and now am back tracking to include. I don't think I am going about it the right way, at all. I just chose the easiest way with what I already know. If you can help me and shed some light on how to accomplish this. I had issues with the combobox and trying to pass it through into a JTable.

I didn't realize that I called two JFrames. Well, I kind of did but wasn't understanding why. Thank you for pointing that out I understand understand why now.

The last thing you mention? do you mean for me to use the swingUtilities to invoke the GUI instead of calling my method?

Edited 3 Years Ago by Blink383

I updated my code but I can't get all the data to display in my JLabel "slabel" I figured out how to pass the combobox and get selected item. Is my problem with the slabel, it doesn't display all the code I am asking it to. is the problem that I'm trying to pass to much data into it?. is there a better way for me to accomplish this? here is my code:

/* 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, dptselecteditem;
    int index;
    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 discountlabel = new JLabel("Department:");
        final String[] department = {"Women's Clothing", "Men's Clothing", "Boy's Clothing", "Girl's Clothing", "Infant Clothing", "Kitchen", "Sporting Goods", "Toys"};
        final JComboBox deptbox = new JComboBox(department);
        deptbox.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(discountlabel, c);
        panel1.add(deptbox, 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");
        calc.setEnabled(true); //enables user clicking
        slabel = new JLabel();
        ilabel = new JLabel();
        dplabel = new JLabel();
        plabel = new JLabel();
        drlabel = new JLabel();


        /*
         * creates an action listener for JButton calc
         */
        calc.addActionListener(new ActionListener() {


            @Override
            public void actionPerformed(ActionEvent ae) {

                /*
                 * assigns p to pricefield and d to discountfield to retrive user input
                 */


                /*
                 * creats a try and catch to handle invalid input
                 */
                try {
                i = itemfield.getText();
                p = pricefield.getText();
                d = discountfield.getText();
                pf = Double.parseDouble(p);
                df = Double.parseDouble(d);


                index = deptbox.getSelectedIndex();
                dptselecteditem = department[index];  
                    /*
                     * creates an object to store pricefield user input for calculation
                     */
                 Object val = p;

                    /*
                     * if statement to handel all invalid input.
                     */
                  if (val != null) {

                        /*
                         * equation to multiple the item price times the discount rate
                         */
                        df = df/100;
                        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.
                         */

                        slabel.setText(String.format("Item:" + i, "/n", "Department:"+ dptselecteditem + "%","/n", "Original Price:"+ p,
                                "/n", "Discount rate: " + d, "/n","Discount Price: $" + "%3.2f", (pf - sum)));                                                                    
                        slabel.setVisible(true);

                    }

                    /*
                     * catch exception to handle invalid input.
                     */
                } catch (NumberFormatException e) {
                    slabel.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();

    }
}

Edited 3 Years Ago by Blink383

You aren't using String.format correctly. It is very important to learn the rules of how a method is used before you attempt to use it. You should look at:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format%28java.lang.String,%20java.lang.Object...%29

http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

If you want to get the string that it looks like you are trying to make, you should do it like this:

final String form = "Item:%s%n" +
    "Department:%s%%%n" +
    "Original Price:%s%n" +
    "Discount rate: %s%n" +
    "Discount Price: $%.2f";
String result = String.format(form, i, dptselecteditem, p, d, pf - sum);

That's just my best guess because your use of String.format is nonsense.

Unfortunately, when I tried that string in your application the JLabel refused to show it. I can only guess that you're right and it is too long, but I don't know why that would cause the JLabel to show nothing at all. Aside from that, it doesn't seem like a good idea for you to try to put all of this stuff into a single JLabel. Why not use 5 JLabels? I would probably even use 10 JLabels, 5 for "Item", "Department", "Original Price", and "Discount Price". And another 5 to show the values. That would allow you to use a LayoutManager to lay it all out.

If you prefer to represent all of it as a single string, I would prefer to use a javax.swing.JTextArea. You can use setEditable(false) so the user doesn't type into it, and it works pretty nicely for this in my opinion.

When I run it shows the first line - "item: " and it displays the name of the item correctly and nothign else. I will read over the oracle pages you provided but I find those pages hard to understand without pretty examples LOL.. I need examples they really help me get the understand the processes a lot better. The code is probably really sloppy because I am back tracking and went through about 4 different routes to try and achive what I need for my assignment. I will mess with this more and get back to you guys :) Thank you for your advice and knowledge as always it is greatly valued and appriciated :)

Don't let Javadoc pages scare you! You must try to read them. Once you are able to read them everything will be very easy. Until you are able to read them, everything will be very hard.

Comments
Very good advice regarding Javadoc.

So i got my code all squared away, learned a thing or two about passing strings, and not soo afraid of JavaDoc pages anymore lol...

When I try to run this from the cmd it compiles but doesn't run. When I debug the program I get the following warnings -

Note: RetailCal.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

I could really use a little insight toward how to make my programs compile and run correctly from the cmd. I am concerned I"m going to be docked points if the application isn't compiling and running properly from the cmd. I do not have a lot of experiance with running from command prompt as I was taught through netbeans. I think this is gonna be a big issue for me if I don't learn this now.

C:\myfiles>javac -verbose RetailCal.java
[parsing started RegularFileObject[RetailCal.java]]
[parsing completed 24ms]
[search path for source files: C:\Program Files\Java\jdk1.7.0_09\lib]
[search path for class files: C:\Program Files\Java\jdk1.7.0_09\jre\lib\resource
s.jar,C:\Program Files\Java\jdk1.7.0_09\jre\lib\rt.jar,C:\Program Files\Java\jdk
1.7.0_09\jre\lib\sunrsasign.jar,C:\Program Files\Java\jdk1.7.0_09\jre\lib\jsse.j
ar,C:\Program Files\Java\jdk1.7.0_09\jre\lib\jce.jar,C:\Program Files\Java\jdk1.
7.0_09\jre\lib\charsets.jar,C:\Program Files\Java\jdk1.7.0_09\jre\lib\jfr.jar,C:
\Program Files\Java\jdk1.7.0_09\jre\classes,C:\Program Files\Java\jdk1.7.0_09\jr
e\lib\ext\access-bridge-64.jar,C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext\dns
ns.jar,C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext\jaccess.jar,C:\Program File
s\Java\jdk1.7.0_09\jre\lib\ext\localedata.jar,C:\Program Files\Java\jdk1.7.0_09\
jre\lib\ext\sunec.jar,C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext\sunjce_provi
der.jar,C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext\sunmscapi.jar,C:\Program F
iles\Java\jdk1.7.0_09\jre\lib\ext\zipfs.jar,C:\Program Files\Java\jdk1.7.0_09\li
b]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/awt/event/ActionEvent.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/awt/event/ActionListener.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/text/DecimalFormat.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/util/logging/Level.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/util/logging/Logger.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/table/DefaultTableModel.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/Object.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/String.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/awt/Insets.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/awt/Container.class)]]
[checking RetailCal]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/AutoCloseable.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/JFrame.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/awt/GraphicsConfiguration.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/awt/Frame.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/awt/Window.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/awt/Component.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/JLabel.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/Icon.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/io/Serializable.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/Comparable.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/CharSequence.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/JComboBox.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/util/Vector.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/ComboBoxModel.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/JComponent.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/JTextField.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/table/AbstractTableModel.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/JButton.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/beans/ConstructorProperties.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/annotation/Target.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/annotation/ElementType.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/annotation/Retention.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/annotation/RetentionPolicy.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/Action.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/AbstractButton.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/util/EventListener.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/Override.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/annotation/Annotation.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/text/JTextComponent.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/Double.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/Number.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/NumberFormatException.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/IllegalArgumentException.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/RuntimeException.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/Exception.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/Throwable.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/JOptionPane.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/JTable.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/table/TableModel.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/JScrollPane.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/System.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/awt/Dimension.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/awt/PopupMenu.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/UIManager.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/LookAndFeel.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/ClassNotFoundException.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/ReflectiveOperationException.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/InstantiationException.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/IllegalAccessException.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/UnsupportedLookAndFeelException.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/Class.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/Boolean.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/Runnable.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/javax/swing/SwingUtilities.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/Error.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/awt/HeadlessException.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/lang/UnsupportedOperationException.class)]]
[wrote RegularFileObject[RetailCal$createAndShowGUI$1.class]]
[wrote RegularFileObject[RetailCal$createAndShowGUI$2.class]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.7.0_09\lib\ct.sym(MET
A-INF/sym/rt.jar/java/awt/LayoutManager.class)]]
[wrote RegularFileObject[RetailCal$createAndShowGUI.class]]
[wrote RegularFileObject[RetailCal$1.class]]
[wrote RegularFileObject[RetailCal.class]]
[total 488ms]
Note: RetailCal.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\myfiles>


Here is my code:


/**
 *
 * @author Ashley
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Ashley
 */
public class RetailCal {

     private static class createAndShowGUI {

          private final Insets insets;
          private final Container container;

          public createAndShowGUI() {
               JFrame frame = new JFrame("Retail Discount Calculator");  //create jframe
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //default close operations
               frame.setSize(600, 450); //sets window size
               frame.setLocationRelativeTo(null); //centers the window on users monitor.
               container = frame.getContentPane();
               insets = container.getInsets();
               container.setLayout(null);

               /*create labels*/
               final JLabel itemlabel = new JLabel("Item Name:");
               JLabel departmentlabel = new JLabel("Department:");
               JLabel pricelabel = new JLabel("Original Price:");
               JLabel discountratelabel = new JLabel("Discount Rate:");

               /*create combobox and array for combobox values*/
               final String[] department = {"Women's Clothing", "Men's Clothing",
                    "Boy's Clothing", "Girl's Clothing", "Infant Clothing", "Kitchen",
                    "Sporting Goods", "Toys"
               };
               final JComboBox deptbox = new JComboBox(department);
               deptbox.setSelectedIndex(7);

               /*create textfields for user input*/
               final JTextField itemfield = new JTextField(8);
               final JTextField pricefield = new JTextField(8);
               final JTextField discountratefield = new JTextField(8);

               /*create a table using default table model and assign column names*/
               final DefaultTableModel model = new DefaultTableModel();
               model.addColumn("Item Name");
               model.addColumn("Department");
               model.addColumn("Original Price");
               model.addColumn("Discount Rate");
               model.addColumn("Discount Price");

               /*create button for calculations*/
               JButton button = new JButton("Calculate");
               button.setEnabled(true);
               button.addActionListener(new ActionListener() {

                    /* create nested inner class for button action */
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                         DecimalFormat formatter = new DecimalFormat("#.00");
                         try {
                              /* parse jtextfields to doubles */
                              double price = Double.parseDouble(pricefield.getText());
                              double discount = Double.parseDouble(discountratefield.getText()) / 100;

                              /* assign item text field to string */
                              String item = itemfield.getText();

                              /* assign selected combobox item to string*/
                              int index = deptbox.getSelectedIndex();
                              String deptselected = department[index];

                              /* calculation for item discount */
                              double discountrate, newprice;

                              discountrate = price * discount;
                              newprice = price - discountrate;

                              /* assign double values to strings */
                              String sprice = Double.toString(price);
                              String sdiscount = Double.toString(discount);
                              String snewprice = Double.toString(newprice);

                              /* create string array to store data */
                              String[] data = {item, deptselected, sprice, sdiscount, snewprice};

                              /* add string array to default table model */
                              model.addRow(data);


                              /* create an assignment to clear text after calculations */
                              itemfield.setText("");
                              pricefield.setText("");
                              discountratefield.setText("");

                         } catch (NumberFormatException ne) {
                              JOptionPane.showMessageDialog(null, "Please enter price and discount rate.");
                         }
                    }
               });
               final JTable table = new JTable(model);
               final JScrollPane scrollpane = new JScrollPane(table);

               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);
                    }
               });

               /*Set the bounds for components. This will layout each component in the content pane for a customized layout.*/

               itemlabel.setBounds(insets.left + 50,
                       insets.top + 10,
                       itemlabel.getPreferredSize().width,
                       itemlabel.getPreferredSize().height
                       );
               itemfield.setBounds(itemlabel.getX() + insets.left + 85,
                       insets.top + 10,
                       itemfield.getPreferredSize().width,
                       itemfield.getPreferredSize().height
                       );

               departmentlabel.setBounds(insets.left + 50,
                       insets.top + 50,
                       departmentlabel.getPreferredSize().width,
                       departmentlabel.getPreferredSize().height
                       );
               deptbox.setBounds(departmentlabel.getX() + insets.left + 85,
                       insets.top + 50, deptbox.getPreferredSize().width,
                       deptbox.getPreferredSize().height
                       );

               pricelabel.setBounds(insets.left + 50,
                       insets.top + 100,
                       pricelabel.getPreferredSize().width,
                       pricelabel.getPreferredSize().height
                       );
               pricefield.setBounds(pricelabel.getX() + insets.left + 85,
                       insets.top + 100,
                       pricefield.getPreferredSize().width,
                       pricefield.getPreferredSize().height
                       );

               discountratelabel.setBounds(insets.left + 50,
                       insets.top + 150,
                       discountratelabel.getPreferredSize().width,
                       discountratelabel.getPreferredSize().height
                       );
               discountratefield.setBounds(discountratelabel.getX() + insets.left + 85,
                       insets.top + 150,
                       discountratefield.getPreferredSize().width,
                       discountratefield.getPreferredSize().height
                       );

               button.setBounds(insets.left + 50,
                       insets.top + 210, button.getPreferredSize().width,
                       button.getPreferredSize().height
                       );
               exit.setBounds(button.getX() + insets.left + 100,
                       insets.top + 210,
                       button.getPreferredSize().width,
                       button.getPreferredSize().height
                       );

               scrollpane.setBounds(insets.left + 30,
                       insets.top + 250,
                       scrollpane.getPreferredSize().height + 100,
                       scrollpane.getPreferredSize().width + 150
                       );

               /* add components to content pane */
               container.add(itemlabel);
               container.add(itemfield);
               container.add(departmentlabel);
               container.add(deptbox);
               container.add(pricelabel);
               container.add(pricefield);
               container.add(discountratelabel);
               container.add(discountratefield);
               container.add(exit);
               container.add(button);
               container.add(scrollpane);

               /* make container visible */
               container.setVisible(true);

               frame.setVisible(true);


          }
     }

     /**
      *
      * @param args
      */
     public static void main(String[] args) {

          try {
               UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
               UIManager.put("sing.boldMetal", Boolean.FALSE);
          } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
               Logger.getLogger(RetailCal.class.getName()).log(Level.SEVERE, null, ex);
          }


          javax.swing.SwingUtilities.invokeLater(new Runnable() {
               @Override
               public void run() {
                    createAndShowGUI createAndShowGUI = new createAndShowGUI();
               }
          });

     }
}

Edited 3 Years Ago by Blink383

First step is simply to do exactly what it told you to do:
Recompile with -Xlint:unchecked for details.
That will tell you what the unsafe or uchecked problem is, and exactly where it is. Then you can either update the code or add an annotation to allow the operation.

-verbose is not the option you want. It is very important to read and understand every message that javac gives you, and -verbose makes that a lot of work. What you want is what javac asked you to do if you care about the warning that it gave you, -Xlint:unchecked. Like this:

javac -Xlint:unchecked RetailCal.java

RetailCal.java:217: warning: [unchecked] unchecked call to JComboBox(E[]) as a member of the raw type JComboBox
final JComboBox deptbox = new JComboBox(department);
                          ^
  where E is a type-variable:
    E extends Object declared in class JComboBox
1 warning

That is so much more informative and readable. It's just reminding you that JComboBox has a generic type parameter that you have neglected to use. You could get rid of that warning by using JComboBox<String>, but generic parameters are always optional.

Edited 3 Years Ago by bguild

I actually did add -Slint: unchecked into netbeans. I was trying from cmd and trying the different options. So, your explaination for this was quiet helpful toward the different debugging functions.

I adjusted how a I declare my combobox and how I add the items and I don't get that message anymore. Thank you for your help :)

This question has already been answered. Start a new discussion instead.