Don Corleone
Don Corleone

Reputation: 361

How to update frames after event in java?

Background : I'm a complete newbie in Java.

I am trying to build a small applet which has 4 frames, one to ask user for two numbers and then three different frames, (1) to show sum, (2) to show difference, and (3) to show product of numbers. After user clicks on 'Calculate' button I'm able to get values inside the required variables (i,j) I'm displaying in other frames but I don't know how will other frames show the updated values after 'calculate button' is pressed. Instead, in my current code they keep showing default value.

import java.awt.*;
import javax.swing.JApplet;
import javax.swing.*;
import java.awt.event.*;

public class Applet extends JFrame implements ActionListener 
 {

 JTextField txtdata,txdatas;
    JButton calbtn = new JButton("Calculate");
    String c,d;
    int i=0,j=0;
public Applet() {

    JTabbedPane tabbedPane = new JTabbedPane ();
    JPanel tabonepanel = new JPanel();
    JPanel tabtwopanel = new JPanel ();
    JPanel tabtthreepanel = new JPanel();
    JPanel tabfourpanel = new JPanel();
    JLabel sumlabel = new JLabel ("The Sum is "+(i+j)); 


    tabonepanel.add(sumlabel);
    tabtwopanel.add(new JLabel("The Difference is "+ (i-j)));
    tabtthreepanel.add(new JLabel("The Product is "+ (i*j)));
    tabfourpanel.add(new JLabel("Enter two numbers"));
    txtdata= new JTextField();
    txdatas=new JTextField();
    tabfourpanel.add(txtdata);
    tabfourpanel.add(txdatas);
    tabfourpanel.add(calbtn); 
    calbtn.addActionListener(this);

    tabbedPane.addTab("Enter",tabfourpanel);
    tabbedPane.addTab("Sum",tabonepanel);
    tabbedPane.addTab("Dif",tabtwopanel);
    tabbedPane.addTab("Mul",tabtthreepanel);  

    this.getContentPane().add(tabbedPane);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setTitle("Tabbed Frames Demo");
    this.setVisible(true);
    this.setSize(300,450);

}

  public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == calbtn) {
        c =   txtdata.getText()  ; 
        d =   txdatas.getText() ;
        i = Integer.parseInt(c);
        j = Integer.parseInt(c);




    }

}
   public static void main(String[] args)
 { 
  Applet x = new Applet();
 }
     }

Upvotes: 1

Views: 336

Answers (2)

camickr
camickr

Reputation: 324118

I am trying to build a small applet

It is not an "applet". You are building and application. Don't call your class "Applet" because there is an AWT class by that name which is confusing. Give you class a better descriptive name

which has 4 frames,

You have 1 JFrame. You have 4 tabs in a JTabbedPane.

After user clicks on 'Calculate' button I'm able to get values inside the required variables (i,j)

Because you use the getText() method on those varaibles.

but I don't know how will other frames show the updated values after 'calculate button' is pressed.

You use the "getter" method above to get the values. So you can use the "setter" method to set the text on the component when the value changes.

JLabel sumlabel = new JLabel ("The Sum is "+(i+j)); 

The above label (and any other label you want to change the text on) will need to be defined as an instance variable in your class the same way you define the text fields (txtdata and txdatas) as instance variables.

tabtwopanel.add(new JLabel("The Difference is "+ (i-j)));
tabtthreepanel.add(new JLabel("The Product is "+ (i*j)));

These will also need to be instance variables. The text is not changed because the value of i, j changes. You need to manually reset the text every time you do the calculation.

Upvotes: 4

FredK
FredK

Reputation: 4084

enter code hereYour sum, difference, and product labels should be declared as instance variables:

JTextField txtdata,txdatas;
JButton calbtn = new JButton("Calculate");
JLabel sumLabel;
JLabel differenceLabel;
JLabel productLabel;
String c,d;
int i=0,j=0;

Then create them in the constructor:

sumLabel = new JLabel("The Sum is "+(i+j));
differenceLabel = new JLabel("The Difference is "+(i-j));
productLabel = new JLabel("The Product is "+(i*j));

Then in your actionPerformed method, change the label text:

   sumLabel.setText("("The Sum is "+(i+j));
   //etc.

note that you may have to revalidate the parent of the labels if the size of the label string changes.

Upvotes: 3

Related Questions