Results 1 to 4 of 4
  1. #1
    Irish Rayray is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Adding Text from button to text field.

    Hi All,

    This is my code so far.
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class NestedLayoutsLab extends JFrame implements ActionListener{
    	
    	private JPanel masterPanel = new JPanel(new BorderLayout());
    	private JPanel minorPanel = new JPanel(new GridLayout(4, 2));
    	
    	private JButton Clear = new JButton("CE");
    	private JButton Dot = new JButton(".");
    	private JButton Zero = new JButton("0");
    	private JButton One = new JButton("1");
    	private JButton Two = new JButton("2");
    	private JButton Three = new JButton("3");
    	private JButton Four = new JButton("4");
    	private JButton Five = new JButton("5");
    	private JButton Six = new JButton("6");
    	private JButton Seven = new JButton("7");
    	private JButton Eight = new JButton("8");
    	private JButton Nine = new JButton("9");
    	
    
    	private JTextField theNorthLabel = new JTextField("No Input Entered");
    
    	public NestedLayoutsLab(String aStr){
    		super(aStr);
    		
    		Clear.addActionListener(this);
    		Dot.addActionListener(this);
    		Zero.addActionListener(this);
    		One.addActionListener(this);
    		Two.addActionListener(this);
    		Three.addActionListener(this);
    		Four.addActionListener(this);
    		Five.addActionListener(this);
    		Six.addActionListener(this);
    		Seven.addActionListener(this);
    		Eight.addActionListener(this);
    		Nine.addActionListener(this);
    		
    		masterPanel.add(theNorthLabel, BorderLayout.NORTH);
    		
    		minorPanel.add(Nine);
    		minorPanel.add(Eight);
    		minorPanel.add(Seven);
    		minorPanel.add(Six);
    		minorPanel.add(Five);
    		minorPanel.add(Four);
    		minorPanel.add(Three);
    		minorPanel.add(Two);
    		minorPanel.add(One);
    		minorPanel.add(Zero);
    		minorPanel.add(Dot);
    		minorPanel.add(Clear);		
    		
    		theNorthLabel.setHorizontalAlignment(JLabel.CENTER);
    		
    		masterPanel.add(minorPanel);
    		getContentPane().add(masterPanel);
    
            setSize(350,200);
    		setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource().equals(Nine)){
    			theNorthLabel.setText("9");
    		}else if(e.getSource().equals(Eight)){
    			theNorthLabel.setText("8");
    		}else if(e.getSource().equals(Seven)){
    			theNorthLabel.setText("7");
    		}else if(e.getSource().equals(Six)){
    			theNorthLabel.setText("6");
    		}else if(e.getSource().equals(Five)){
    			theNorthLabel.setText("5");
    		}else if(e.getSource().equals(Four)){
    			theNorthLabel.setText("4");
    		}else if(e.getSource().equals(Three)){
    			theNorthLabel.setText("3");
    		}else if(e.getSource().equals(Two)){
    			theNorthLabel.setText("2");
    		}else if(e.getSource().equals(One)){
    			theNorthLabel.setText("1");
    		}else if(e.getSource().equals(Zero)){
    			theNorthLabel.setText("0");
    		}else if(e.getSource().equals(Dot)){
    			theNorthLabel.setText(".");
    		}else if(e.getSource().equals(Clear)){
    			theNorthLabel.setText("");
    		}
    	}	
    	public static void main(String[] args){
    		new NestedLayoutsLab("Border Layout");
    	}
    }
    What I'm trying to so is when you click a button instead of setting the Field to = the button i want to add the digit to the field.
    I'm still looking into this and if i figure it out i will post my solution but some help would be appropriated as it may help speed up things.

    Thank you.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,209
    Rep Power
    6

    Default Re: Adding Text from button to text field.

    theNorthLabel.setText(theNorthLabel.getText()+ "0");
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Irish Rayray is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Re: Adding Text from button to text field.

    Haha I can't believe i never tried that.
    Thanks :)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,929
    Rep Power
    16

    Default Re: Adding Text from button to text field.

    Moved from Advanced Java. Not in any way an advanced question either.

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Similar Threads

  1. Replies: 5
    Last Post: 05-21-2012, 10:33 AM
  2. adding text to a text area dynamically
    By tom2zip in forum AWT / Swing
    Replies: 3
    Last Post: 01-17-2012, 05:58 AM
  3. getting text from a text field
    By tomandhisjones in forum New To Java
    Replies: 9
    Last Post: 04-17-2011, 08:21 AM
  4. Regarding Text Field
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-05-2008, 11:01 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
  •