need help with the GUI of my program...
i want it to be like this:
[IMG]http://img256.imageshack.us/img256/8919/faceeq9.th.jpg[/IMG]
but this is what it shows.. hehe
[IMG]http://img440.imageshack.us/img440/8486/caym9.th.jpg[/IMG]

im having a hard time with the GridLayout coz i think ie. GridLayout 3,4 doesnt appear to be 3,4..

thankss...

import java.awt.*;
import java.awt.event.*;

public class Project extends Frame implements ActionListener {

		private TextArea display;
		private TextArea msg;
		
	public static void main (String args[]){
		Project cl = new Project();
		FlowLayout fl = new FlowLayout();
		Panel panel1 = new Panel();
		Panel panel2 = new Panel();
		Panel panel3 = new Panel();
		Panel panel4 = new Panel();
		Panel panel5 = new Panel();
		GridLayout gl = new GridLayout(1,3);
		Button quit = new Button("Quit");
		Button print = new Button("Print Total");
		
		TextArea display = new TextArea("Information");
		TextField msg = new TextField("%");
		
		cl.setSize(650,500); // window size
		cl.setLayout(new BorderLayout());

		// PANEL 1
		panel1.add(new Label ("Cash Register for Carinderias :)"));
		
	    // PANEL 2 
	    panel2.setLayout(new GridLayout(10,0));
	    panel2.setSize(450,600);
	    panel2.add(new Label("Rice"));
	    panel2.add(new Label("Softdrinks"));
	    panel2.add(new Label("Meat"));
	    panel2.add(new Label("Soup"));
	    panel2.add(new Label("Arroz Caldo"));
	    panel2.add(new Label("Batchoy"));
		//List list = new List (5, false); //list of items
	      //list.add ("Rice");
	      //list.add ("Softdrinks (8oz)");
	      //list.add ("Meat");
	     // list.add ("Soup");
	     // list.add ("Arroz Caldo");
	   // panel2.add(list);
	   //panel2.setLayout(new GridLayout(10,0));
		msg.setBackground(Color.GREEN);
		panel2.add(new Label("How many percent discount?"));
		panel2.add(msg);
     	panel2.add(new Button ("Give Discount"));
		
		// PANEL 3 
		Choice choose = new Choice();
		choose.add("Dine In");
		choose.add("Take Out");
		panel3.add(choose);
		//panel3.add(new Checkbox("Dine in"));
		//panel3.add(new Checkbox("Take out"));
		panel3.add(print);
		
		// PANEL 4
		//panel4.add(new TextArea("Information"));
		display.setBackground(Color.YELLOW);
		panel4.add(display);
		
		// PANEL 5 
		panel5.add(new Button ("Next"), BorderLayout.CENTER);
		panel5.add(quit);
		quit.addActionListener(this);
		
		// the GridLayout consist of Panels
		cl.setLayout(new GridLayout(5,5));
		cl.add(panel1, BorderLayout.NORTH);
		cl.add(panel2, BorderLayout.CENTER);
		cl.add(panel3, BorderLayout.CENTER);
		cl.add(panel4, BorderLayout.CENTER);
		cl.add(panel5, BorderLayout.SOUTH);
		cl.setTitle("Cash Register");
		cl.setVisible(true);

	}

	// Prints the total in the display window
	public void actionPerformed(ActionEvent e)
	{
		if ("Print Total".equals(e.getActionCommand()))
		{
		String myText="";
		myText=msg.getText();
		display.append(myText);
		
	    if ("Quit".equals(e.getActionCommand()))
		{
			System.exit(0);
		}
		}
	}
}

can you make those photos larger?

No need for it, if you will try that program you would understand what he is about....


GridLayout is not best solution for the layout you try to do. You should consider BoxLayout, GridBagLayout or combination of more then one. So if we look at first image you can split it on 3 main sections

  1. Labels, text fields, combobox and buttons in top part
  2. Text area from middle
  3. Two buttons from bottom

Then top part can be split on following

  • 3 text fields with their labels from left
  • text field with label from middle together with combo box(this can be also split but not necessary)
  • 2 buttons from the right

Hope that helps

Comments
Useful, well planned reply

I didnt know that BoxLayout and GridBagLayout exists... Hehe ill try that.. thanks!! it really helped me.. :)

Im done with the simple GUI with the help of GridBagLayout... thanks guys..

Output: [IMG]http://img98.imageshack.us/img98/5559/fnlgg8.th.jpg[/IMG]

import java.awt.*;
import java.awt.event.*;


public class testing extends Frame
{
	private Label rice = new Label("Rice --- P10/cup");
	private TextField rice1 = new TextField("");
	private Label egg = new Label("Egg --- P15");
	private TextField egg1 = new TextField("");
	private Label softdrink = new Label("Softdrink --- P15");
	private TextField softdrink1 = new TextField("");
	private Label tapsilog = new Label("Tapsilog --- P15");
	private TextField tapsilog1 = new TextField("");
	private Label cornedbeef = new Label("Corned Beef --- P25");
	private TextField cornedbeef1 = new TextField("");
	private Label chopsuey = new Label("Chopsuey --- P20");
	private TextField chopsuey1 = new TextField("");
	
	private Label discount = new Label("Discount:");
	private TextField discount1 = new TextField("");
	private Button givediscount = new Button("Give Discount");
	
	private Button print = new Button("Print");
	private Button quit = new Button("Quit");
	private Button next = new Button("Next");
	
	private TextArea output = new TextArea("",15,25);
	
	public testing()
 {
  super( "Cash Register" );
  GridBagLayout gridbag = new GridBagLayout();
  GridBagConstraints c = new GridBagConstraints();
  setLayout( gridbag );
  c.weightx = 0;
  c.weighty = 0;
  c.ipadx = 2;

  // Rice
  c.gridx = 0;
  c.gridy = 1;
  c.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints( rice, c );
  add( rice );
  c.gridx = 1;
  c.anchor = GridBagConstraints.WEST;
  gridbag.setConstraints( rice1, c );
  add( rice1 );

  // Egg
  c.gridx = 0;
  c.gridy = 2;
  c.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints( egg, c );
  add( egg );
  c.gridx = 1;
  c.anchor = GridBagConstraints.WEST;
  gridbag.setConstraints( egg1, c );
  add( egg1 );

  // Softdrink
  c.gridx = 0;
  c.gridy = 3;
  c.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints( softdrink, c );
  add( softdrink );
  c.gridx = 1;
  c.anchor = GridBagConstraints.WEST;
  gridbag.setConstraints( softdrink1, c );
  add( softdrink1 );

  // tapsilog
  c.gridx = 0;
  c.gridy = 4;
  c.anchor = GridBagConstraints.NORTHEAST;
  gridbag.setConstraints( tapsilog, c );
  add( tapsilog );
  c.gridx = 1;
  c.anchor = GridBagConstraints.NORTHWEST;
  gridbag.setConstraints( tapsilog1, c );
  add( tapsilog1 );
  
  // corned beef
  c.gridx = 0;
  c.gridy = 5;
  c.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints( cornedbeef, c );
  add( cornedbeef );
  c.gridx = 1;
  c.anchor = GridBagConstraints.WEST;
  gridbag.setConstraints( cornedbeef1, c );
  add( cornedbeef1 );

  // chopsuey
  c.gridx = 0;
  c.gridy = 6;
  c.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints( chopsuey, c );
  add( chopsuey );
  c.gridx = 1;
  c.anchor = GridBagConstraints.WEST;
  gridbag.setConstraints( chopsuey1, c );
  add( chopsuey1 );
  
 // dine in or take out
  c.gridx = 0;
  c.gridy = 7;
  c.anchor = GridBagConstraints.EAST;
  Choice dinein = new Choice();
  dinein.add("Dine In");
  dinein.add("Take Out");
  gridbag.setConstraints( dinein, c );
  add( dinein );
 

  // discount
  c.gridx = 0;
  c.gridy = 8;
  c.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints( discount, c );
  add( discount );
  c.gridx = 1;
  c.anchor = GridBagConstraints.WEST;
  gridbag.setConstraints( discount1, c );
  add( discount1 );
  c.gridx = 2;
  c.anchor = GridBagConstraints.CENTER;
  gridbag.setConstraints( givediscount, c );
  add( givediscount );
  
  // output and print
  c.gridx = 0;
  c.gridy = 9;
  c.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints( output, c );
  add( output );
  c.gridx = 3;
  c.anchor = GridBagConstraints.WEST;
  gridbag.setConstraints( print, c );
  add( print );
  
  // next and quit
  c.gridx = 0;
  c.gridy = 10;
  c.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints( next, c );
  add( next );
  c.gridx = 1;
  c.anchor = GridBagConstraints.WEST;
  gridbag.setConstraints( quit, c );
  add( quit );
  
  this.setLocation( 100, 100 );
  this.setSize( 500, 500 );
  this.setVisible( true );
 }
 
 public static void main (String args[]) {
 	testing test = new testing();
 	
 	test.addWindowListener
            (new WindowAdapter()
                {
                    public void windowClosing (WindowEvent e)
                    {
                        System.exit (0);
                    }
 });
 }
 
}

another question: im going to put something in all the text field... how can i transfer them(text field items) to the TextArea(output) by clicking the PRINT button?

Similar to this:
[IMG]http://img528.imageshack.us/img528/5486/secondug1.th.jpg[/IMG]

oh, can i put labels in the TextArea? or a table??

You have to associate apropriate action with your Print button which will retrive all data from labels and text fields, then display them in text area

To collect your data use following methods
JTextField.getText() - will return string inside text component. The method is inherited from JTextComponent
JLabel.getText() - returns the text string that the label displays.

PS: Next time you realy need to give bigger images or provide binoculars to see these miniatures

waa.. i dont know understand about public String getText()...
can you give me a sample of it??

this is my action for the PRINT button:

public void actionPerformed (ActionEvent e)
{
  if(e.getActionCommand()=="PRINT")
  {  String temp="";
      temp=rice1.getText();        
      output.append(temp);
      rice1.setText("");
  }

not working though :(

waa.. i dont know understand about public String getText()...
can you give me a sample of it??

this is my action for the PRINT button:

public void actionPerformed (ActionEvent e)
{
  if(e.getActionCommand()=="PRINT")
  {  String temp="";
      temp=rice1.getText();        
      output.append(temp);
      rice1.setText("");
  }

not working though :(

That's because your button says "Print", not "PRINT". Your check on the action command is failing because of that.
Another way to determine which component is making the call is to use e.getSource() and compare that to your components

public void actionPerformed (ActionEvent e)
{
  if(e.getSource() == print)
  {  String temp="";
      temp=rice1.getText();        
      output.append(temp);
      rice1.setText("");
  }

You can create an HTML table, but if you are wanting an interactive JTable you'll need to place that as a separate component.

umm ok hehehe. thanks...

another question: my input is an integer.. but i use getText() to transfer that input to the TextArea... Now i want to multiply that integer to a certain number... is there a any way?

i got an error "cannot find symbol
symbol : variable integerAsString" when i added "int temp = Integer.parseInt(integerAsString);...."

where should i put that? hehe...

public void actionPerformed (ActionEvent e)
{
  if(e.getSource() == print)
  { // String temp="", temp2="", temp3="", temp4="", temp5="", temp6="";
  
  int temp = Integer.parseInt(integerAsString);
  int temp2 = Integer.parseInt(integerAsString);
  int temp3 = Integer.parseInt(integerAsString);
  int temp4 = Integer.parseInt(integerAsString);
  int temp5 = Integer.parseInt(integerAsString);
  int temp6  = Integer.parseInt(integerAsString);
  
      int ricetot = 10;
      int eggtot = 15;  
      int softdrinktot =15;
      int tapsilogtot = 15; 
      int cornedbeeftot = 25;
      int chopsueytot = 20;
      int discounttot=0;
      
      temp=rice1.getText(); 
      temp2=egg1.getText(); 
      temp3=softdrink1.getText(); 
      temp4=tapsilog1.getText(); 
      temp5=cornedbeef1.getText(); 
      temp6=chopsuey1.getText(); 
      
      int ricefinal;
      int eggfinal;
      int softdrinkfinal;
      int tapsilogfinal;
      int cornedbeeffinal;
      int chopsueyfinal;
     
      ricefinal = ricetot * temp;
      eggfinal = eggtot * temp2;
      softdrinkfinal = softdrinktot * temp3;
      tapsilogfinal = tapsilogtot * temp4;
      cornedbeeffinal = cornedbeeftot * temp5;
      chopsueyfinal = chopsueytot * temp6;
      
      output.append("Rice:" +ricefinal);
      output.append("\nEgg:" +eggfinal);
      output.append("\nSoftdrink:" +softdrinkfinal);
      output.append("\nTapsilog:" +tapsilogfinal);
      output.append("\nCorned Beef:" +cornedbeeffinal);
      output.append("\nChopsuey:" +chopsueyfinal);

That was merely an example of coverting a string, which you get from the text field, into an int value. You need to pass in your own string variable.

updated.. is this right?

public void actionPerformed (ActionEvent e)
{
  if(e.getSource() == print)
  {  String temp="", temp2="", temp3="", temp4="", temp5="", temp6="";
  
      temp=rice1.getText(); 
      temp2=egg1.getText(); 
      temp3=softdrink1.getText(); 
      temp4=tapsilog1.getText(); 
      temp5=cornedbeef1.getText(); 
      temp6=chopsuey1.getText(); 
  
      int atemp = Integer.parseInt(temp);
      int atemp2 = Integer.parseInt(temp2);
      int atemp3 = Integer.parseInt(temp3);
      int atemp4 = Integer.parseInt(temp4);
      int atemp5 = Integer.parseInt(temp5);
      int atemp6 = Integer.parseInt(temp6);
  
   	  int ricetot = 10;
      int eggtot = 15;  
      int softdrinktot =15;
      int tapsilogtot = 15; 
      int cornedbeeftot = 25;
      int chopsueytot = 20;
      int discounttot=0;
      
      int ricefinal;
      int eggfinal;
      int softdrinkfinal;
      int tapsilogfinal;
      int cornedbeeffinal;
      int chopsueyfinal;
     
      ricefinal = ricetot * atemp;
      eggfinal = eggtot * atemp2;
      softdrinkfinal = softdrinktot * atemp3;
      tapsilogfinal = tapsilogtot * atemp4;
      cornedbeeffinal = cornedbeeftot * atemp5;
      chopsueyfinal = chopsueytot * atemp6;
      
      output.append("Rice:" +ricefinal);
      output.append("\nEgg:" +eggfinal);
      output.append("\nSoftdrink:" +softdrinkfinal);
      output.append("\nTapsilog:" +tapsilogfinal);
      output.append("\nCorned Beef:" +cornedbeeffinal);
      output.append("\nChopsuey:" +chopsueyfinal);

i was able to complete the process.. but when i executed it, there were some "errors"...

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:468)
at java.lang.Integer.parseInt(Integer.java:497)
at testing.actionPerformed(testing.java:173)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

Read the api doc on Integer.parseInt(). It indicates that it will throw a NumberFormatException if it cannot convert the string to an inv value. Your program needs to handle this. You can't perform math on nulls or empty strings, so it's up to you to decide what to do if that text can't be converted to an int.

An example of converting text field inputs to ints

JTextField txtField = new JTextField();
String textValue = txtField.getText();
int value = 0;   // starting with a default of 0
try {
    value = Integer.parseInt(textValue);
} catch (NumberFormatException nfe){
    // you can either leave it zero or tell the user it's not a good value
    // or whatever you need to do if the text can't be parsed to long.
    System.out.println("bad value");
}
System.out.println("value="+value);

ohhh.. yes i left the textfield blank.. hehehe its ok now.. thank you so much for the help and time!! :)

but im not gonna mark it as solved yet huh.. hehe

in the textfield, i have the number zero as a default entry.. now if i click on it, i want the zero to disappear.. and if i click on the other textfield, the zero in the first textfield will appear again...unless i input a new entry/number..is that possible?

Typically you would select the 0 when the user clicks in the field rather than erasing it. That way they can replace it by merely typing a value,but if they leave the field you don't have to reset it. You can find some examples of working with text selection here: http://www.exampledepot.com/egs/javax.swing.text/Selection.html

That site is very useful for finding short code examples for many things and is worth keeping bookmarked :)

cannot find symbol when i included the xxxx.replaceSelection("replacement text");....??

how can i save a file using a save button? heheh

cannot find symbol when i included the xxxx.replaceSelection("replacement text");....??

Then "xxxx" is probably not a text component.

how can i save a file using a save button? heheh

I'm sure the site that I linked above has examples of using a BufferedWriter to write a text file as well. Writing to a file should have been covered in your class if you are expected to do that.

if (e.getSource() == save) {
  	try {
        BufferedWriter out = new BufferedWriter(new FileWriter("savedoutput.txt", true));
        [B]out.write(output);[/B]
        out.close();
    } catch (IOException e33) {
    }
  }

can anyone tell me what this out.write(output); does? thanks..

the code's not working...

It's writing whatever "output" is to a buffered file output stream. If you just copied that in from somewhere, and you don't have a variable "output", then it certainly won't work.

aww "output" exist in my code btw. hehe. so that "output" will be saved to savedoutput.txt. right?

there was an error... cannot find symbol: method write(java.awt.TextArea) ???

You need to read and think about these errors a bit. Is there a version of the write() method that takes a component? Nope, all of them take char or String data. A TextArea is not the same.

Take the time to examine the API documents of the classes you are using. Those explicitly lay out how you may interact with all of the JDK classes that you use. Make sure you have those bookmarked.

got it. thanks :)

In GridBagLayout.. i have the code:

c.gridx = 0;
    c.gridy = 1;
    c.anchor = GridBagConstraints.[B]EAST[/B];
    gridbag.setConstraints( rice, c );
    add( rice ); //the Label
    c.gridx = 1;
    c.anchor = GridBagConstraints.[B]WEST[/B];
    gridbag.setConstraints( rice1, c );
    add( rice1 ); //the TextField

screenshot
im just wondering why the Rice(Label) is on the west side and Rice(TextField) on the east side?

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