TextField help

I have four fields that go out of alignment when I try to resize them to match the other fields in the program. quantityField, unitPriceField and the two others. Can you help?



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

public class CashRegFinal extends Frame implements ActionListener
{
      // declare variables
      int quantity;
      double unitPrice, extendedPrice, total;
      double grandTotal = 0, totalTax = 0, subTotal = 0, dailyTotal = 0;
      double quantityInT = 0, priceInT = 0, priceIn = 0, quantityIn = 0;
      double subtotalT, nextItemtotal = 0,checkoutSubtotal = 0,dailyCashout, price;
      double salesTax = .06;
      //Color blue = new Color(255, 90, 90);

                        Panel ButtonPanel = new Panel();
                              Label quantLabel = new Label("Quantity Taxable");
                              TextField quantityField = new TextField(5);
                              Label unitPriceLabel = new Label("Unit Price");
                              TextField unitPriceField = new TextField(5);
                              Label extendPriceLabel = new Label("Extended Price");
                              Button nextItemButton = new Button("Next Item");
                              Button cancelButton = new Button("Cancel");

                        Panel ButtonPanelTwo = new Panel();
                              Label quantTLabel = new Label("Quantity NonTaxable");
                              TextField quantityTField = new TextField(5);
                              Label unitPriceTLabel = new Label("Unit Price");
                              TextField unitPriceTField = new TextField(5);
                              Label extendPriceTLabel = new Label("Extended Price");
                              Button nextItemTButton = new Button("Next Item");
                              Button cancelTButton = new Button("Cancel");
                        Panel InputPanel = new Panel();
                              Label dailyCashoutLabel = new Label("Daily Total");
                              TextField dailyCashoutField = new TextField(5);
                              Label totalDueLabel = new Label("Total Amount Due");
                              TextField totalDueField = new TextField(5);
                              CheckboxGroup options = new CheckboxGroup();
                              Checkbox cash = new Checkbox("Cash",false,options);
                              Checkbox charge = new Checkbox("Charge",false,options);
                              Checkbox hidden = new Checkbox("",true,options);

                        Panel FinishPanel = new Panel();
                              Button checkOutButton = new Button("Checkout");
                              Button resetButton = new Button("Cancel Order");

                        Panel LastButtonPanel = new Panel();
                              Button dailyCashoutButton = new Button("Daily Cashout");
                              Button exitProgramButton = new Button("Exit");


      public CashRegFinal()
      {
                  //set layouts for Frame and five Panels
                  this.setLayout(new BorderLayout());
                  LastButtonPanel.setLayout( new FlowLayout());

                  ButtonPanel.setLayout(new GridLayout(8,2));
                  ButtonPanelTwo.setLayout(new GridLayout(8,2));
                  InputPanel.setLayout (new GridLayout(3,1));
                  FinishPanel.setLayout( new FlowLayout());

                  setBackground(Color. blue);
                  quantityField.requestFocus();

                  //add components to ButtonPanel
                  ButtonPanel.add(quantLabel);
                  ButtonPanel.add(quantityField);
                  ButtonPanel.add(unitPriceLabel);
                  ButtonPanel.add(unitPriceField);
                  ButtonPanel.add(extendPriceLabel);

                  //add components to ButtonPanel
                  ButtonPanelTwo.add(quantTLabel);
                  ButtonPanelTwo.add(quantityTField);
                  ButtonPanelTwo.add(unitPriceTLabel);
                  ButtonPanelTwo.add(unitPriceTField);
                  ButtonPanelTwo.add(extendPriceTLabel);

             Panel dcGridPanel = new Panel(new GridLayout(2, 1));
                  Panel dcLabelPanel = new Panel(new FlowLayout());
                  dcGridPanel.add(dailyCashoutLabel);
                  dcGridPanel.add(dailyCashoutField);
                  dcLabelPanel.add(dcGridPanel);
                  InputPanel.add(dcLabelPanel);
            Panel tdGridPanel = new Panel(new GridLayout(2, 1));
                  Panel tdLabelPanel = new Panel(new FlowLayout());
                  tdGridPanel.add(totalDueLabel);
                   tdGridPanel.add(totalDueField);
                  tdLabelPanel.add(tdGridPanel);
                  InputPanel.add(tdLabelPanel);
            Panel cashChargePanel = new Panel(new FlowLayout());
                  cashChargePanel.add(cash);
                  cashChargePanel.add(charge);
                  InputPanel.add(cashChargePanel);
            Panel nextItemButtonPanel = new Panel(new FlowLayout());
                  nextItemButtonPanel.add(nextItemButton);
                  nextItemButtonPanel.add(cancelButton);
                  ButtonPanel.add(nextItemButtonPanel);

            Panel nextItemTButtonPanel = new Panel(new FlowLayout());
                  nextItemTButtonPanel.add(nextItemTButton);
                  nextItemTButtonPanel.add(cancelTButton);
                  ButtonPanelTwo.add(nextItemTButtonPanel);

                  //add components to FinishPanel
                  FinishPanel.add(checkOutButton);
                  FinishPanel.add(resetButton);

                  //add components to LastButtonPanel
                  LastButtonPanel.add(dailyCashoutButton);
                  LastButtonPanel.add(exitProgramButton);

                  //add Panels to Frame
                  add(LastButtonPanel, BorderLayout.NORTH);
                  add(ButtonPanel, BorderLayout.WEST);
                  add(ButtonPanelTwo, BorderLayout.EAST);
                  add(InputPanel, BorderLayout.CENTER);
                  add(FinishPanel, BorderLayout.SOUTH);

                  //add ActionListener
                  nextItemButton.addActionListener(this);
                  cancelButton.addActionListener(this);
                  checkOutButton.addActionListener(this);
                  resetButton.addActionListener(this);
                  dailyCashoutButton.addActionListener(this);
                  exitProgramButton.addActionListener(this);
                  nextItemTButton.addActionListener(this);
                  cancelTButton.addActionListener(this);

                        //WindowClosing method() will allow the user to  click the Close button
                        addWindowListener(
                        new WindowAdapter()
                        {
                              public void windowClosing(WindowEvent e)
                              {
                              System.exit(0);
                              }
                        }
                        );


      }
      public static void main(String[] args)
      {

            CashRegFinal f = new CashRegFinal();
             f.setBounds(400, 100, 500, 425);

            f.setTitle("      Cash Register Simulator");
            f.setVisible(true);
      }


      public void clearFields()//clear fields and reset focus for cancel button
      {
            quantityField.setText("");
            unitPriceField.setText("");
            quantityField.requestFocus();

      }

      public void clearFieldsT()//clear fields and reset focus for cancel buttonT
      {
            quantityTField.setText("");
            unitPriceTField.setText("");
            quantityTField.requestFocus();

      }

      public void clearFieldsCancelOrder()//clear fields and reset focus for cancel buttonT
      {

            quantityTField.setText("");
            unitPriceTField.setText("");
            quantityField.setText("");
            unitPriceField.setText("");
            quantityField.requestFocus();
            extendPriceLabel.setText("Extended Price");
            extendPriceTLabel.setText("Extended Price");
            totalDueField.setText("Thank You");
            nextItemtotal = 0;
            subtotalT = 0;
      }

      public void actionPerformed(ActionEvent e)
      {


            if (e.getSource() == cancelButton)  //clear text fields for cancel button
            {
            clearFields();
            }

            if (e.getSource() == cancelTButton)  //clear text fields for cancel ButtonT
            {
            clearFieldsT();
            }

            if (e.getSource() == exitProgramButton)  //clear text fields for Exit Button
            {
            System.exit(0);
            }

            if (e.getSource() == resetButton)  //cancels order
            {
                  clearFieldsCancelOrder();
            }

            if (e.getSource() == nextItemButton)

            {
                  try
                  {
                        priceIn = getPrice();
                        quantityIn = getQuantity();
                        nextItemtotal = nextItemtotal + (quantityIn * priceIn);
                  }

                  catch (NumberFormatException ex)
                  {
                        extendPriceLabel.setText(      "");
                        unitPriceField.setText("");
                        quantityField.setText("");
                  }

                        extendPriceLabel.setText("SubTotal: " + (nextItemtotal));

                  clearFields();

            }

                  if (e.getSource() == nextItemTButton)
                  {
                              try
                              {
                                    priceInT = getPriceT();
                                    quantityInT = getQuantityT();
                                    subtotalT = subtotalT + (quantityInT * priceInT);
                              }

                              catch (NumberFormatException ex)
                              {
                                    extendPriceTLabel.setText(      "");
                                    unitPriceTField.setText("");
                                    quantityTField.setText("");
                              }

                  extendPriceTLabel.setText("SubTotal: " + (subtotalT));

                  }

                  if (e.getSource() == checkOutButton)
                  {
                  try
                  {
                        total = (nextItemtotal * salesTax);
                        checkoutSubtotal = ((nextItemtotal + total) + subtotalT);
                        clearFields();
                        clearFieldsT();
                  }

                  catch (NumberFormatException ex)
                  {

                  }

                  totalDueField.setText("" + (checkoutSubtotal));

                  }

                  if (e.getSource() == dailyCashoutButton)
                  {
                  try
                  {
                        dailyTotal = dailyTotal + (checkoutSubtotal);
                        dailyCashoutField.setText("" + dailyTotal);

                  }

                  catch (NumberFormatException ex)
                  {

                  }

                  }
      }

      public double getPrice()
      {
            double priceIn = Double.parseDouble(unitPriceField.getText());

            return priceIn;
      }

      public double getQuantity()
      {
            double quantityIn = Double.parseDouble(quantityField.getText());
            return quantityIn;
      }




      public double getPriceT()
      {
            double priceInT = Double.parseDouble(unitPriceTField.getText());
            return priceInT;
      }


      public double getQuantityT()
      {
            double quantityInT = Double.parseDouble(quantityTField.getText());
            return quantityInT;
            }

}
KevinGenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimmackCommented:
Would adding these TextFields into FlowLayout panels do what you need (as you've done with the daily total and total amount due text fields), or are you more concerned with positioning of the fields?

If the first solution is adequate, you need:

        //add components to ButtonPanel
        ButtonPanel.add(quantLabel);
        Panel qf1Panel = new Panel(new FlowLayout());
        qf1Panel.add(quantityField);
//        ButtonPanel.add(quantityField);
        ButtonPanel.add(qf1Panel);
        ButtonPanel.add(unitPriceLabel);
        Panel up1Panel = new Panel(new FlowLayout());
        up1Panel.add(unitPriceField);
//        ButtonPanel.add(unitPriceField);
        ButtonPanel.add(up1Panel);
        ButtonPanel.add(extendPriceLabel);

and similar for ButtonPanelTwo.
0
KevinGenAuthor Commented:
Would it be possible to get my cancel and next item button back on the bottom?
0
jimmackCommented:
Looks like you might have deleted too much ;-)

Here is the whole code that I tried between "//add components to ButtonPanel" and "//add components to FinishPanel"

        //add components to ButtonPanel
        ButtonPanel.add(quantLabel);
        Panel qf1Panel = new Panel(new FlowLayout());
        qf1Panel.add(quantityField);
//        ButtonPanel.add(quantityField);
        ButtonPanel.add(qf1Panel);
        ButtonPanel.add(unitPriceLabel);
        Panel up1Panel = new Panel(new FlowLayout());
        up1Panel.add(unitPriceField);
//        ButtonPanel.add(unitPriceField);
        ButtonPanel.add(up1Panel);
        ButtonPanel.add(extendPriceLabel);

        //add components to ButtonPanel
        ButtonPanelTwo.add(quantTLabel);
        Panel qf2Panel = new Panel(new FlowLayout());
        qf2Panel.add(quantityTField);
//        ButtonPanelTwo.add(quantityTField);
        ButtonPanelTwo.add(qf2Panel);
        ButtonPanelTwo.add(unitPriceTLabel);
        Panel up2Panel = new Panel(new FlowLayout());
        up2Panel.add(unitPriceTField);
//        ButtonPanelTwo.add(unitPriceTField);
        ButtonPanelTwo.add(up2Panel);
        ButtonPanelTwo.add(extendPriceTLabel);

        Panel dcGridPanel = new Panel(new GridLayout(2, 1));
        Panel dcLabelPanel = new Panel(new FlowLayout());
        dcGridPanel.add(dailyCashoutLabel);
        dcGridPanel.add(dailyCashoutField);
        dcLabelPanel.add(dcGridPanel);
        InputPanel.add(dcLabelPanel);
        Panel tdGridPanel = new Panel(new GridLayout(2, 1));
        Panel tdLabelPanel = new Panel(new FlowLayout());
        tdGridPanel.add(totalDueLabel);
        tdGridPanel.add(totalDueField);
        tdLabelPanel.add(tdGridPanel);
        InputPanel.add(tdLabelPanel);
        Panel cashChargePanel = new Panel(new FlowLayout());
        cashChargePanel.add(cash);
        cashChargePanel.add(charge);
        InputPanel.add(cashChargePanel);
        Panel nextItemButtonPanel = new Panel(new FlowLayout());
        nextItemButtonPanel.add(nextItemButton);
        nextItemButtonPanel.add(cancelButton);
        ButtonPanel.add(nextItemButtonPanel);

        Panel nextItemTButtonPanel = new Panel(new FlowLayout());
        nextItemTButtonPanel.add(nextItemTButton);
        nextItemTButtonPanel.add(cancelTButton);
        ButtonPanelTwo.add(nextItemTButtonPanel);

        //add components to FinishPanel
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

jimmackCommented:
Just for interest, is there any particular reason why you're not using Swing (JFrame, JPanel, JTextField etc.)?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KevinGenAuthor Commented:
In my book I'm using this is where I was at the time I decided to try this. I have read those sections now but I had already done this much work on this and couldn't see starting over. Believe it or not I'm more familiar with this. Why? Is it alot easier the other way?
0
jimmackCommented:
Not if you're happy with the way it is working at the moment.  Swing just provides more alternatives (eg. more layout managers) and the facilities to change the "look and feel" etc.  Although it would be possible to add Swing components into your existing code, it is not advisable to mix Swing and AWT.  Don't worry about it for now, I was just interested ;-)

How's it looking?
0
KevinGenAuthor Commented:
It looks great. Now that's what I was looking for. If you get the time and want to try to set it up a different way (if it's within what I know, don't laugh) then I would like to see it.

Thanks again for the help you have given me over the last two days and hopefully you will be around if I need more in the future. You are very good at explaining and making me think rather than just giving me answers.

Kevin
0
jimmackCommented:
No problem :-)

It's always a pleasure the help someone who is actually interested and doesn't just want the answer (or usually their homework ;-)) handed to them on a plate :-)
0
KevinGenAuthor Commented:
I studied what you said about the panels and you said to ask if I didn’t understand. I want to make sure I have this right.

I have this block of code below

      //add components to InputPanel
                      Panel dailyCashoutGridPanel = new Panel(new GridLayout(2, 1));
                      Panel dailyCashoutLabelPanel = new Panel(new FlowLayout());
                          dailyCashoutGridPanel.add(dailyCashoutLabel);
                          dailyCashoutGridPanel.add(dailyCashoutField);
                          dailyCashoutLabelPanel.add(dailyCashoutGridPanel);
                               InputPanel.add(dailyCashoutLabelPanel);

You are saying that       “Panel dailyCashoutGridPanel = new Panel(new GridLayout(2, 1));”
Makes my GridLayout, which will give me two vertical spaces or boxes

In the top space goes “ dailyCashoutGridPanel.add(dailyCashoutLabel);”
Which is my label that says “daily total”

Then in the bottom vertical space goes my “dailyCashoutGridPanel.add(dailyCashoutField);
That puts my text Field below the label


The following line “dailyCashoutLabelPanel.add(dailyCashoutGridPanel);”
Adds the   “dailyCashoutGridPanel “                        
To the “dailyCashoutLabelPanel”

Then the line “InputPanel.add(dailyCashoutLabelPanel);”

Adds “dailyCashoutLabelPanel”
To the “InputPanel”

The line   “Panel dailyCashoutLabelPanel = new Panel(new FlowLayout());”
Adds the panel “Panel dailyCashoutLabelPanel”  to its own FlowLayout which will keep the text field at the size it is set to.

Am I right. I want to understand this.
No more questions

0
jimmackCommented:
Everything is right, up to this point:

The line   “Panel dailyCashoutLabelPanel = new Panel(new FlowLayout());”
Adds the panel “Panel dailyCashoutLabelPanel”  to its own FlowLayout which will keep the text field at the size it is set to.

All this line does is to create another Panel with a FlowLayout.  It doesn't add anything to it.  The reason it is there is to contain the dailyCashoutGridPanel.  This prevents the grid from expanding to fit the space available (and distorting the contents of the two grid cells).

Basically, with the GridLayout inside the FlowLayout, the size of the cells is dictated by the size of the largest component in one of the cells (in this case, the label and text area are very similar in size.  It is probably the text area that is slightly larger).

Without the FlowLayout, the GridLayout would expand to fill the space available in the InputPanel.  This, in turn, would mean that the cells would expand (in a GridLayout, all the cells are the same size, so if you have a few small components and then you add a large one, the small components tend to look a bit stretched).

If you want to try out some of these ideas, create some simple panels with different layouts and then add some Buttons.  The buttons will normally take up as much space as they can (with the exception of FlowLayout ;-)).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.