473,326 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Update JTextField in another class

JoeMac3313
The question is how can I update the JTextField in Class CreditDisplayField when I hit the button in class CreditButton? I wrote in a test printing to console to make sure the "logic.addCredits();" in the class ButtonHandler is being done and that works but the "creditDisplay.update();" in the same area still shows 0 and does not update w/ the added amount of credits. This was originally a homework assignment that I completed in 3 very large classes and I was told to break it down to better understand OOP so I am trying to get it to work. Any suggestions on how to write it would be very helpful.

JoeMac

Expand|Select|Wrap|Line Numbers
  1. import java.awt.FlowLayout;
  2. import javax.swing.JLabel;
  3. import javax.swing.JPanel;
  4. import javax.swing.JTextField;
  5.  
  6.  
  7. public class CreditDisplayField extends JPanel{
  8.  
  9.     private JPanel creditDisplay;
  10.     private JLabel credits;
  11.     private JTextField creditsLeft;
  12.     private SlotLogic logic = new SlotLogic();
  13.  
  14.     CreditDisplayField(){
  15.         creditDisplay = new JPanel();
  16.         credits = new JLabel("Credits");
  17.         creditsLeft = new JTextField(6);
  18.         creditsLeft.setText("" + logic.getCredits());
  19.         creditDisplay.add(credits);
  20.         creditDisplay.add(creditsLeft);
  21.         this.setLayout(new FlowLayout());
  22.         this.add(creditDisplay);
  23.     }
  24.     public void update(){
  25.  
  26.         creditsLeft.setText("" + logic.getCredits());
  27.  
  28.     }
  29.  
  30. }
  31.  
Expand|Select|Wrap|Line Numbers
  1. import java.awt.FlowLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JButton;
  5. import javax.swing.JPanel;
  6.  
  7.  
  8.  
  9. public class CreditButton extends JPanel {
  10.     private JPanel creditButton;
  11.     private JButton credit;
  12.     private ButtonHandler addCredits = new ButtonHandler();
  13.     SlotLogic logic = new SlotLogic();
  14.     CreditDisplayField creditDisplay = new CreditDisplayField();
  15.  
  16.     CreditButton(){
  17.         creditButton = new JPanel();
  18.         credit = new JButton("Add Credits");
  19.         creditButton.add(credit);
  20.  
  21.         credit.addActionListener(addCredits);
  22.  
  23.         this.setLayout(new FlowLayout());
  24.         this.add(creditButton);
  25.     }
  26.  
  27.  
  28.     private class ButtonHandler implements ActionListener{    
  29.         public void actionPerformed(ActionEvent event){
  30.             logic.addCredits();
  31.             creditDisplay.update();
  32.         }
  33.     }// inner class ButtonHandler
  34. }
  35.  
Nov 28 '07 #1
0 2020

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Alexander Mazo | last post by:
Hi! I'm new in JAVA. I'm wont change "Home" and "End", but nothing receive. public class RLTextField { public static String s = new String(); public static void main(String args) { JFrame...
5
by: Stephen Plotnick | last post by:
I'm very new to VB.NET 2003 Here is what I have accomplished: MainSelectForm - Selects an item In a public class I pass a DataViewRow to ItemInformation1 Form ItemInformation2 Form
3
by: eholz1 | last post by:
Hello PHP programmers. I had a brilliant idea on one of my pages that selects some data from my mysql database. I first set the page up to display some info and an image, just one item, with a...
4
by: thesti | last post by:
hi, i wonder a way to validate a JTextField to only receive numerical input. i've tried with the KeyListener and consumer() but it doesn't work. i've tried InputVerifier but it also doesn't work,...
2
by: all eyes | last post by:
hi all, i am using here JTextField to get sum of all previously pressed numbers in one button called (+) and so on. there is only one button take care of addition and displaying the result in the...
1
by: ahmed222too | last post by:
the jtextfield donot accept Arabic language font when i run this code jtextfield.settext="ذهب الولد إلى المدرسة"; the arabic font change into another font in the jtextfield
4
by: ahmed222too | last post by:
when i read data from file by code into jtextfield the lines of the file apprear in Concatenation in the jtextfield (without Enters between lines) i want to transfere the content of the file to the...
3
by: Kid Programmer | last post by:
Hello guys. I was wondering if it is possible to make a JTextField read only. In a program I am writing I don't want the user to be able to edit the text in a JTextField.
1
by: dig909 | last post by:
I am trying to compare a number inserted into a jtextfield with a number generated with the program. Unfortunately the numbers never evaluate as equal. Here is an example of the code I'm working...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.