Results 1 to 6 of 6
- 12-30-2011, 01:44 AM #1
Member
- Join Date
- Dec 2011
- Location
- Southern Wisconsin
- Posts
- 8
- Rep Power
- 0
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:
Java Code:public class awesomeCalc { public static void main(String[] args) { new buildCalc(); } }
Second Class:
Third ClassJava 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); } }
Fourth ClassJava 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"); } }
Java 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(); } } }
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.
- 12-30-2011, 02:10 AM #2
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: Changing the text in a JTextField
The error is in your buildLabel class. Try removing the JTextField from the constructor:
Like this:
Java Code:public buildLabel() { display = new JTextField("0"); add(display); setVisible(true); }
- 12-30-2011, 07:55 AM #3
Member
- Join Date
- Dec 2011
- Location
- Southern Wisconsin
- Posts
- 8
- Rep Power
- 0
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:
any further help or resources you can point me at would be appreciatedJava 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"); } }
-
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.
- 12-31-2011, 04:40 AM #5
Member
- Join Date
- Dec 2011
- Posts
- 8
- Rep Power
- 0
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:
Java 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); } }Java 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(); } } }
- 12-31-2011, 05:52 AM #6
Member
- Join Date
- Dec 2011
- Location
- Southern Wisconsin
- Posts
- 8
- Rep Power
- 0
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 :)
Similar Threads
-
Remember the text in JTextField
By seredi in forum New To JavaReplies: 11Last Post: 09-21-2011, 06:55 PM -
jtextfield, actionlistener and changing background of parent jpanel
By kpwane in forum AWT / SwingReplies: 4Last Post: 04-19-2011, 05:09 AM -
Changing text color in SWT
By ourimaler in forum SWT / JFaceReplies: 1Last Post: 06-02-2010, 01:08 PM -
Reaplacing text in JTextField
By PhQ in forum New To JavaReplies: 5Last Post: 04-14-2010, 12:25 AM -
Changing the color of text
By Lang in forum New To JavaReplies: 1Last Post: 11-04-2007, 09:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks