I am writing a program that allows a user to enter the name and phone-number of a contact and have the information show up as a button that when clicked, will reset the text fields to the information shown on the button.
My program does not reset the text of the of text field when the button is pressed. There are no compile-time or run-time errors. Please let me know what I am doing wrong. Thanks.
Here is the code for the CellPhone class.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
publicclass CellPhone extends JFrame implements ActionListener{
static InfoButton button; // button holding information
JPanel master = new JPanel(); // Panel holding all other panels
JPanel numbers = new JPanel(); // Panel holding number buttons
JPanel right = new JPanel(); // Panel holding text-fields and JButtons
JPanel view = new JPanel(); // Panel inside JScrollPane
Dimension size = new Dimension(355,155); //Size of JScrollPane
JScrollPane scroller = new JScrollPane(view); // Pane holding InfoButtons
JButton[] number = new JButton[12]; // Numbers
JButton save = new JButton("Save"); // Save contact
JTextArea numberField = new JTextArea();
JTextField nameField = new JTextField(10);
JRadioButton cell = new JRadioButton("Cell");
JRadioButton work = new JRadioButton("Work");
JRadioButton home = new JRadioButton("Home");
ButtonGroup option = new ButtonGroup();
boolean count = false; // desides if there are numbers in number-field
int array = 0; // number of buttons
String name;
String number2;
String type;
public CellPhone(){
super("Cell Phone");
numbers.setLayout(new GridLayout(4,3));
right.setLayout(new GridLayout(6,1));
for(int i=0; i<=9; i++){
number[i] = new JButton(""+i);
number[i].addActionListener(this);
}
number[10] = new JButton("*");
number[11] = new JButton("#");
number[10].addActionListener(this);
number[11].addActionListener(this);
numbers.add(number[1]);
numbers.add(number[2]);
numbers.add(number[3]);
numbers.add(number[4]);
numbers.add(number[5]);
numbers.add(number[6]);
numbers.add(number[7]);
numbers.add(number[8]);
numbers.add(number[9]);
numbers.add(number[10]);
numbers.add(number[0]);
numbers.add(number[11]);
nameField.setText("Name: ");
numberField.setText("Number: ");
scroller.setPreferredSize(size);
scroller.setMaximumSize(size);
cell.addActionListener(this);
home.addActionListener(this);
work.addActionListener(this);
save.addActionListener(this);
option.add(cell);
option.add(work);
option.add(home);
right.add(nameField);
right.add(numberField);
right.add(cell);
right.add(work);
right.add(home);
right.add(save);
master.add(scroller);
master.add(numbers);
master.add(right);
add(master);
}
publicvoid actionPerformed(ActionEvent e){
if(numberField.getText().matches("Number: ")){
}else count=true;
if(e.getSource()==number[0]){
if(count == false)
numberField.setText("");
numberField.append("0");
count = true;
}
if(e.getSource()==number[1]){
if(count == false)
numberField.setText("");
numberField.append("1");
count = true;
}
if(e.getSource()==number[2]){
if(count == false)
numberField.setText("");
numberField.append("2");
count = true;
}
if(e.getSource()==number[3]){
if(count == false)
numberField.setText("");
numberField.append("3");
count = true;
}
if(e.getSource()==number[4]){
if(count == false)
numberField.setText("");
numberField.append("4");
count = true;
}
if(e.getSource()==number[5]){
if(count == false)
numberField.setText("");
numberField.append("5");
count = true;
}
if(e.getSource()==number[6]){
if(count == false)
numberField.setText("");
numberField.append("6");
count = true;
}
if(e.getSource()==number[7]){
if(count == false)
numberField.setText("");
numberField.append("7");
count = true;
}
if(e.getSource()==number[8]){
if(count == false)
numberField.setText("");
numberField.append("8");
count = true;
}
if(e.getSource()==number[9]){
if(count == false)
numberField.setText("");
numberField.append("9");
count = true;
}
if(e.getSource()==number[10]){
if(count == false)
numberField.setText("");
numberField.append("*");
count = true;
}
if(e.getSource()==number[11]){
if(count == false)
numberField.setText("");
numberField.append("#");
count = true;
}
if(e.getSource()==save){
String type=""; // Type of number
if(option.getSelection().equals(cell.getModel())){
type = "Cell";
}
if(option.getSelection().equals(work.getModel())){
type = "Work";
}
if(option.getSelection().equals(home.getModel())){
type = "Home";
}
String name = nameField.getText();
String number = numberField.getText();
button = new InfoButton(name, number,type); // information of button
view.setLayout(new GridLayout(array+1,1));
view.add(button);
master.revalidate();
array++;
}
}
/* public void setAll(String n, String num, String tp){
name = n;
number2 = num;
type = tp;
nameField.setText(name);
/*_nameField.setText("Jake");
System.out.println(n);
System.out.println(num);
System.out.println(tp);
numberField.setText(num);
//_numberField.setText("2683053");
if(type.matches("Cell")){
cell.setSelected(true);
}
if(type.matches("Home")){
home.setSelected(true);
}
if(type.matches("Work")){
work.setSelected(true);
}
public void setName(String activate){
nameField.setText(button.getName());
}
public void setNumber(String activate){
numberField.setText(button.getNumber());
}
public void setType(String activate){
if(button.getType().matches("Cell")){
cell.setSelected(true);
}
if(button.getType().matches("Home")){
home.setSelected(true);
}
if(button.getType().matches("Work")){
work.setSelected(true);
}
}
/*
public void settehName(String n){
_nameField.setText(n);
}
public void setNumber(String num){
_numberField.setText(num);
}
public void setType(String tp){
if(tp.matches("Cell")){
cell.setSelected(true);
}
if(tp.matches("Home")){
home.setSelected(true);
}
if(tp.matches("Work")){
work.setSelected(true);
}
}
*/
publicstaticvoid main(String [] args){
CellPhone cp = new CellPhone();
cp.setBounds(0,0,780,210);
cp.setVisible(true);
}
}
Here is the code for the InfoButton class.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
publicclass InfoButton extends JButton implements ActionListener{
static CellPhone cp = new CellPhone();
String name; // name of contact
String number; // contact number
String type; // type of number
String store; // all information
String info; // Text on button
/* These variables hold substrings of the
* text on the button.
*/
String name1;
String name2;
String num;
String num2;
String type1;
String type2;
public InfoButton(String n, String num, String tp){ // n is name, num is number, tp is type
name = n;
number = num;
type = tp;
store = "Name: "+name+" "+"Number: "+number+" "+"Type: "+type;
setPreferredSize(new Dimension(300,100));
setBackground(Color.WHITE);
setText(store);
addActionListener(this);
}
/* public String getName(){
return name;
}
public String getNumber(){
return number;
}
public String getType(){
return type;
}*/
publicvoid actionPerformed(ActionEvent e){
info = getText();
name1 = info.substring(info.indexOf(" "), info.indexOf(" Nu"));
name2 = name.substring(1, name.length());
num = info.substring(info.indexOf("r: "), info.indexOf(" Ty"));
num2 = num.substring(3,num.length());
type1 = info.substring(info.indexOf("pe:"), info.length());
type2 = type.substring(4, type.length());
cp.nameField.setText(name2);
cp.numberField.setText(num2);
if(type2.matches("Cell")){
cp.cell.setSelected(true);
}
if(type2.matches("Work")){
cp.work.setSelected(true);
}
if(type2.matches("Home")){
cp.home.setSelected(true);
}
}
/*
public String getName(){
return name2;
}
public String getNumber(){
return num2;
}
public String getType(){
return type2;
}
cp.setAll(name2, num2, type2);
*/
}
Well there is a little bug in ur program. The CellPhone object in InfoButton class and CellPhone object which is running right now are not identical. They both refer to different instances of CellPhone class. Solution : Modify constructor of InfoButton class as following public InfoButton(String n, String num, String tp, CellPhone c){ name = n; number = num; type = tp;