Nobody Special
Nobody Special

Reputation: 19

javax.swing.jlabel Error

When I run my program in Eclipse, it says there are errors but they are not marked on the left margin like most of the errors are shown in that program. The program runs correctly if I bypass that message. In the console area of Eclipse, it gives me this:

javax.swing.jlabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0

What does that mean? How can I go about fixing this problem? Do you see anything in the program that I might want to change, i.e. things I should remove/add, things that are not needed?

NOTE: Updated code is below with the case change and the getText() added as suggested below.

import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;

public class JPizzeria extends JFrame implements ActionListener, ItemListener {

    DecimalFormat fmt = new DecimalFormat("0.00");

    private double Price;
    double smallPizza = 7;
    double toppings;

    JLabel title = new JLabel("Steve's Pizzeria");
    Font headlineFont = new Font("Courier", Font.BOLD, 40);
    JLabel info = new JLabel("What size of pizza do you wish to order?");
    Font infoFont = new Font("Courier", Font.BOLD, 18);
    JLabel info2 = new JLabel("What would you like on your pizza?");
    Font info2Font = new Font("Courier", Font.BOLD, 18);
    JLabel totalPrice = new JLabel("Total Price");
    Font totalPriceFont = new Font("Courier", Font.BOLD, 18);

    JCheckBox extraCheeseBox = new JCheckBox("Extra Cheese", false);
    JCheckBox pepperoniBox = new JCheckBox("Pepperoni", false);
    JCheckBox sausageBox = new JCheckBox("Sausage", false);
    JCheckBox groundBeefBox = new JCheckBox("Ground Beef", false);
    JCheckBox onionBox = new JCheckBox("Onions", false);
    JCheckBox mushroomBox = new JCheckBox("Mushrooms", false);
    JCheckBox blackOlivesBox = new JCheckBox("Black Olives", false);
    JCheckBox greenPeppersBox = new JCheckBox("Green Peppers", false);

    JTextField totPrice = new JTextField(10);

    public JPizzeria() {

        String[] pizzaSize = { "Small-$7.00", "Medium-$9.00", "Large-$11.00",
                "Extra-Large-$14.00" };

        JComboBox decide = new JComboBox(pizzaSize);

        add(title);
        title.setFont(headlineFont);
        add(info2);
        info2.setFont(info2Font);
        add(extraCheeseBox);
        add(pepperoniBox);
        add(sausageBox);
        add(groundBeefBox);
        add(onionBox);
        add(mushroomBox);
        add(blackOlivesBox);
        add(greenPeppersBox);
        add(info);
        info.setFont(infoFont);
        add(decide);
        add(totalPrice);
        totalPrice.setFont(totalPriceFont);
        add(totPrice);
        setLayout(new FlowLayout(FlowLayout.CENTER));
        decide.setSelectedIndex(3);
        decide.addActionListener(this);
        totPrice.addActionListener(this);

        totPrice.setText("$");
        extraCheeseBox.addItemListener(this);
        pepperoniBox.addItemListener(this);
        sausageBox.addItemListener(this);
        groundBeefBox.addItemListener(this);
        onionBox.addItemListener(this);
        mushroomBox.addItemListener(this);
        blackOlivesBox.addItemListener(this);
        greenPeppersBox.addItemListener(this);

    }

    public static void main(String[] args) {
        JPizzeria aFrame = new JPizzeria();
        final int WIDTH = 650;
        final int HEIGHT = 250;
        aFrame.setSize(WIDTH, HEIGHT);
        aFrame.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        JComboBox decide = (JComboBox) e.getSource();
        String pizzaSize = (String) decide.getSelectedItem();

        System.out.println(pizzaSize);

        if (pizzaSize.equals("Small-$7.00"))
            Price = smallPizza + 0;

        else if (pizzaSize.equals("Medium-$9.00"))
            Price = smallPizza + 2;

        else if (pizzaSize.equals("Large-$11.00"))
            Price = smallPizza + 4;

        else
            Price = smallPizza + 7;

        System.out.println(totalPrice.getText());;
        totPrice.setText("$" + (fmt.format(Price + toppings)));
    }

    public void itemStateChanged(ItemEvent event) {
        Object source = event.getSource();
        int select = event.getStateChange();

        if (source == extraCheeseBox) {
            if (select == ItemEvent.SELECTED) {
                toppings += 1;
            } else
                toppings -= 1;
        } else if (source == pepperoniBox) {
            if (select == ItemEvent.SELECTED)
                toppings += 1.00;
            else
                toppings -= 1.00;
        } else if (source == sausageBox) {
            if (select == ItemEvent.SELECTED)
                toppings += 1.00;
            else
                toppings -= 1.00;
        } else if (source == groundBeefBox) {
            if (select == ItemEvent.SELECTED)
                toppings += 1.00;
            else
                toppings -= 1.00;
        } else if (source == onionBox) {
            if (select == ItemEvent.SELECTED)
                toppings += 1.00;
            else
                toppings -= 1.00;
        } else if (source == mushroomBox) {
            if (select == ItemEvent.SELECTED)
                toppings += 1.00;
            else
                toppings -= 1.00;
        } else if (source == blackOlivesBox) {
            if (select == ItemEvent.SELECTED)
                toppings += 1.00;
            else
                toppings -= 1.00;
        } else if (source == greenPeppersBox) {
            if (select == ItemEvent.SELECTED)
                toppings += 1.00;
            else
                toppings -= 1.00;
        } else if (select == ItemEvent.SELECTED)
            toppings += 1.00;
        else
            toppings -= 1.00;
        totPrice.setText("$ " + fmt.format(toppings));
    }
}

Upvotes: 1

Views: 1281

Answers (3)

Dylan Meeus
Dylan Meeus

Reputation: 5802

In this method

    public void actionPerformed(ActionEvent e) {
    JComboBox decide = (JComboBox) e.getSource();
    String pizzaSize = (String) decide.getSelectedItem();

    System.out.println(pizzaSize);

    if (pizzaSize.equals("Small-$7.00"))
        Price = smallPizza + 0;

    else if (pizzaSize.equals("Medium-$9.00"))
        Price = smallPizza + 2;

    else if (pizzaSize.equals("Large-$11.00"))
        Price = smallPizza + 4;

    else
        Price = smallPizza + 7;

    System.out.println(totalPrice);
    totPrice.setText("$" + (fmt.format(Price + toppings)));
}

You are saying System.out.println(totalPrice). So you are actually printing the textbox, not the data in the textbox. To do this, use System.out.println(totalPrice.getText());

Upvotes: 0

Cyrille Ka
Cyrille Ka

Reputation: 15523

If there are errors, but they are not marked in the gutter, then the error is not in this file, but somewhere else in your project, either in another file or in the project configuration. You can see all errors in your project in the Problems view. If you do not see the Problems view, you can show it by going to Windows > Show View > Console or typing Alt+Shift+Q, then X.

enter image description here

The output that you obtain in the console is due to this line:

System.out.println(totalPrice);

totalPrice is a JLabel so it will print information about the component itself, like its position and other properties. This is not related to the message that states you have errors in your project.

If what you want is to print the text of the JLabel, you should replace it by:

System.out.println(totalPrice.getText());

Suggestions

As you are asking for suggestions, the first thing you should do is to not set your class as as subclass of JFrame. This is unnecessary and not flexible. Just create a JFrame object and add your stuff in it.

Also, it is bad Java style to start a variable or field name by an uppercase letter, like you do with all your JCheckBox instances. See how that confuses StackOverflow's syntax highlighter. Names with first capital letters are for classes, interfaces or enums.

Finally, you seem to have a bug: if you select first the pizza size and only afterwards the toppings, the total price will be the total price of the toppings only, without the price of the pizza itself.

Upvotes: 1

An SO User
An SO User

Reputation: 24998

When I run my program in Eclipse, it says there are errors but they are not marked on the left margin like most of the errors are shown in that program

You are not making any syntactical errors at all. Why would it show you an error when you haven't made any ?


What does that mean?

You are printing out your JLabel.
What you see is not an error, rather it is the String representation of your JLabel.


How can I go about fixing this problem? Do you see anything in the program that I might want to change, i.e. things I should remove/add, things that are not needed?

If you would like to get the text that is in the JLabel, use the getText() method of JLabel.
You would do it as shown below:

System.out.println(totalPrice.getText());  

Upvotes: 0

Related Questions