1 Attachment(s)
Changing the text in a JTextField
I have a calculator problem I'm working on and am getting an error. I will post the classes I have and the error I'm getting:
First Class:
Code:
public class awesomeCalc
{
public static void main(String[] args)
{
new buildCalc();
}
}
Second Class:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class buildCalc extends JFrame
{
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 200;
public buildLabel topDisplay;
private buildButtons buttonDisplay;
public buildCalc()
{
topDisplay = new buildLabel();
buttonDisplay = new buildButtons();
setTitle("Awesome Calculator!");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(topDisplay, BorderLayout.NORTH);
add(buttonDisplay, BorderLayout.CENTER);
pack();
setVisible(true);
}
}
Third Class
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class buildLabel extends JPanel
{
public JTextField display;
private String currentLabel = "0";
public buildLabel()
{
JTextField display = new JTextField("0");
add(display);
setVisible(true);
}
public void changeDisplay()
{
display.setText("1");
}
}
Fourth Class
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class buildButtons extends JPanel
{
private JButton num1;
private JButton num2;
private JButton num3;
private JButton num4;
private JButton num5;
private JButton num6;
private JButton num7;
private JButton num8;
private JButton num9;
private JButton num0;
private JButton clear;
private JButton numDec;
private JButton add;
private JButton subtract;
private JButton multiply;
private JButton divide;
public buildButtons()
{
setLayout(new GridLayout(4,4));
JButton num1 = new JButton("1");
JButton num2 = new JButton("2");
JButton num3 = new JButton("3");
JButton num4 = new JButton("4");
JButton num5 = new JButton("5");
JButton num6 = new JButton("6");
JButton num7 = new JButton("7");
JButton num8 = new JButton("8");
JButton num9 = new JButton("9");
JButton num0 = new JButton("0");
JButton numDec = new JButton(".");
JButton clear = new JButton("Clear");
JButton add = new JButton("+");
JButton subtract = new JButton("-");
JButton multiply = new JButton("*");
JButton divide = new JButton("/");
num1.addActionListener(new num1Listener());
add(num1);
add(num2);
add(num3);
add(divide);
add(num4);
add(num5);
add(num6);
add(multiply);
add(num7);
add(num8);
add(num9);
add(subtract);
add(clear);
add(num0);
add(numDec);
add(add);
setVisible(true);
}
buildLabel changes = new buildLabel();
private class num1Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
changes.changeDisplay();
}
}
}
Attachment 2428
The problem occurs when I press the num1 button. I am trying to set the textfield to the number 1 and failing miserably. Any help would be appreciated.
Re: Changing the text in a JTextField
The error is in your buildLabel class. Try removing the JTextField from the constructor:
Like this:
Code:
public buildLabel()
{
display = new JTextField("0");
add(display);
setVisible(true);
}
Re: Changing the text in a JTextField
I put in your fix and I'm no longer getting the error but it's not actually changing the text when I call the method from the buildButtons class. The following is what I changed the buildLabel class to:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class buildLabel extends JPanel
{
public JTextField display;
private String currentLabel = "0";
public buildLabel()
{
display = new JTextField("0");
add(display);
setVisible(true);
}
public void changeDisplay()
{
display.setText("1");
}
}
any further help or resources you can point me at would be appreciated
Re: Changing the text in a JTextField
You're changing a JTextField, just not the one that is being displayed. Just look at your code and you'll see that you display the buildLabel that's been placed in the variable called topDisplay, and then you change the JLabel in ta buildLabel object that is held by the in a variable called changes. Again, they're two different objects. If you want to see a change in the state of a visualized object, you must call methods on that very same object.
Re: Changing the text in a JTextField
As Fubarable said, you're creating a new instance of "buildLabel" in your "buildButtons" class, and in consequence, you're editing the JTextField of "changes" (which isn't visible) instead the topDisplay's JTextField. Try this out:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class buildCalc extends JFrame
{
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 200;
public buildLabel topDisplay;
private buildButtons buttonDisplay;
public buildCalc()
{
topDisplay = new buildLabel();
buttonDisplay = new buildButtons(topDisplay);
setTitle("Awesome Calculator!");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(topDisplay, BorderLayout.NORTH);
add(buttonDisplay, BorderLayout.CENTER);
pack();
setVisible(true);
}
}
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class buildButtons extends JPanel
{
private JButton num1;
private JButton num2;
private JButton num3;
private JButton num4;
private JButton num5;
private JButton num6;
private JButton num7;
private JButton num8;
private JButton num9;
private JButton num0;
private JButton clear;
private JButton numDec;
private JButton add;
private JButton subtract;
private JButton multiply;
private JButton divide;
private buildLabel changes;
public buildButtons(buildLabel changes)
{
this.changes = changes;
setLayout(new GridLayout(4,4));
JButton num1 = new JButton("1");
JButton num2 = new JButton("2");
JButton num3 = new JButton("3");
JButton num4 = new JButton("4");
JButton num5 = new JButton("5");
JButton num6 = new JButton("6");
JButton num7 = new JButton("7");
JButton num8 = new JButton("8");
JButton num9 = new JButton("9");
JButton num0 = new JButton("0");
JButton numDec = new JButton(".");
JButton clear = new JButton("Clear");
JButton add = new JButton("+");
JButton subtract = new JButton("-");
JButton multiply = new JButton("*");
JButton divide = new JButton("/");
num1.addActionListener(new num1Listener());
add(num1);
add(num2);
add(num3);
add(divide);
add(num4);
add(num5);
add(num6);
add(multiply);
add(num7);
add(num8);
add(num9);
add(subtract);
add(clear);
add(num0);
add(numDec);
add(add);
setVisible(true);
}
private class num1Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
changes.changeDisplay();
}
}
}
Re: Changing the text in a JTextField
Thanks guys the solution you guys gave works better than what I came up with. I was sending a function from the buildButtons to the buildCalc and having the buildCalc then call the buildLabel and change the text. Passing the object works way better thanks again :)