Results 1 to 19 of 19

Thread: JFrame Help!!!

  1. #1
    hamad3914 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default JFrame Help!!!

    Hi Guys i have this code and in this Cafe Frame i am stuck on two problems

    A. When I click on cancel, the frame must go back to the default, the way it is when it starts, but i cannot seem to remove the amount and the ID when i click cancel.

    B. When i click OK it must replace the old price and ID with the new one - for me it prints again and again .. :P

    help me lol

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.NumberFormat;
    import java.util.Random;
    
    import javax.swing.JFormattedTextField;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    
    
    public class CafeFrame extends JFrame implements ActionListener{
    	
    	private JButton b1, b2;
    	private JCheckBox c1, c2, c3;
    	private JRadioButton c5, c6, c7;
    	private JComboBox q;
    	private JLabel label1, label2,label3,label4;
    	private JPanel p1, p2, p3, p4, p5, p6, p7, p8;
    	private JFormattedTextField textbox1,textbox2,textbox3;
    
    
    	public CafeFrame()
    	{
    	// build left north panel
    	label1 = new JLabel( "Cafe: My Cafe" );
    	p1 = new JPanel();
    	p1.setLayout( new FlowLayout( FlowLayout.LEFT ) );
    	p1.add( label1 );
    	
    	// build right east panel
    	b1 = new JButton( "Ok" );
    	b2 = new JButton( "Cancel" );
    	p2 = new JPanel();
    	p2.setLayout( new GridLayout( 2, 1, 5, 5 ) );
    	p2.add( b1 );
    	p2.add( b2 );
    	
    	// build left south panel
    	label2 = new JLabel( "Payment Mode: " );
    	q = new JComboBox();
    	q.addItem( "Cash" );
    	q.addItem("Credit Card");
    	p3 = new JPanel();
    	p3.setLayout( new FlowLayout( FlowLayout.LEFT, 10, 0 ) );
    	p3.add( label2 );
    	p3.add( q );
    	
    	// build left east panel
    	label3=new JLabel("Item");
    	c1 = new JCheckBox( "Sandwich" );
    	c2 = new JCheckBox( "Tea" );
    	c3 = new JCheckBox( "Coke" );
    	p4 = new JPanel();
    	p4.setLayout( new GridLayout(4,1,3,3 ) );
    	p4.add(label3);
    	p4.add(c1);
    	p4.add(c2);
    	p4.add(c3);
    	
    	// build left west panel
    	p5 = new JPanel();
    	p5.setLayout( new GridLayout(4,1,5,5) );
    	label4=new JLabel("Quantity");
    	textbox1=new JFormattedTextField(NumberFormat.getInstance());
    	textbox2=new JFormattedTextField(NumberFormat.getInstance());
    	textbox3=new JFormattedTextField(NumberFormat.getInstance());
    	
    	p5.add(label4);
    	p5.add(textbox1);
    	p5.add(textbox2);
    	p5.add(textbox3);
    	
    	// build left center
    	p8 = new JPanel();
    	p8.setLayout( new FlowLayout( FlowLayout.CENTER, 30, 0 ) );
    	
    	// p8.setBackground( Color.white );
    	p8.add( p4 );
    	p8.add( p5 );
    	
    	// setup left panel
    	p6 = new JPanel();
    	p6.setLayout( new BorderLayout() );
    	p6.add( p1, BorderLayout.NORTH );
    	p6.add( p8, BorderLayout.CENTER );
    	p6.add( p3, BorderLayout.SOUTH );
    	
    	// setup applet layout
    	p7 = new JPanel();
    	p7.setLayout( new FlowLayout( FlowLayout.CENTER, 10, 5 ) );
    	p7.add( p6 );
    	p7.add( p2 );
    	getContentPane().add( p7 );
    	setSize( 400, 250 );
    	show();
    	b1.addActionListener(this);
    	b2.addActionListener(this);
    	
    
    }
    public static void main(String args[])
    {
    	new CafeFrame();
    
    }
    JPanel JP = new JPanel();
    JLabel JL = new JLabel();
    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(10000);
    JLabel JL1 = new JLabel("Your Reciept ID is: " + randomInt);
    
    public void actionPerformed(ActionEvent arg0) {
    	
    	if(arg0.getSource()==b1)
    	{
    		int sum = 0;
    		int a=0, b=0, c=0;
    		if(c1.isSelected())
    		{
    			a = Integer.parseInt(textbox1.getText());
    			
    		}
    		if(c2.isSelected())
    		{
    			b = Integer.parseInt(textbox2.getText());
    			
    		}
    		if(c3.isSelected())
    		{
    			c = Integer.parseInt(textbox1.getText());
    			
    		}
    		sum = (a*2);
    		sum = sum + (b * 1);
    		sum = sum + (c * 1);
    			
    		JLabel JL = new JLabel("The Total Amount is: " + sum);
    		p7.add( JP );
    		p7.add( JL );
    		p7.add( JL1 );
    		p7.repaint();
    	}
    
    		
    	if(arg0.getSource()==b2)
    	{
    		textbox1.setText("");
    		textbox2.setText("");
    		textbox3.setText("");
    		c1.setSelected(false);
    		c2.setSelected(false);
    		c3.setSelected(false);
    		q.setSelectedIndex(0);
    		JP.setVisible(false);
    	}
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Where in your actionPerformed do you set the JLabel text for the labels that show the ID or the sum? The code will do only what you tell it to.

    On another note, please for your benefit and ours, change your variable names to names that make sense. For example instead of b2, you'd be better off naming the variable cancelButton. That way your code becomes self-commenting and much easier for you and for others to debug.

  3. #3
    hamad3914 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default lol

    ok if i still do this code:

    Java Code:
    JPanel JP = new JPanel();
    JLabel JL = new JLabel();
    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(10000);
    JLabel JL1 = new JLabel("Your Reciept ID is: " + randomInt);
    in the actionPerformed ... still wen i press the cancel button the label does not disappear ...

    dude i want u to tel me hw to fix it not criticize :P

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hamad3914 View Post
    ok if i still do this code:

    Java Code:
    JPanel JP = new JPanel();
    JLabel JL = new JLabel();
    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(10000);
    JLabel JL1 = new JLabel("Your Reciept ID is: " + randomInt);
    in the actionPerformed ... still wen i press the cancel button the label does not disappear ...

    dude i want u to tel me hw to fix it not criticize :P


    DUDE: I told you how to fix it. Where in the actionPerformed, specifically the if block for button "b2":

    Java Code:
    if(arg0.getSource()==b2) {
    (whatever the heck "b2" is) do you have code to change the text in the JLabels? If you take the time to add the necessary code in here, then your problems are solved.

    And my "criticism" is to help you write better code and to help us to help you. If this bothers you, then perhaps you have too thin a skin to be asking for free advice in public forums.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    please for your benefit and ours, change your variable names to names that make sense.
    Did you even read that piece of advice?

    Java Code:
    JPanel JP = new JPanel();
    JLabel JL = new JLabel();
    How do those variable names follow the above advice?

    Also, where in any text box of example have you ever seen the first character of the variable name capitalized? Follow existing standards if you want help for others.

  6. #6
    hamad3914 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default hahaaaa

    Bro see the code

    Java Code:
    if(arg0.getSource()==b2)
    	{
    		textbox1.setText("");
    		textbox2.setText("");
    		textbox3.setText("");
    		c1.setSelected(false);
    		c2.setSelected(false);
    		c3.setSelected(false);
    		q.setSelectedIndex(0);
    		JP.setVisible(false);
    	}
    isnt this there in my code? :S

    thanks for the criticism btw :P ;)

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Java Code:
    textbox1.setText("");
    I can never remember how to use JFormattedTextField. I never remember whether to use setText() or I think there is also a method setValue().

    Also, I doubt that "" is a valid value for a number, so it may be ignored. Try resetting it to "0".

  8. #8
    hamad3914 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    @camikr ... i did it man its my code hahahaaha i knw wht it is and hw to name variables with meaningful names dnt tel me tht :P plus i gt this code for a lab assignment i dnt care wht the code is :P

  9. #9
    hamad3914 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    no the empty double quotes work fine ... :)

    its just the JL and the JL1 Labels tht dont disappear if i hit cancel :)

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    i did it man its my code hahahaaha i knw wht it is and hw to name variables
    Then use proper variable name is you want us to take the time to read the code and help.

    i knw wht it is and hw to name variables with meaningful names dnt tel me tht
    Also, spell out words. This is a forum not a chat site.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hamad3914 View Post
    Bro see the code

    Java Code:
    if(arg0.getSource()==b2)
    	{
    		textbox1.setText("");
    		textbox2.setText("");
    		textbox3.setText("");
    		c1.setSelected(false);
    		c2.setSelected(false);
    		c3.setSelected(false);
    		q.setSelectedIndex(0);
    		JP.setVisible(false);
    	}
    isnt this there in my code? :S

    thanks for the criticism btw :P ;)

    Bro,
    your sum is set in a JLabel here:
    Java Code:
    JLabel JL = new JLabel("The Total Amount is: " + sum);
    And to reset it, you must set the text in this JLabel. I see nowhere where you set the JLabel in the block from your actionPerformed method above.

    Again, the label name JL is confusing. If you had named this sumLabel or something logical, you'd have seen the problem and the solution off the bat and not have had to ask here. You have the same problem with your other JLabel.

  12. #12
    hamad3914 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    ok what you can do is see where I am going wrong and Make all the necessary corrections in the Code and post it here :D Job All Done.

    And lol i had forgot to use full words instead of chat slangs :P

  13. #13
    hamad3914 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    set the text in this JLabel
    how do I do this exactly? :P

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hamad3914 View Post
    ok what you can do is see where I am going wrong and Make all the necessary corrections in the Code and post it here :D Job All Done.
    That's not how it's done here as we don't believe in cheating you out of an opportunity to create code yourself and to learn.


    how do I do this exactly?
    You set the text of the JLabel, by calling setText(...) on it, just as you've already done in this code. The code that you've posted is your code, correct?

  15. #15
    hamad3914 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    yea lol its my code hahaahha ... ok nt the whole code jus the parts tht need to be fixed :D

    look dude ill tel u

    everything works fine ... all i need to know is how to make the label JL and JL1 disappear when i click the cancel button. And wen i call setText() it sets the text. but i need to know a way to make is disappear when i click cancel. When i click cancel even tough i say
    Java Code:
    JP.setVisible(false);
    it does not remove the label :S

  16. #16
    ebresie is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Were did you plan to add() items to your JP?

    And did you mean to have two variables named JL (global and local variable)?

  17. #17
    hamad3914 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by ebresie View Post
    Were did you plan to add() items to your JP?

    And did you mean to have two variables named JL (global and local variable)?
    no ... i just want one JP which if i make local to the first if function does not get removed if i make it global then it does not know what the sum is because sum is local :P

  18. #18
    ebresie is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Add Label to Panel

    You aren't addding JL to your JP panel. You are adding them to p7. With nothing added to JP, even if you do use setVisible correctly, it won't make anything show or disappear.

    Once you have added your lablels, you can update them with calls like:

    JL.setText("whatever you want goes here");
    JL1.setText("you new value goes here");

  19. #19
    hamad3914 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Ok i did that ... but then even if i dont add to the panel JP i can still set JL and JL1 to

    Java Code:
    setVisible(false);
    and this does not make the JL disappear when i do this:

    Java Code:
    if(arg0.getSource()==b2)
    	{
    		textbox1.setText("");
    		textbox2.setText("");
    		textbox3.setText("");
    		c1.setSelected(false);
    		c2.setSelected(false);
    		c3.setSelected(false);
    		q.setSelectedIndex(0);
    		JL.setVisible(false);
                    JL1.setVisible(false);
    	}

Similar Threads

  1. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  2. JFrame from a JFrame in project
    By anah in forum AWT / Swing
    Replies: 2
    Last Post: 04-19-2010, 06:20 PM
  3. Replies: 6
    Last Post: 04-12-2010, 11:33 AM
  4. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  5. Replies: 4
    Last Post: 04-22-2009, 11:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •