tags:

views:

86

answers:

1

Hello again, I am trying to get the user to input 2 fields. One is the volume of the pool and one is the volume of the hut tub. This then calculates the price of each, what I am having trouble is with if the user enters volume for the pool, then they can't enter anything for the hot tub and vise versa. This is what I have so far. Do I need to have 2 separate fields for this, or how can it be done?

Pretty much the string errors = ""; can be removed once I figure out how to only allow them to enter one set of numbers. Here is the calculate portion, the other part of the code is just the 3 labels.

pricePanel = new JPanel(new FlowLayout());

        final JRadioButton poolPrice= new JRadioButton("Pool");
        final JRadioButton tubPrice = new JRadioButton("Hot Tub");

        poolPrice.setSelected(true);

        ButtonGroup group = new ButtonGroup();
        group.add(poolPrice);
        group.add(tubPrice);

        pricePanel.add(poolPrice);
        pricePanel.add(tubPrice);

        pricePanel.add(new JLabel("Enter the pool's volume: "));
        final JTextField poolField = new JTextField(10);
        pricePanel.add(poolField);

        pricePanel.add(new JLabel("Enter the tub's volume: "));
        final JTextField tubField = new JTextField(10);
        pricePanel.add(tubField);


        JButton calculatePrice = new JButton("Calculate Price");
        calculatePrice.setMnemonic('C');
        pricePanel.add(calculatePrice);
        pricePanel.add(createExitButton());

        pricePanel.add(new JLabel("The price is:$ "));
        final JTextField priceField = new JTextField(10);
        priceField.setEditable(false);
        pricePanel.add(priceField);


        calculatePrice.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                    double pool = Double.parseDouble (poolField.getText());
                    double tub = Double.parseDouble(tubField.getText());

                    double price;
                    if (poolPrice.isSelected()) {
                        price = pool * 100;
                    } else {
                        price = tub * 75;
                    }
                    priceField.setText(df.format(price));
                }
        });
    };


        ActionListener priceListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == poolPrice) {
                    tubLabel.setEnabled(false);
                    tubField.setEnabled(false);
                    messageArea.setVisible(true);
                } else if (e.getSource() == tubPrice) {
                    poolLabel.setEnabled(false);
                    poolField.setEnabled(false);
                    messageArea.setVisible(true);
                }
            }
        };


        poolPrice.addActionListener(priceListener);
        tubPrice.addActionListener(priceListener);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

hotTubsPanel = new JPanel(new FlowLayout());

    final JRadioButton roundTub = new JRadioButton("Round Tub");
    final JRadioButton ovalTub = new JRadioButton("Oval Tub");

    roundTub.setSelected(true);

    ButtonGroup group = new ButtonGroup();
    group.add(roundTub);
    group.add(ovalTub);

    hotTubsPanel.add(roundTub);
    hotTubsPanel.add(ovalTub);

    hotTubsPanel.add(new JLabel("Enter the tub's length:       "));
    final JTextField lengthField = new JTextField(10);
    hotTubsPanel.add(lengthField);

    final JLabel widthLabel = new JLabel("Enter the tub's width*: ");
    widthLabel.setEnabled(false);
    hotTubsPanel.add(widthLabel);

    final JTextField widthField = new JTextField(10);
    widthField.setEnabled(false);
    hotTubsPanel.add(widthField);

    hotTubsPanel.add(new JLabel("Enter the tub's depth: "));
    final JTextField depthField = new JTextField(10);
    hotTubsPanel.add(depthField);

    JButton calculateVolume = new JButton("Calculate Volume");
    calculateVolume.setMnemonic('C');
    hotTubsPanel.add(calculateVolume);
    hotTubsPanel.add(createExitButton());

    hotTubsPanel.add(new JLabel("The tub's volume is: "));
    final JTextField volumeField = new JTextField(10);
    volumeField.setEditable(false);
    hotTubsPanel.add(volumeField);

    final JTextArea messageArea = createMessageArea(1, 25,
            "*Width will be set to the same value as length");
    hotTubsPanel.add(messageArea);

    calculateVolume.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (roundTub.isSelected()) {
                widthField.setText(lengthField.getText());
            }

            ValidationResult result = validateFields(new JTextField[] {
                    lengthField, widthField, depthField });

            String errors = "";
            if (result.filled != 3) {
                errors += "Please fill out all fields! ";
            }
            if (result.valid != 3 && result.filled != result.valid) {
                errors += "Please enter valid numbers!";
            }

            if (errors != "") {
                messageArea.setText(errors);
                messageArea.setVisible(true);
            } else {
                messageArea.setVisible(false);

                double length = Double.parseDouble(lengthField.getText());
                double width = Double.parseDouble(widthField.getText());
                double depth = Double.parseDouble(depthField.getText());

                double volume;
                if (roundTub.isSelected()) {
                    volume = Math.PI * Math.pow(length / 2.0, 2) * depth;
                } else {
                    volume = Math.PI * Math.pow(length * width, 2) * depth;
                }
                volumeField.setText(df.format(volume));
            }
        }
    });

    ActionListener tubsListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == roundTub) {
                widthLabel.setEnabled(false);
                widthField.setEnabled(false);
                widthField.setText(lengthField.getText());
                messageArea.setText("Tub's width set to length");
                messageArea.setVisible(true);
            } else if (e.getSource() == ovalTub) {
                widthLabel.setEnabled(true);
                widthField.setEnabled(true);
                messageArea.setVisible(false);
            }
        }
    };
    roundTub.addActionListener(tubsListener);
    ovalTub.addActionListener(tubsListener);
}
+2  A: 

Two text fields and only one result field could be confusing for the user. If there's only going to be one result field, there should only be one text field as well. The radio buttons Stian suggested would work, or even having two buttons, one for pool and one for hot tub (this would require only one click to get the other price). You should also have something in the result field indicating which one has been calculated, like "Pool price: $XX.XX" or "Hot tub price: $XX.XX". If you went with the buttons idea and labeled the buttons "Pool" and "Hot Tub", you could even do something fancy with the result, like setText(e.getActionCommand()+" price:"+price);.

Also, if (errors != "") is always going to be true. You are testing if your errors object is the same object as a new String object you are creating; it never will be. You should instead test for if (!errors.equals("")) or if (errors.length()!=0).

qmega
Ok, so I edited the program to use a radio button set up, but for some reason I get this error at the end of: poolPrice.addActionListener(priceListener); tubPrice.addActionListener(priceListener);Syntax error on token "priceListener", VariableDeclaratorId expected after this token...But this set up is what I have used for another method that works just fine, so i am confuzzled.
David
That error will usually happen when you declare the type of a parameter (in a method definition) but not its name. I don't see that happening here, so honestly I'm a bit confused as well. Could you post a little more of your code so I can see the context?
qmega
That pretty much is the whole code, it is a section, a tab for a better understanding, for a program. I can post another tab of the program, but it shouldn't have any importance to this one.
David
Could you post the full stack trace of your error(s) then?
qmega
Yeah, this is what I get:Exception in thread "main" java.lang.Error: Unresolved compilation problem: at guiTut.GuiTut.main(GuiTut.java:41)And all the error I get is it is underlined in red for the actionListener
David
I posted the main with the errors under the code at the bottom.
David
Well, one problem with your `ActionListener` is that it references `poolPrice` and `tubPrice`, which are local variables. The `actionPerformed` method will be run in a different thread (the Swing EDT), so local variables where the `ActionListener` were created won't be available. You'll need to declare `poolPrice` and `tubPrice` as private variables so that you can use them in the `ActionListener`.
qmega
tubPrice and poolPrice are radio buttons though. I added the code for the tab that I used to make this tab that doesnt have these errors, since it was just a repeat minus a few things.
David
Yes, they are radio buttons, but their addresses are still stored as variables, and right now they are stored only locally. You have to make them private (e.g. `private JRadioButton poolPrice, tubPrice;` at the top of your program). Problem with that is you're doing this stuff in `main`, which would require them to be static, but for the `ActionListener` they can't be, so you'll have to make a constructor that sets everything up.
qmega
But in my other tab, the radioButtons aren't declared as private and it works fine, that is my confusion. And when I make them private, it screws up the whole code, errors everywhere.
David
It doesn't seem like it should work, but I guess if it works ok. In that case I'm not sure where your problem is, though. If you want to upload your whole program somewhere I can run it an take a look at it; otherwise I'm not sure what's wrong.
qmega
Yeah Im not sure either, I was messing around with more code and still nothing. I uploaded the file to media fire if you have the timehttp://www.mediafire.com/?1e8kf8y1lycprle
David
Sorry, I didn't see your last comment when you posted it. If you haven't figured it out yet: I can compile your program fine; when I try to calculate the price, I get an error because it tries to parse a `double` from an empty `String` (which it gets from the tub price field). You can fix this by surrounding the parsing with a `try/catch` block.
qmega
Yeah, I am still having problems with this.
David