Solved

TextField help

Posted on 2003-11-30
10
346 Views
Last Modified: 2010-03-31
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;
            }

}
0
Comment
Question by:KevinGen
  • 6
  • 4
10 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9844967
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
 

Author Comment

by:KevinGen
ID: 9844975
Would it be possible to get my cancel and next item button back on the bottom?
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9844983
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
 
LVL 15

Accepted Solution

by:
jimmack earned 350 total points
ID: 9844986
Just for interest, is there any particular reason why you're not using Swing (JFrame, JPanel, JTextField etc.)?
0
 

Author Comment

by:KevinGen
ID: 9845000
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 15

Expert Comment

by:jimmack
ID: 9845008
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
 

Author Comment

by:KevinGen
ID: 9845017
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
 
LVL 15

Expert Comment

by:jimmack
ID: 9845034
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
 

Author Comment

by:KevinGen
ID: 9845320
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
 
LVL 15

Expert Comment

by:jimmack
ID: 9845397
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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now