5 Replies - 4928 Views - Last Post: 27 March 2011 - 10:46 PM Rate Topic: -----

#1 Stinger169  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 13-February 11

Need help getting TextArea to display numbers when buttons are pressed

Posted 27 March 2011 - 05:32 PM

My assignment is to create a simple cellphone type display with a numberpad, an output area and a clear button.
I have the thing pretty much done but when I press the buttons the text output disappears to show only the most recently pressed button.

Fore example when I press 1 then 2 just 2 shows up instead of 12.
Here is my code:

\\\main\\\
import javax.swing.JFrame;



public class LayoutManager {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		JFrame frame = new JFrame("Phone Display");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.getContentPane().add(new PhoneDisplay(null));
		
		frame.pack();
		frame.setVisible(true);
		

	}

}
\\\\\\layoutmanager\\\
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class PhoneDisplay extends JFrame implements ActionListener {

	private JLabel mainTxtArea;
	private ButtonGroup numberBtns;
	private JButton clearBtn;
	private JButton one;
	private JButton two;
	private JButton three;
	private JButton four;
	private JButton five;
	private JButton six;
	private JButton seven;
	private JButton eight;
	private JButton nine;
	private JButton star;
	private JButton zero;
	private JButton pound;
	String lineSeparator = System.getProperty("line.separator");
	
	
public PhoneDisplay(String name) {
	super();
	JPanel infoPanel = new JPanel();
	mainTxtArea = new JLabel("null");
	infoPanel.add(mainTxtArea);
	getContentPane().add(infoPanel, BorderLayout.NORTH);
	
	JPanel numberButtons = new JPanel();
	numberBtns = new ButtonGroup();
	numberButtons.setLayout(new GridLayout(0,3));
	one = new JButton("1");
	two = new JButton("2");
	three = new JButton("3");
	four = new JButton("4");
	five = new JButton("5");
	six = new JButton("6");
	seven = new JButton("7");
	eight = new JButton("8");
	nine = new JButton("9");
	star = new JButton("*");
	zero = new JButton("0");
	pound = new JButton("#");
	numberBtns.add(one);
	numberBtns.add(two);
	numberBtns.add(three);
	numberBtns.add(four);
	numberBtns.add(five);
	numberBtns.add(six);
	numberBtns.add(seven);
	numberBtns.add(eight);
	numberBtns.add(nine);
	numberBtns.add(star);
	numberBtns.add(zero);
	numberBtns.add(pound);
	numberButtons.add(one);
	numberButtons.add(two);
	numberButtons.add(three);
	numberButtons.add(four);
	numberButtons.add(five);
	numberButtons.add(six);
	numberButtons.add(seven);
	numberButtons.add(eight);
	numberButtons.add(nine);
	numberButtons.add(star);
	numberButtons.add(zero);
	numberButtons.add(pound);
	getContentPane().add(numberButtons, BorderLayout.WEST);
	
	JPanel clearButton = new JPanel();
	clearBtn = new JButton("Clear");
	clearButton.add(clearBtn);
	getContentPane().add(clearButton, BorderLayout.EAST);
	
	pack();
	setVisible(true);
	
	clearBtn.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);
	star.addActionListener(this);
	zero.addActionListener(this);
	pound.addActionListener(this);
	
	//addWindowListener(new WindowAdapter() {
		//public void windowClosing(WindowEvent event) {
		//	System.exit(0);
		//}
	//});
}

public void actionPerformed(ActionEvent event) {
	
	Object srcObj = event.getSource();
	
	if (srcObj == clearBtn) {
		mainTxtArea.setText("");
		
	} else if (srcObj == one) {
			mainTxtArea.setText("1");
			
		}
	else if (srcObj == two) {
		mainTxtArea.setText("2");
		
	}
	else if (srcObj == three) {
		mainTxtArea.setText("3");
		
	}
	else if (srcObj == four) {
		mainTxtArea.setText("4");
		
	}
	else if (srcObj == five) {
		mainTxtArea.setText("5");
		
	}
	else if (srcObj == six) {
		mainTxtArea.setText("6");
		
	}
	else if (srcObj == seven) {
		mainTxtArea.setText("7");
		
	}
	else if (srcObj == eight) {
		mainTxtArea.setText("8");
		
	}
	else if (srcObj == nine) {
		mainTxtArea.setText("9");
		
	}
	else if (srcObj == star) {
		mainTxtArea.setText("*");
		
	}
	else if (srcObj == zero) {
		mainTxtArea.setText("0");
		
	}
	else if (srcObj == pound) {
		mainTxtArea.setText("#");
		
  }
 }
}


I know there is probably an easier way using arrays or something to make this a hell of a lot shorter and would appreciate any advice on that but my main concern is getting this to output strings of numbers instead of just one at a time since its due at midnight.

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Need help getting TextArea to display numbers when buttons are pressed

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Need help getting TextArea to display numbers when buttons are pressed

Posted 27 March 2011 - 05:40 PM

If PhoneDisplay extends JFrame

Why do you make a JFrame

JFrame frame = new JFrame("Phone Display");

instead of

PhoneDisplay pd = new PhoneDisplay("bla bla bla");
Was This Post Helpful? 1
  • +
  • -

#3 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: Need help getting TextArea to display numbers when buttons are pressed

Posted 27 March 2011 - 05:42 PM

All your action listener does is delete the current text:
mainTxtArea.setText("#"); //One example



What you want is to add the current text and the symbols like so:

mainTxtArea.setText( mainTxtArea.getText() + " " + "#");



Do it for all of them.

Hope I helped.

edit- I also got that problem that pbl mentioned. I assume you made that so we could test it...hopefully.

This post has been edited by Ember: 27 March 2011 - 05:43 PM

Was This Post Helpful? 2
  • +
  • -

#4 Stinger169  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 13-February 11

Re: Need help getting TextArea to display numbers when buttons are pressed

Posted 27 March 2011 - 06:19 PM

pbl, I see what you mean now... originally I didnt have PhoneDisplay extending JFrame but then I had some difficulties with compile errors and I never changed it in LayoutManager.

Ember, that worked perfectly and most importantly, I see why it worked.

I am confused though about why :
  mainTxtArea.setText( mainTxtArea.getText() + "2"); 


wouldnt work but then when I tried it like you said:
mainTxtArea.setText( mainTxtArea.getText() + " " + "2");



it worked fine.

not sure how a space makes the whole thing work.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Need help getting TextArea to display numbers when buttons are pressed

Posted 27 March 2011 - 08:00 PM

View PostStinger169, on 27 March 2011 - 08:19 PM, said:

not sure how a space makes the whole thing work.

me neither...
Was This Post Helpful? 0
  • +
  • -

#6 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: Need help getting TextArea to display numbers when buttons are pressed

Posted 27 March 2011 - 10:46 PM

Oh, I only put the space in because I find it hard to read otherwise. It worked fine without the space for me...Odd.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1